matlab练习程序(图像加噪,各种滤波)

cl;

img=imread('3.17.tif');
imgn=imnoise(img,'salt & pepper',0.02);
[m n]=size(img);

%h=fspecial('average',[3 3]); % 3*3的均值滤波
%imgn1=imfilter(imgn,h,'replicate');

%imgn=medfilt2(imgn,[3 3]); % 3*3的中值滤波

%%%%自己编写3*3均值滤波
%{
tem=0;
for i=2:1:m-1
for j=2:1:n-1

for p=i-1:1:i+1
for q=j-1:1:j+1
tem=tem+double(imgn(p,q));
end
end

tem=tem/9;
imgn(i,j)=tem;
tem=0;
end
end
%
}

%%%%自己编写3*3中值滤波
%{
s=zeros(1,9);
for i=2:1:m-1
for j=2:1:n-1

he=1;
for p=i-1:1:i+1
for q=j-1:1:j+1
s(he)=imgn(p,q);
he=he+1;
end
end

s=sort(s);
imgn(i,j)=s(5);

end
end
%
}
imshow(imgn)

你可能感兴趣的:(matlab)