Image Noise Detection in MATLAB

I am getting error while implementing a simple idea for noise detection in an image. Let I denote noisy image and for each pixel of I is represented by I(x,y). A Sliding window of size 3X3 centered at I(x, y) is defined. At first we estimate that the center pixel of window is noise or not. To differ the noise and signal, we calculate the mean (m) and standard deviation (s) of the window. If the pixel is between m-s and m+s then it is a signal otherwise noise and we will apply median Filter.

Algorithm:

1.  Read an Image I.
2.  Take a Sliding Window or Mask of size 3X3.
3.  Calculate the Mean (m) and Standard deviation (s) from the Mask.
4.  Calculate the threshold as follows
t1 = m-s and t2 = m+s

5.  IF     t1 <= I(x,y) and  I(x,y)  <= t2 THEN

Result(x,y) = I(x,y)
ELSE
Result(x, y) = medfilt2(I(x,y)

6.  Repeat the step 3 , 4 and 5 on Entire Image.
7.  Display the Resultant Image.


I tried to implement this algorithm as follows

clc;
close all;
clear all;

I=rgb2gray(I);
J=imresize(I, [128 128]);
L = imnoise(J,'speckle',0.01);

[m,n]=size(L);
% RETAINING THE CORNER PIXELS/ we can do zero padding also, suggest me which one is good

for i= 1:m+4
for j = 1:n+4
L(:,1:2)=J(:,1:2);         % retain the first two columns
L(:,(m-1):m)=J(:,(m-1):m); % retain the last two columns
L(1:2,:)=J(1:2,:);         % retain the first two rows
L((n-1):n,:)=J((n-1):n,:); % retain the last two rows
end
end

for i=1:m-2
for j=1:n-2

P=L(i:(i+2),j:(j+2));
s=std2(P(:)); % Calculating Standard deviation
m=mean2(P(:)); % Calulating Mean
t1=m-s; % Threshold 1
t2=m+s; % Threshold 2

if (P(2,2)<=t1 && P(2,2)>=t2)
iout(i,j)=medfilt2(P(:))
else
iout(i,j)=P(2,2)
end

end
end
L
iout
imshow(iout, []);
imshowpair(L,iout,'montage');


It runs but there is no change between input and output images.. Image is not filtered. Please help me.

1) This condition can never be satisfied: if (P(2,2)<=t1 && P(2,2)>=t2).

It should be if (P(2,2)>=t1 && P(2,2)<=t2)

2) You haven't created iout. You should be creating it with: iout=zeros(m,n) at least before the beginning of second loop.

3) medfilt2 performs a median filtering. What you would like to write is median(P(:)).

4) This is not an error but for use with std2 and mean2 you can just use std2(P), you don't need to std2(P(:))

• I updated the code and got this error, Assignment has more non-singleton rhs dimensions than non-singleton subscripts Error in n1 (line 36) iout(x,y)=medfilt2(P(:)) – Premnath D Mar 16 '14 at 13:18
• I edited my answer. Read it again. – Tolga Birdal Mar 16 '14 at 13:23
• Ok..thank you.. i will try.. what to do if I want to apply different filter other than median by detecting noisy pixel this way – Premnath D Mar 16 '14 at 13:29
• You can just use median and get the code working. – Tolga Birdal Mar 16 '14 at 13:30
• k.. It is running .. Taking too much time.. Can we implement this using blockproc ? – Premnath D Mar 16 '14 at 13:32

If you would like to do this faster, what you could try is the neighborhood operations. Take for example the sample code below:

 I = imread('tire.tif'); f = @(x) max(x(:)); I2 = nlfilter(I,[3 3],f); imshow(I); figure, imshow(I2); 

This applies a maximum filtering. You could consider changing your neighborhood function f to your custom algorithm (inside your loop) and this should give you a better speed.

Of course there are a lot of ways of getting speed. Use of integral images, moving averages and precomputations might save you from the burden of heavy block processing. But this would be way a lot to write in here. For more details check out these papers:

Cheers,