To remove the noise using Median Filtering.

5 views (last 30 days)
This is my code for median filtering..i have 5 image in "class2" folder. Am able to display only the 1st image of original & noise image. Its not displaying the resorted image, as well the other remaining 4 images..can anyone help me out to correct the error..
myFolder='E:\MRP\accuracy\class2';
m=input('Type the Number of Images to Process:');
for k = 1:m
jpgFilename = sprintf('%d.jpg', k);
fullFileName = fullfile(myFolder, jpgFilename);
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original image.
figure,imshow(grayImage);
title('Original Gray Scale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Generate a noisy image with salt and pepper noise.
noisyImage = imnoise(grayImage,'salt & pepper', 0.05);
figure,imshow(noisyImage);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Median Filter the image:
medianFilteredImage = medfilt2(noisyImage, [3 3]);
% Find the noise. It will have a gray level of either 0 or 255.
noiseImage = (noisyImage == 0 | noisyImage == 255);
% Get rid of the noise by replacing with median.
noiseFreeImage = noisyImage; % Initialize
noiseFreeImage(noiseImage) = medianFilteredImage(noiseImage); % Replace.
% Display the image.
figure,imshow(noiseFreeImage);
title('Restored Image', 'FontSize', fontSize);

Accepted Answer

kash
kash on 1 Mar 2013
Edited: kash on 1 Mar 2013
EDITED
clc
clear all
myFolder='E:\MRP\accuracy\class2'
m=input('Type the Number of Images to Process:');
for k = 1:m
jpgFilename = sprintf('%d.jpg', k);
fullFileName = fullfile(myFolder, jpgFilename);
% end
grayImage = imread(fullFileName))
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands==3
grayImage=rgb2gray(grayImage);
else
grayImage=grayImage;
end
% Display the original image.
figure('name','grayImage','numbertitle','off');imshow(grayImage);
fontSize=10
% Enlarge figure to full screen.
% Generate a noisy image with salt and pepper noise.
noisyImage = imnoise(grayImage,'salt & pepper', 0.05);
figure('name','noisyImage','numbertitle','off');imshow(noisyImage);
% Median Filter the image:
medianFilteredImage = medfilt2(noisyImage, [3 3]);
% Find the noise. It will have a gray level of either 0 or 255.
noiseImage = (noisyImage == 0 | noisyImage == 255);
% Get rid of the noise by replacing with median.
noiseFreeImage = noisyImage; % Initialize
noiseFreeImage(noiseImage) = medianFilteredImage(noiseImage); % Replace.
% Display the image.
figure('name','Restored Image','numbertitle','off');imshow(noiseFreeImage);
% figure,imshow(noiseFreeImage);
% title('Restored Image', 'FontSize', fontSize);
end
  6 Comments
sudha
sudha on 1 Mar 2013
ya i got it..thank you..
If i want to apply on RGB images then wat to do???
kash
kash on 1 Mar 2013
Accept the answer if it is correct and open a new forum,because RGB is different from gray scale filtering

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!