%%自写滤波
I = double(imread('Fig3.35(a).jpg'));
[m,n] = size(I);
A3 = zeros(m,n);
A5 = zeros(m,n);
for i = 3:m-3
for j = 3:n-3
A3(i,j) = ( I(i-1,j-1)+I(i-1,j)+I(i-1,j+1)+I(i,j-1)+I(i,j)+I(i,j+1)+I(i+1,j-1)+I(i+1,j)+I(i+1,j+1))/9;
A5(i,j)= ( I(i-2,j-2)+I(i-2,j-1)+I(i-2,j)+I(i-2,j+1)+I(i-2,j+2)+...
I(i-1,j-2)+I(i-1,j-1)+I(i-1,j)+I(i-1,j+1)+I(i-1,j+2)+...
I(i,j-2)+I(i,j-1)+I(i,j)+I(i,j+1)+I(i,j+2)+...
I(i+1,j-2)+I(i+1,j-1)+I(i+1,j)+I(i+1,j+1)+I(i+1,j+2)+...
I(i+2,j-2)+I(i+2,j-1)+I(i+2,j)+I(i+2,j+1)+I(i+2,j+2))/25;
end
end
subplot(1,3,1),imshow(uint8(I));title('原图像');
subplot(1,3,2),imshow(uint8(A3));title('3X3均值滤波器');
subplot(1,3,3),imshow(uint8(A5));title('5X5均值滤波器');`
%%系统滤波
I = double(imread('Fig3.35(a).jpg'));
[m,n] = size(I);
A3 = zeros(m,n);
A5 = zeros(m,n);
H1 = fspecial('average',[3,3]);
H2 = fspecial('average',[5,5]);
A3 = imfilter(I,H1);
A5 = imfilter(I,H2);
subplot(1,3,1),imshow(uint8(I));title('原图像');
subplot(1,3,2),imshow(uint8(A3));title('3X3均值滤波器');
subplot(1,3,3),imshow(uint8(A5));title('5X5均值滤波器');
题目:均值滤波和中值滤波 在自己的证件照中加入椒盐噪声、高斯白噪声。
分别用33、55、7*7的均值滤波器和中值滤波器进行滤波。
处理过程
%%均值滤波
rgb=imread('photo.jpg');
J1=imnoise(rgb,'salt & pepper',0.02); %%添加噪声
J2=imnoise(J1,'gaussian',0,0.01); %%添加噪声
h1=fspecial('average',[3,3]); %%平均滤波算子
h2=fspecial('average',[5,5]);
h3=fspecial('average',[7,7]);
rgb1=imfilter(J2,h1); %% 用算子使用平均滤波
rgb2=imfilter(J2,h2);
rgb3=imfilter(J2,h3); figure;
subplot(2,3,1);imshow(rgb) title('原图像');
subplot(2,3,2);imshow(J2) title('加入噪声后的图像');
subplot(2,3,4);imshow(rgb1) title('3*3均值滤波图像');
subplot(2,3,5);imshow(rgb2) title('5*5均值滤波图像');
subplot(2,3,6);imshow(rgb3) title('7*7均值滤波图像');
%%(2)中值滤波
rgb=imread('photo.jpg');
J1=imnoise(rgb,'salt & pepper',0.02);
J2=imnoise(J1,'gaussian',0,0.01);
J3=rgb2gray(J2);
rgb1=medfilt2(J3,[3 3]);
rgb2=medfilt2(J3,[5 5]);
rgb3=medfilt2(J3,[7 7]);