1
$\begingroup$

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=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)<=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.

$\endgroup$

2 Answers 2

0
$\begingroup$

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(:))

$\endgroup$
11
  • $\begingroup$ 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(:)) $\endgroup$
    – Premnath D
    Mar 16, 2014 at 13:18
  • $\begingroup$ I edited my answer. Read it again. $\endgroup$ Mar 16, 2014 at 13:23
  • $\begingroup$ 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 $\endgroup$
    – Premnath D
    Mar 16, 2014 at 13:29
  • $\begingroup$ You can just use median and get the code working. $\endgroup$ Mar 16, 2014 at 13:30
  • $\begingroup$ k.. It is running .. Taking too much time.. Can we implement this using blockproc ? $\endgroup$
    – Premnath D
    Mar 16, 2014 at 13:32
1
$\begingroup$

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,

$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.