@[TOC](利用matlab自带均值滤波器的代码,分别对一幅图像实现3*3,5*5,7*7,9*9的均值滤波,并对实验结果进行分析。)
@[TOC](分别给干净图像添加高斯和椒盐噪声,然后进行均值滤波、高斯滤波和中值滤波,并对实现结果进行分析。)
@[TOC](自编均值滤波器对一幅图像实现填充后,并完成3*3,5*5,7*7,9*9,11*11的均值滤波并对实验结果进行分析。)
% 读入图像
Image = imread('C:\Users\HUAWEI\Desktop\A计协\1大三重点\数字图像处理\数字图像处理第二次上机\lena.bmp');
% 设置均值滤波
H3 = fspecial('average',[3,3]);
H5 = fspecial('average',[5,5]);
H7 = fspecial('average',[7,7]);
H9 = fspecial('average',[9,9]);
% 利用滤波对图像进行处理
r3 = imfilter(Image,H3);
r5 = imfilter(Image,H5);
r7 = imfilter(Image,H7);
r9 = imfilter(Image,H9);
% 展示结果
subplot(2,3,1);imshow(Image);title('原图');
subplot(2,3,2);imshow(r3);title('3*3均值滤波结果');
subplot(2,3,3);imshow(r5);title('5*5均值滤波结果');
subplot(2,3,4);imshow(r7);title('7*7均值滤波结果');
subplot(2,3,5);imshow(r9);title('9*9均值滤波结果');
% 读入图像
Image = imread('C:\Users\HUAWEI\Desktop\A计协\1大三重点\数字图像处理\数字图像处理第二次上机\lena.bmp');
% 给原图加入高斯噪声
GaussainI = imnoise(Image,'gaussian');
% 给原图加入椒盐噪声
SaltPepperI = imnoise(Image,'salt & pepper');
% 设置均值滤波
aveFilter3 = fspecial('average',[3,3]);
% 设置高斯滤波
gausFilter3 = fspecial('gaussian',[3,3],0.8);
tempG=rgb2gray(GaussainI); %灰度处理,灰度处理后的图像是二维矩阵
tempSP=rgb2gray(SaltPepperI);
% 用均值滤波对高斯噪声图像进行处理
GJ = imfilter(GaussainI,aveFilter3);
% 用高斯滤波对高斯噪声图像进行处理
GG = imfilter(GaussainI,gausFilter3,'conv');
% 用中值滤波对高斯图像进行处理
GM = medfilt2(tempG,[3,3]);
% 用均值滤波对椒盐噪声图像进行处理
SPJ = imfilter(SaltPepperI,aveFilter3);
% 用高斯滤波对椒盐噪声图像进行处理
SPG = imfilter(SaltPepperI,gausFilter3,'conv');
% 用中值滤波对椒盐噪声图像进行处理
SPM = medfilt2(tempSP,[3,3]);
%展示结果
subplot(3,3,1);imshow(Image);title('原图');
subplot(3,3,2);imshow(GaussainI);title('添加高斯噪声后的图像');
subplot(3,3,3);imshow(SaltPepperI);title('添加椒盐噪声后的图像');
subplot(3,3,4);imshow(GJ);title('高斯噪声经均值滤波处理后');
subplot(3,3,5);imshow(GG);title('高斯噪声经高斯滤波处理后');
subplot(3,3,6);imshow(GM);title('高斯噪声经中值滤波处理后');
subplot(3,3,7);imshow(SPJ);title('椒盐噪声经均值滤波处理后');
subplot(3,3,8);imshow(SPG);title('椒盐噪声经高斯滤波处理后');
subplot(3,3,9);imshow(SPM);title('椒盐噪声经中值滤波处理后');
% 读入图像,并进行灰度处理,灰度处理后的图像是二维矩阵
Image = rgb2gray(imread('C:\Users\HUAWEI\Desktop\A计协\1大三重点\数字图像处理\数字图像处理第二次上机\lena.bmp'));
% 给图像添加高斯噪声
GaussainI=imnoise(Image,'gaussian');
%使用matlab自带的均值滤波对高斯噪声图像进行处理,用于后面与自编均值滤波函数的效果对比
H3 = fspecial('average',[3,3]);
b3 = imfilter(GaussainI,H3);
% 获取图像二维矩阵的高和宽
[h,w]=size(GaussainI);
% 设置均值滤波处理的半径,半径为1对应3*3均值滤波,半径为2对应5*5均值滤波...
r3 = 1; r5 = 2; r7 = 3; r9 = 4; r11 = 5;
% 建新矩阵用于保存处理后的结果
matrix3 = zeros(h,w);
matrix5 = zeros(h,w);
matrix7 = zeros(h,w);
matrix9 = zeros(h,w);
matrix11 = zeros(h,w);
% 扩展要被处理的图像,留出充足的处理空间
matrix3Plus=zeros(h+2*r3,w+2*r3);
matrix3Plus(r3+1:h+r3,r3+1:w+r3)=GaussainI;
matrix5Plus = zeros(h+2*r5,w+2*r5);
matrix5Plus(r5+1:h+r5,r5+1:w+r5) = GaussainI;
matrix7Plus = zeros(h+2*r7,w+2*r7);
matrix7Plus(r7+1:h+r7,r7+1:w+r7)=GaussainI;
matrix9Plus = zeros(h+2*r9,w+2*r9);
matrix9Plus(r9+1:h+r9,r9+1:w+r9)=GaussainI;
matrix11Plus = zeros(h+2*r11,w+2*r11);
matrix11Plus(r11+1:h+r11,r11+1:w+r11)=GaussainI;
%自编算法实现均值滤波处理
%除开边界,要处理的是2到4
for x=r3+1:h+r3
for y=r3+1:w+r3
new_r=x-r3;new_c=y-r3;value=0;
for i=1:2*r3+1
for j=1:2*r3+1 %每一个被处理的点,处理后的值,为该点高斯函数的值乘以该点周围(3*3)范围的加和
value=value+(matrix3Plus(new_r+i-1,new_c+j-1));
end
end
%将对应位置的结果保存
matrix3(x,y)=value/((r3*2+1)*(r3*2+1));
end
end
for x=r5+1:h+r5
for y=r5+1:w+r5
new_r=x-r5;new_c=y-r5;value=0
for i=1:2*r5+1
for j=1:2*r5+1
value=value+(matrix5Plus(new_r+i-1,new_c+j-1));
end
end
matrix5(x,y)=value/((r5*2+1)*(r5*2+1));
end
end
for x=r5+1:h+r5
for y=r5+1:w+r5
new_r=x-r5;new_c=y-r5;value=0
for i=1:2*r5+1
for j=1:2*r5+1
value=value+(matrix5Plus(new_r+i-1,new_c+j-1));
end
end
matrix5(x,y)=value/((r5*2+1)*(r5*2+1));
end
end
for x=r7+1:h+r7
for y=r7+1:w+r7
new_r=x-r7;new_c=y-r7;value=0
for i=1:2*r7+1
for j=1:2*r7+1
value=value+(matrix7Plus(new_r+i-1,new_c+j-1));
end
end
matrix7(x,y)=value/((r7*2+1)*(r7*2+1));
end
end
for x=r9+1:h+r9
for y=r9+1:w+r9
new_r=x-r9;new_c=y-r9;value=0
for i=1:2*r9+1
for j=1:2*r9+1
value=value+(matrix9Plus(new_r+i-1,new_c+j-1));
end
end
matrix9(x,y)=value/((r9*2+1)*(r9*2+1));
end
end
for x=r11+1:h+r11
for y=r11+1:w+r11
new_r=x-r11;new_c=y-r11;value=0
for i=1:2*r11+1
for j=1:2*r11+1
value=value+(matrix11Plus(new_r+i-1,new_c+j-1));
end
end
matrix11(x,y)=value/((r11*2+1)*(r11*2+1));
end
end
%展示结果
subplot(3,3,1);imshow(Image);title('原图');
subplot(3,3,2);imshow(GaussainI);title('添加高斯噪声后');
subplot(3,3,3);imshow(b3);title('matlab自带均值滤波3*3滤波处理后');
subplot(3,3,4);imshow(uint8(matrix3));title('自编3*3均值滤波处理后');
subplot(3,3,5);imshow(uint8(matrix5));title('自编5*5均值滤波处理后');
subplot(3,3,6);imshow(uint8(matrix7));title('自编7*7均值滤波处理后');
subplot(3,3,7);imshow(uint8(matrix9));title('自编9*9均值滤波处理后');
subplot(3,3,8);imshow(uint8(matrix11));title('自编11*11均值滤波处理后');