当前位置:文档之家› 西安交通大学大学数字图像处理大作业

西安交通大学大学数字图像处理大作业

数字图像处理目录作业一 (1)一作业要求 (1)二源代码 (1)三运行结果 (3)作业二 (5)一作业要求 (5)二算法描述 (5)三源代码 (7)四运行结果 (10)作业一一作业要求在图像的空间域滤波操作中,会出现有部分掩膜矩阵在图像外面的情况,所以需要给图像先加入一个边界,执行完操作之后,再去掉这个边界,保证图像中所有的像素都参与矩阵运算。

二源代码byte[,] filter(byte[,]f,float[,]mask){int w = f.GetLength(0);int h = f.GetLength(1);byte[,] g = new byte[w,h];int M = mask.GetLength(0)/2;int N = mask.GetLength(1)/2;for (int y=N;y<h-N;y++)for (int x=M;x<w-M;x++){float s = 0;for (int m=-M;m<=M;m++)for (int n=-N;n<=N;n++)s+=f[x+m,y+n]*mask[M+m,N+n];g[x,y] = SByte(s);}return g;}byte SByte(double v){if (v>255) return 255;if (v<0) return 0;return (byte)v;}float[,] averagingMask(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;}byte[,] addboard(byte[,] f,int M,int N){int w=f.GetLength(0);int h=f.GetLength(1);int gw=w+2*M;int gh=h+2*N;byte[,] g=new byte[gw,gh];//add top board and bottom boardfor(int i=0;i<N;i++)for(int j=0;j<w;j++)g[M+j,i]=f[j,0];for(int i=0;i<N;i++)for(int j=0;j<w;j++)g[M+j,i+h+N]=f[j,h-1];//copy the imagefor(int i=0;i<w;i++)for (int j=0;j<h;j++)g[i+M,j+N]=f[i,j];//add left and right boardfor(int i=0;i<M;i++)for (int j=0;j<gh;j++)g[i,j]=g[M,j];for(int i=0;i<M;i++)for (int j=0;j<gh;j++)g[w+M+i,j]=g[gw-1-M,j];return g;}byte[,] removeboard(byte[,]f,int M,int N){int w=f.GetLength(0);int h=f.GetLength(1);int gw=w-2*M;int gh=h-2*N;byte[,] g=new byte[gw,gh];for(int i=0;i<gw;i++)for(int j=0;j<gh;j++)g[i,j]=f[i+M,j+N];return g;}void main(){byte[,] f = LoadImg();ShowImg("f",f);int w=f.GetLength(0);int h=f.GetLength(1);int M=10,N=20;int gw=w-2*M;int gh=h-2*N;byte[,] boardimage=new byte[gw,gh];byte[,] filterimage=new byte[gw,gh];boardimage=addboard(f,M,N);ShowImg("boardimage",boardimage);filterimage=filter(boardimage,averagingMask(M,N));ShowImg("result",removeboard(filterimage,M,N)); }三运行结果原图像:加边界之后的图像:均值滤波并且去除边界的图像:作业二一作业要求给定图像与处理结果,如图一所示,思考算法并编程实现。

图一左边为原图,右边为处理结果二算法描述Compare the result and the original image, we can see that the program change thecontrast of image and enhance the edge of Lena. Consider using intensity transform and Sobel edge detection to achieve the goal. After many experiments, I choose following intensity transform function.y0.25110x=+The graph of this function is Fig 1.Fig 1 Graph of intensity transform functionUse Sobel to detect the edge of the original image.Sobel in x direction can be written as the following transform matrix:SobleX=101 202 101 -⎡⎤⎢⎥-⎢⎥⎢⎥-⎣⎦Similar, Sobel in y direction can be written as the following transform matrix:SobleY=121 000 121 ---⎡⎤⎢⎥⎢⎥⎢⎥⎣⎦We can use SobelX and SobelY to compute the gradient image.f=f is the value of pixels in the gradient image,xg is the value of pixels in imageprocessed by SobelX, andyg is the value of pixels in image processed by SobelY. In order to decrease noise, I use the following function to process gradient image:70after before f f =-After processing, I use 0 to replace these negative values.Because the gradient image has a very high contrast, so I use intensity transform again.0.22after before f f =After that, the final result can be calculated:result y f =-All the function is chosen through experiment, maybe the result isn’t the same with the teacher’s result, but it’s very similar.三 源代码byte[,] filter(byte[,]f,float[,]mask){int w = f.GetLength(0);int h = f.GetLength(1);int M = mask.GetLength(0)/2;int N = mask.GetLength(1)/2;byte[,] g = new byte[w,h];for (int y=N;y<h-N;y++)for (int x=M;x<w-M;x++){float r = 0;for (int m=-M;m<=M;m++)for (int n=-N;n<=N;n++){r+=f[x+m,y+n]*mask[M+m,N+n];}g[x,y]=S(r);}return g;}byte[,] SobelX(byte[,]f){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 filter(f,mask);}byte[,] SobelY(byte[,]f){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 filter(f,mask);}byte S(double f){if(f>255) return 255;if(f<0) return 0;return (byte)f;}byte[,] Scale(byte[,]f,double a){int w = f.GetLength(0);int h = f.GetLength(1);for(int i=0;i<w;i++)for(int j=0;j<h;j++)f[i,j]=(byte)(f[i,j]*a);return f;}byte[,] Subnum(byte[,]f,int a){int w = f.GetLength(0);int h = f.GetLength(1);for(int i=0;i<w;i++)for(int j=0;j<h;j++)f[i,j]=S((f[i,j]-a));return f;}byte[,] Sub(byte[,]f,byte[,]g){int w = f.GetLength(0);int h = f.GetLength(1);byte[,] p = new byte[w,h];for(int x=0;x<w;x++)for(int y=0;y<h;y++)p[x,y]=S(f[x,y]-g[x,y]);return p;}byte[,] Mix(byte[,]fx,byte[,]fy){int w = fx.GetLength(0);int h = fx.GetLength(1);byte[,] g = new byte[w,h];for (int y=0;y<h;y++)for (int x=0;x<w;x++){float px =(fx[x,y]);float py =(fy[x,y]);g[x,y] = S(Sqrt(px*px+py*py));}return g;}byte[,] IntensityTransform(byte[,]f){int w = f.GetLength(0);int h = f.GetLength(1);byte[,] g = new byte[w,h];for(int i=0;i<w;i++)for(int j=0;j<h;j++)g[i,j] = S(0.25*f[i,j]+110);return g;}void main(){byte [,] I = LoadImg();ShowImg("Original",I);byte [,] I1 = IntensityTransform(I);ShowImg("IntensityTransform",I1);byte [,] I2 = SobelX(I);byte [,] I3 = SobelY(I);byte [,] I4 = Mix(I2,I3);ShowImg("Gradient",I4);byte [,] I5=Subnum(I4,70);ShowImg("DecreaseNoise",I5);byte [,] I6=Scale(I5,0.22);ShowImg("Scale",I6);byte [,] I7 = Sub(I1,I6);ShowImg("FinalResult",I7);}四运行结果原图像:灰度变换之后的图像:Sobel算子提取到的边缘图像:降低噪声之后的边缘图像:改变提取到的边缘的亮度之后得到的图像:最终结果:。

相关主题