以灰階圖像eight.tif為例,向原始圖像中加入椒鹽噪聲,再對噪聲圖像調用中值濾波函數midfilt進行去噪。
I=imread('eight.tif');
J=imnoise(I,'salt & pepper');
after=midfilt(J,);
subplot(,,);
imshow(I);
subplot(,,);
imshow(J);
subplot(,,);
imshow(after);
function d=midfilt(x, n)
[M,N]=size(x);
x1=x;
x2=x1;
for i=:M-n+
for j=:N-n+
c=x1(i:i+n-,j:j+n-);
e=c(,:);
for k=:n
e=[e,c(k,:)];
end
x2(i+(n-)/,j+(n-)/)=median(e);
end
end
d=x2;