22
22
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.
Finally, calculate the subtraction between the input image and the Magnitude of
gradient to get the image. The result is as follows.
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);
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);
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];
return mask;
}
float[,] sobelY()
{
float[,] mask = new float[3,3];
return mask;
}
return mag;
return g;
}
byte[,] sub(byte[,]f1,byte[,]f2)
{
int w = f1.GetLength(0);
int h = f1.GetLength(1);
return g;
}
float[,] blur_MxN(int M,int N)
{
float[,] mask = new float[2*M+1,2*N+1];
return mask;
}
float[,] gblur_mask(double r)
{
int M = (int)(3*r);
int N = M;
float s = 0;
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);
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);
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);
return g;
}
byte[] gamma_tab(double r)
{
byte[] tab = new byte[256];
return tab;
}
void main()
{
ARGB[,] f = LoadColorImg();
ShowImg("f",f);
ShowImg("g,r=4",GT(f,gamma_tab(4)));