Sie sind auf Seite 1von 1

clc;

clear all;
close all;
f=imread('cameraman.tif');
g=imnoise(f,'salt & pepper', 0.01);
smax=1;
%obtain width and height of image g
[M, N] = size(g);
% Initial setup
f = g;
f(:) = 0;
alreadyProcessed = false(size(g));
% Begin filtering
%increase size of filter window from 3 to Smax by 2
for k = 3:2:1
%minimum filtering ?maximum filtering and median filtering
zmin = ordfilt2(g, 1, ones(k, k), 'symmetric');
zmax = ordfilt2(g, k * k, ones(k, k), 'symmetric');
zmed = medfilt2(g, [k k], 'symmetric');
%check if zmed is the impulse noise:
%if no,processUsingLevelB=true,and go to Process Level B;vice versa
processUsingLevelB = (zmed > zmin) & (zmax > zmed) & ~alreadyProcessed;
%check if g is the impulse noise:
%if no,zB=true,and go to Process Level B;vice versa
zB = (g > zmin) & (zmax > g);
% if processUsingLevelB and zB are both ture,Zxy is output
outputZxy = processUsingLevelB & zB;
% if processUsingLevelB is ture and zB is false,Zmed is output
outputZmed = processUsingLevelB & ~zB;
%set Zxy to output
f(outputZxy) = g(outputZxy);
%set Zmed to output
f(outputZmed) = zmed(outputZmed);
%set true pixel already processed,namely relative ouput is already set
alreadyProcessed = alreadyProcessed | processUsingLevelB;
% if all pixel are processed and the outputs are obtained ,filtering is
%completed
if all(alreadyProcessed(:))
break;
end
end
% Output zmed for any remaining unprocessed pixels. Note that this
% zmed was computed using a window of size Smax-by-Smax, which is
% the final value of k in the loop.
f(~alreadyProcessed) = zmed(~alreadyProcessed);
imshow(zmed);

Das könnte Ihnen auch gefallen