西安交通大学大学数字图像处理大作业
- 格式:docx
- 大小:739.51 KB
- 文档页数:15
数字图像处理
目录
作业一 (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 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; 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 board for(int i=0;i for(int j=0;j g[M+j,i]=f[j,0]; for(int i=0;i for(int j=0;j g[M+j,i+h+N]=f[j,h-1]; //copy the image for(int i=0;i for (int j=0;j g[i+M,j+N]=f[i,j]; //add left and right board for(int i=0;i for (int j=0;j g[i,j]=g[M,j]; for(int i=0;i for (int j=0;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 for(int j=0;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)); } 三运行结果 原图像: 加边界之后的图像: 均值滤波并且去除边界的图像: