Image denoising using median of pixels

  • Follow


hello i ve to remove noise from gray scale image by using median of pixels.first a pixel is checked i.e.whether it is corrupted or not, if it is corrupted then using 3*3 mask and padding calculate the median and replace corrupted pixel.after running following code I got the error which shows that loop goes out of bound.

I=imread('eight.tif');
subplot(1,3,1);
imshow(I);
title('original image');
J=imnoise(I,'salt & pepper',0.10);
subplot(1,3,2);
imshow(J);
title('noisy image');
D=double(J);
[x,y]=size(D);
xx=x+2;
yy=y+2;
med(3)=0;
n=zeros(3);
n1=zeros(xx,yy);
%n1(2:x+1,2:y+1) =D 
for i=2:xx-1;
    for j=2:yy-1;
        if(0>D(i,j)<255)
for k=i:i+2;
    for l=j:j+2;
        n(k,l)=D(k,l);
    end
end
O=median(n);

n1(i,j)=O(1);
        end
    end
end
A=uint8(n1);
    subplot(1,3,3);
       imshow(A);
       title('filtered image');
0
Reply arti 12/26/2010 2:40:22 PM

arti:
You don't want to do it that way - with loops - it's very slow and
inefficient.  Try it this vectorized way.  Basically it calculated the
median filtered image of the *whole* image.  Then it finds locations
where you have salt and pepper noise at 0 or 255.  Then it replaces
the noisy image pixels with the median filtered image pixels ONLY at
those locations where there is noise.  Non-noise pixels remain
unaffected (they don't get filtered by the median filter).
ImageAnalyst

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% by ImageAnalyst

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
	cd(fileparts(which(mfilename)));
end

% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos';
baseFileName = 'moon.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
		uiwait(warndlg(errorMessage));
		return;
	end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Get a noisy version of it.
noisyImage = imnoise(grayImage, 'salt & pepper', 0.10);
% Display the noisy image.
subplot(2, 2, 2);
imshow(noisyImage, []);
title('Noisy Image', 'FontSize', fontSize);

% Get the median filter of it.
medianFilteredImage = medfilt2(noisyImage, [3 3]);
% Display the median filtered image.
subplot(2, 2, 3);
imshow(medianFilteredImage, []);
title('Median Filtered Image', 'FontSize', fontSize);

% Find out the the values where it = 255 or 0
noisePixels = (noisyImage == 255) | (noisyImage == 0);

% Replace noisy pixels with median filtered pixels
% only for the noise pixels alone, not all pixels.
fixedImage = noisyImage; % Initialize
fixedImage(noisePixels) = medianFilteredImage(noisePixels); % Repair
% Display the noisy image.
subplot(2, 2, 4);
imshow(fixedImage, []);
title('Repaired Image', 'FontSize', fontSize);

0
Reply ImageAnalyst 12/26/2010 4:17:21 PM


thnk u sir for ur guidance.
but after filtering i am not getting original image, it shws some noise on the edges...
just change the image (having light backgroud)then you will get it.I tried it using noise density .10 with mask 3*3.I also want to know how to calculate PSNR for the image for different noise densities.
0
Reply arti 12/27/2010 2:52:04 PM

arti gautam:
Due to the random nature of the noise, some will sneak through.  You can enlarge your window size, from 3x3 to 7x7, to try to eliminate more of it.

The formula for PSNR is at
http://en.wikipedia.org/wiki/PSNR
0
Reply Image 12/27/2010 3:45:21 PM

3 Replies
534 Views

(page loaded in 0.07 seconds)

Similiar Articles:













7/24/2012 4:54:52 AM


Reply: