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 &lt;= I(x,y) and  I(x,y)  &lt;= 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=imread('lena.jpg');
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)&lt;=t1 &amp;&amp; P(2,2)&gt;=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.
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:

http://www.csse.uwa.edu.au/~shafait/papers/Shafait-efficient-binarization-SPIE08.pdf
http://arxiv.org/ftp/arxiv/papers/1201/1201.5227.pdf

Cheers,