Adaptive median filter

W
Wildemann · Dec 6, 2021 · 2.2K views
Question
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  
Related Questions
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Aug 14, 2026
You need to take the contents of the "if" block and make it a function that takes the center location and the window size as input arguments. Then in the "else" block you need to increase the window size and call the function. Something like
 
 
% 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)

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!