how i can implement these algorithm in matlab

2 views (last 30 days)
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
Image Analyst
Image Analyst on 14 Oct 2017
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);
aliah aljohani
aliah aljohani on 14 Oct 2017
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!