数字图像处理
目录
数字图像处理...................................................................................................................................................... 1 作业一.................................................................................................................................................................. 1
一 作业要求 ............................................................................................................. 1
二 源代码 ................................................................................................................. 1
三 运行结果 ............................................................................................................. 8
作业二.................................................................................................................................................................. 9
一 作业要求 ............................................................................................................. 9
二 算法描述 ........................................................................................................... 10
三 源代码 ............................................................................................................... 13
四 运行结果 ........................................................................................................... 21
作业一
一 作业要求
在图像的空间域滤波操作中,会出现有部分掩膜矩阵在图像外面的情况,所以需要给图像先加入一个边界,执行完操作之后,再去掉这个边界,保证图像中所有的像素都参与矩阵运算。
二 源代码
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 for (int x=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; 2 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) { 3 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 board for(int i=0;i for(int i=0;i //copy the image 4 for(int i=0;i //add left and right board for(int i=0;i for(int i=0;i return g; } byte[,] removeboard(byte[,]f,int M,int N) 5 { 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 return g; } void main() { 6 byte[,] f = LoadImg(); ShowImg(\"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\ filterimage=filter(boardimage,averagingMask(M,N)); ShowImg(\"result\ 7 } 运行结果 原图像: 加边界之后的图像: 8 三 均值滤波并且去除边界的图像: 作业二 一 作业要求 9 给定图像与处理结果,如图一所示,思考算法并编程实现。 图一 左边为原图,右边为处理结果 二 算法描述 Compare the result and the original image, we can see that the program change the contrast 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.25x110 The graph of this function is Fig 1. 10 Fig 1 Graph of intensity transform function Use Sobel to detect the edge of the original image. Sobel in x direction can be written as the following transform matrix: 101202101 SobleX=Similar, Sobel in y direction can be written as the following transform matrix: 121000121 SobleY=11 We can use SobelX and SobelY to compute the gradient image. fgx2gy2 f is the value of pixels in the gradient image, gx is the value of pixels in image processed by SobelX, and gy is the value of pixels in image processed by SobelY. In order to decrease noise, I use the following function to process gradient image: fafterfbefore70 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. fafter0.22fbefore After that, the final result can be calculated: resultyf All the function is chosen through experiment, maybe the result isn’t the same with the teacher’s result, but it’s very similar. 12 源代码 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 float r = 0; for (int m=-M;m<=M;m++) 三 13 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[0,1]=-2; mask[1,1] = 0; 14 mask[2,0] = 1; 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[0,1]= 0; mask[1,1] = 0; mask[0,2]= 1; mask[1,2] = 2; return filter(f,mask); } byte S(double f) { mask[2,1] = 0; mask[2,2] = 1; 15 mask[2,0] =-1; 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 return f; } 16 byte[,] Subnum(byte[,]f,int a) { int w = f.GetLength(0); int h = f.GetLength(1); for(int i=0;i return f; } byte[,] Sub(byte[,]f,byte[,]g) { int w = f.GetLength(0); int h = f.GetLength(1); 17 byte[,] p = new byte[w,h]; for(int x=0;x 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 for (int x=0;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]; 19 for(int i=0;i return g; } void main() { byte [,] I = LoadImg(); ShowImg(\"Original\ byte [,] I1 = IntensityTransform(I); ShowImg(\"IntensityTransform\ byte [,] I2 = SobelX(I); byte [,] I3 = SobelY(I); 20 byte [,] I4 = Mix(I2,I3); ShowImg(\"Gradient\ byte [,] I5=Subnum(I4,70); ShowImg(\"DecreaseNoise\ byte [,] I6=Scale(I5,0.22); ShowImg(\"Scale\ byte [,] I7 = Sub(I1,I6); ShowImg(\"FinalResult\ } 四 运行结果 原图像: 21 灰度变换之后的图像: Sobel算子提取到的边缘图像: 22 降低噪声之后的边缘图像: 改变提取到的边缘的亮度之后得到的图像: 23 最终结果: 24 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务