how i can implement these algorithm in matlab

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

 Accepted Answer

Image Analyst
Image Analyst on 13 Oct 2017
Edited: Image Analyst on 13 Oct 2017
I use an adaptive median filter to do salt and pepper removal. You can easily adapt it to change the values from 0 and 255 to Zmin and Zmax. Let me know how it goes. Attach your adapted code if you run into problems.

9 Comments

thank you but these code for median filter i need one for adaptive filter i have code but not works
can you see
Oh, you didn't say that at first. But unfortunately, you forgot to attach your m-file and 'shyamgs.bmp'. I'll wait until then, since I can't fix your code unless I have your code.
Also explain what this extra edge processing does. How does that make the image better than a regular modified median filter?
Can you ask the author to add more comments? Like explain why there are weird lines in there like this:
ip_edge(i,j) = ip_edge(i,j);
how i can change the values from 0 and 255 to Zmin and Zmax.
in your cod
i am sorry I do not know how i can do these
Replace this line
% Find the noise. It will have a gray level of either 0 or 255.
noiseImage = (noisyImage == 0 | noisyImage == 255);
with this line:
% Find the noise. It will have a gray level of either less than Zmin or more than Zmax.
noiseImage = (noisyImage <= Zmin | noisyImage >= Zmax);
Again, attach your image and m-file.
Exactly what does "code but not works" mean? And I mean EXACTLY.
i mean these code not work it has some error at for ,if and end and i do not know how i can fixed these error
This gets rid of the error, so that now you can continue to figure out why your code doesn't get rid of salt and pepper noise like my code does.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('h1.jpg');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
subplot(2,2,1);
imshow(grayImage)
title('Original image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
% Add noise.
noiseFreeImage = imnoise(grayImage,'salt & pepper',.02);
subplot(2,2,2);
imshow(noiseFreeImage);
title('noisy image', 'FontSize', fontSize);
drawnow;
% Make failed attempt to denoise the image.
Smax = 9;
for col = 1:columns-2
for row = 1:rows-2
n = noiseFreeImage(row:row+2,col:col+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)
noiseFreeImage(row:row+2,col:col+2) = n(row, col);
break;
else
noiseFreeImage(row:row+2,col:col+2) = Zmed;
break;
end
else
sx = sx+2;
sy = sy+2;
if (sx > Smax) && (sy > Smax)
noiseFreeImage(row:row+2,col:col+2) = n(row, col);
end
end
end
end
subplot(2,2,3);
imshow(noiseFreeImage)
title('Denoised image', 'FontSize', fontSize);
Yes No errors found in the code Thank you very much for helping me

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!