0% found this document useful (0 votes)
10 views10 pages

22

The document describes image processing techniques using Sobel masks for edge detection, followed by smoothing with filters and brightness transformation. It includes source code for implementing these techniques in a programming context, detailing functions for gradient calculation, image subtraction, and gamma transformation for enhancing color images. The results demonstrate improved visibility of features in images through these processing methods.

Uploaded by

hnytyzb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

22

The document describes image processing techniques using Sobel masks for edge detection, followed by smoothing with filters and brightness transformation. It includes source code for implementing these techniques in a programming context, detailing functions for gradient calculation, image subtraction, and gamma transformation for enhancing color images. The results demonstrate improved visibility of features in images through these processing methods.

Uploaded by

hnytyzb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Question1:

Input image:

First, the Sobel mask is used for gradient calculation to highlight the edges of the
input image.
A Sobel mask in X direction can be represented as:
 −1 0 1 
Gx =  −2 0 2
 −1 0 1 
A Sobel mask in Y direction can be represented as:
 −1 −2 −1
G y =  0 0 0 
 1 2 1 
The results of Sobel mask calculation in X direction and Y direction are shown
below.

Second, use Average Filter or Gaussian Filter to smooth the results of Sobel mask
calculation.
A 3x3 Average Filter mask can be represented as:
1 1 1
Mask = 1 1 1
1
9
1 1 1
A Gaussian Filter mask can be represented as:
( x2 + y 2 )

Mask ( x, y ) = G ( x, y ) = e 2r2

Sampling : G[ x, y] = G[m, n]

Sum : s =  G[m, n]
Normalized : G[m, n] = G[m, n] / s
The smooth processing results of gradient image are shown below.

Then, calculate the Magnitude of gradient. The Magnitude of gradient calculation


formula is as follows:
M ( x, y) = mag (f ) = Gx2 + Gy2

Finally, calculate the subtraction between the input image and the Magnitude of
gradient to get the image. The result is as follows.

Use Brightness Transformation to get the output image. Brightness Transformation


can be represented as:
Basic : g[ x, y] = f [ x, y] + c
Saturation Operation : g[ x, y] = S ( f [ x, y] + c)
Output image:

Source code:
byte[,] filter2D(byte[,]f,float[,]mask)
{

int M = mask.GetLength(0)/2;
int N = mask.GetLength(1)/2;

int w = f.GetLength(0);
int h = f.GetLength(1);

byte[,] g = new byte[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
float s = 0;
for (int m=-M;m<=M;m++)
for (int n=-N;n<=N;n++)
{
int xm = x+m;
int yn = y+n;

if (xm<0) xm = 0;
if (xm>w-1) xm = w-1;

if (yn<0) yn = 0;
if (yn>h-1) yn = h-1;

s += f[xm,yn]*mask[M+m,N+n];
}
g[x,y] = S(s);
}

return g;
}

byte[,] highpass2D(byte[,]f,float[,]mask)
{

int M = mask.GetLength(0)/2;
int N = mask.GetLength(1)/2;

int w = f.GetLength(0);
int h = f.GetLength(1);

byte[,] g = new byte[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
float s = 0;
for (int m=-M;m<=M;m++)
for (int n=-N;n<=N;n++)
{
int xm = x+m;
int yn = y+n;

if (xm<0) xm = 0;
if (xm>w-1) xm = w-1;

if (yn<0) yn = 0;
if (yn>h-1) yn = h-1;

s += f[xm,yn]*mask[M+m,N+n];

}
g[x,y] = S(s+128);
}

return g;
}
byte S(double v)
{
if (v<0) return 0;
if (v>255) return 255;
return (byte)v;
}

float[,] sobelX()
{
float[,] mask = new float[3,3];

mask[0,0]=-1; mask[1,0]= 0; mask[2,0]= 1;


mask[0,1]=-2; mask[1,1]= 0; mask[2,1]= 2;
mask[0,2]=-1; mask[1,2]= 0; mask[2,2]= 1;

return mask;
}

float[,] sobelY()
{
float[,] mask = new float[3,3];

mask[0,0]=-1; mask[1,0]=-2; mask[2,0]=-1;


mask[0,1]= 0; mask[1,1]= 0; mask[2,1]= 0;
mask[0,2]= 1; mask[1,2]= 2; mask[2,2]= 1;

return mask;
}

byte[,] get_mag(byte[,] gx,byte[,]gy)


{
int w = gx.GetLength(0);
int h = gx.GetLength(1);

byte[,] mag = new byte[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
int rx= gx[x,y]-128;
int ry= gy[x,y]-128;
mag[x,y] = S(Sqrt(rx*rx+ry*ry));
}

return mag;

byte[,] gt(byte[,]f,double b,double k)


{
int w = f.GetLength(0);
int h = f.GetLength(1);

byte[,] g = new byte[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
g[x,y] = S(k*(f[x,y]-128)+128+b);
}

return g;
}

byte[,] sub(byte[,]f1,byte[,]f2)
{
int w = f1.GetLength(0);
int h = f1.GetLength(1);

byte[,] g = new byte[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
g[x,y] = S(f1[x,y]-f2[x,y]+128);
}

return g;
}
float[,] blur_MxN(int M,int N)
{
float[,] mask = new float[2*M+1,2*N+1];

for (int m=-M;m<=M;m++)


for (int n=-N;n<=N;n++)
mask[m+M,n+N] = 1.0f/((2*M+1)*(2*N+1));

return mask;
}

float[,] ablur_mask(int size_x,int size_y)


{
return blur_MxN(size_x/2,size_y/2);
}

float[,] gblur_mask(double r)
{

int M = (int)(3*r);
int N = M;

float[,] mask = new float[2*M+1,2*N+1];

for (int m=-M;m<=M;m++)


for (int n=-N;n<=N;n++)
mask[m+M,n+N] = (float)Exp(-(m*m+n*n)/(2*r*r));

float s = 0;

for (int m=-M;m<=M;m++)


for (int n=-N;n<=N;n++)
s += mask[m+M,n+N];

for (int m=-M;m<=M;m++)


for (int n=-N;n<=N;n++)
mask[m+M,n+N] /= s;

return mask;
}

void main()
{
byte[,] f = LoadImg();
ShowImg("f",f);

byte[,] gx = highpass2D(f,sobelX());
byte[,] gy = highpass2D(f,sobelY());

ShowImg("sobel_x",gx);
ShowImg("sobel_y",gy);

//byte[,] gx_1 = filter2D(gx,ablur_mask(5,5));


//byte[,] gy_1 = filter2D(gy,ablur_mask(5,5));

byte[,] gx_1 = filter2D(gx,gblur_mask(1));


byte[,] gy_1 = filter2D(gy,gblur_mask(1));

ShowImg("gx_1",gx_1);
ShowImg("gy_1",gy_1);

ShowImg("mag",gt(sub(f,get_mag(gx_1,gy_1)),0,0.3));

Question 2:
Input color image:
I think the mountain as the main body in this color image is not prominent enough,
and the color is too light. So I use gamma transformation to make it more artistic and
vivid.
The gamma transformation can be represented as:
s = cr 
Output color image:

By using the gamma transformation to process the color image, the shadow contrast
between the front and back of the mountain is more prominent, and the cloud is more
layered than before. What’s more, it also brings out the rainbow in the distance which
is hard to percept before.
Source code:
ARGB[,] GT(ARGB[,]f,byte[]tab)
{
int w = f.GetLength(0);
int h = f.GetLength(1);

ARGB[,] g = new ARGB[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
g[x,y].R = tab[f[x,y].R];
g[x,y].G = tab[f[x,y].G];
g[x,y].B = tab[f[x,y].B];
}

return g;
}
ARGB[,] GT(ARGB[,]f,byte[]tab_r,byte[]tab_g,byte[]tab_b)
{
int w = f.GetLength(0);
int h = f.GetLength(1);

ARGB[,] g = new ARGB[w,h];

for (int y=0;y<h;y++)


for (int x=0;x<w;x++)
{
g[x,y].R = tab_r[f[x,y].R];
g[x,y].G = tab_g[f[x,y].G];
g[x,y].B = tab_b[f[x,y].B];
}

return g;
}

byte[] gamma_tab(double r)
{
byte[] tab = new byte[256];

for (int n=0;n<256;n++)


tab[n] = (byte)(Pow(n/255.0,r)*255);

return tab;
}

void main()
{
ARGB[,] f = LoadColorImg();
ShowImg("f",f);

ShowImg("g,r=4",GT(f,gamma_tab(4)));

You might also like