自适应中值滤波器设计与实现(matlab)

clc;
clear;
[filename,pathname]=uigetfile({'*.jpg;*.bmp;*.tif;*.png;*.gif','All Image Files';'*.*','All Files'});
f = imread([pathname,filename]);
image_gray=rgb2gray(f);%得到灰度图像
f1=imnoise(image_gray,'salt & pepper',0.3);%添加椒盐噪声后的图像
ff =image_gray;
alreadyProcessed = false(size(image_gray));%生成逻辑非的矩阵
% 迭代.
Smax=7;
for k = 3:2:Smax      %步长为2的循环(k的3,5,7,10)
   zmin = ordfilt2(f1, 1, ones(k, k), 'symmetric');
   zmax = ordfilt2(f1, k * k, ones(k, k), 'symmetric');
   zmed = medfilt2(f1, [k k], 'symmetric');
   
   processUsingLevelB = (zmed > zmin) & (zmax > zmed) & ~alreadyProcessed; 
   zB = (f1 > zmin) & (zmax > f1);
   outputZxy  = processUsingLevelB & zB;
   outputZmed = processUsingLevelB & ~zB;
   ff(outputZxy) = f1(outputZxy);
   ff(outputZmed) = zmed(outputZmed);
   
   alreadyProcessed = alreadyProcessed | processUsingLevelB;
   if all(alreadyProcessed(:))
      break;
   end
end
ff(~alreadyProcessed) = zmed(~alreadyProcessed);

f2=medfilt2(f1,[3,3]);%中值滤波后的图像
subplot(2,2,1);
imshow(image_gray);
title('原图');
subplot(2,2,2);
imshow(f1);
title('椒盐噪声污染后的图像');
subplot(2,2,3);
imshow(f2);
title('中值滤波');
subplot(2,2,4);
imshow(ff);
title('自适应中值滤波');

你可能感兴趣的:(matlab)