%目的:观察各种噪声对图像的影响
I=imread('eight.tif');%读取图像
J1=imnoise(I,'salt & pepper',0.02);%添加椒盐噪声
J2=imnoise(I,'gaussian',0,0.02);%添加高斯噪声
J3=imnoise(I,'speckle',0.02);%添加乘性噪声
subplot(221);imshow(I);title('源图像')
subplot(222);imshow(J1);title('椒盐噪声')
subplot(223);imshow(J2);title('高斯噪声')
subplot(224);imshow(J3);title('乘性噪声')
%目的:椒盐噪声中值滤波
[I,map]=imread('eight.tif');
noisy=imnoise(I,'salt & pepper',0.05);
subplot(221);imshow(noisy);title('噪声图像');
hood=3;%模板大小
filtered1=medfilt2(noisy,[hood hood]);
subplot(222);imshow(filtered1,map);title('3*3中值滤波图像');
hood=5;
filtered2=medfilt2(noisy,[hood hood]);
subplot(223);imshow(filtered2,map);title('5*5中值滤波图像');
hood=7;
filtered3=medfilt2(noisy,[hood hood]);
subplot(224);imshow(filtered2,map);title('7*7中值滤波图像');
%目的:椒盐噪声均值滤波
[I,map]=imread('eight.tif');
noisy=imnoise(I,'salt & pepper',0.05);
subplot(221);imshow(I);title('原始图像');
subplot(222);imshow(noisy);title('噪声图像');
myfilt1=[0 1 0;1 1 1;0 1 0];%模板
myfilt1=myfilt1/9;
filtered1=filter2(myfilt1,noisy);
subplot(223);imshow(filtered1,map);title('4方向均值滤波图像');
myfilt2=[1 1 1;1 1 1;1 1 1];
myfilt2=myfilt2/9;
filtered2=filter2(myfilt2,noisy);
subplot(224);imshow(filtered2,map);title('8方向均值滤波图像');
%目的:高斯噪声均值滤波
[I,map]=imread('pout.tif');
noisy=imnoise(I,'gaussian',0.05);
subplot(221);imshow(I);title('高斯噪声图像');
myfilt1=[1 1 1;1 1 1;1 1 1];
myfilt1=myfilt1/9;
filtered1=filter2(myfilt1,noisy);
subplot(222);imshow(filtered1,map);title('高斯噪声均值滤波');
myfilt2=[1 1 1;1 2 1;1 1 1];
myfilt2=myfilt1/10;
filtered2=filter2(myfilt2,noisy);
subplot(223);imshow(filtered2,map);title('高斯噪声加权均值滤波');
myfilt3=[1 2 1;2 4 2;1 2 1];
myfilt3=myfilt1/16;
filtered3=filter2(myfilt3,noisy);
subplot(224);imshow(filtered3,map);title('高斯噪声加权均值滤波');
需要:按照理论公式完成卷积
%按照理论公式法一:
a=[4 5 6 7 8 9];
b=[ 7 5 8 ];
k=length(a);
bk=length(b);
n=length(a)+length(b)-1;
a=[a zeros(1,n-k)];
b=[b zeros(1,n-bk)];
y=zeros(1,n);
for i=1:n
temp=0;
for j=1:n
if i>=j
temp=temp+a(j).*b(i+1-j)
else
break;
end
end
y(i)=temp
end
y
subplot(2,1,1)
stem(y)
%按照理论公式法二:
for i=1:n
j=max(1,i+1-bk):min(i,k);
y3(i)=sum(a(j).*b(i+1-j));
end
y3
subplot(3,1,3)
stem(y3)
%利用conv函数
a=[4 5 6 7 8 9];
b=[ 7 5 8 ];
y2=conv(a,b)
subplot(2,1,2)
stem(y2)