clc; clear all; close all; a=imread('cameraman.tif'); figure(1),imshow(a),title('origanal image'); b=imnoise(a,'salt & pepper',.02) figure(2),imshow(b),title('noisy image'); Smax=9; for i=1:254 for j=1:254 n=b(i:i+2,j:j+2) Zmin=min(n(:)); Zmax=max(n(:)); Zmed=median(n(:)); sx=3; sy=3; A1=Zmed-Zmin; A2=Zmed-Zmax; if (A1>0) && (A2<0) B1 = Zxy-Zmin; B2 = Zxy-Zmax; if (B1>0) && (B2<0) b(i:i+2,j:j+2) = n(i, j); break; else b(i:i+2,j:j+2) = Zmed; break; end else sx=sx+2; sy=sy+2; if (sx > Smax) && (sy > Smax) b(i:i+2,j:j+2) = n(i, j); end end end end figure(3),imshow(b),title('denoised image'); i am trying to implement the following algorithm but where the above pro. is wrong i am unable to find .. please help me....... Adaptive median filter changes size of Sxy (the size of the neighborhood) during operation. ? Notation Zmin = minimum gray level value in Sxy Zmax = maximum gray level value in Sxy Zmed = median of gray levels in Sxy Zxy = gray level at coordinates (x, y) Smax = maximum allowed size of Sxy ? Algorithm Level A: A1 = Zmed - Zmin A2 = Zmed - Zmax if A1 > 0 AND A2 < 0, go to level B else increase the window size if window size < Smax, repeat level A else output Zxy Level B: B1 = Zxy - Zmin B2 = Zxy - Zmax if B1 > 0 AND B2 < 0, output Zxy else output Zmed
John Williams answered .
2025-11-20
% Determine window size. if (A1>0) && (A2<0) windowSize = standardSize; % 3 or whatever else windowSize = % your formula for bigger window end % Make the assignment. newImage(x,y) = GetMedian(x,y,windowSize)