I want to implement the following algorithm. Let $I$ denote noisy image and for each pixel of $I$ is represented by $I(x,y)$. A Sliding window of size $3\times3$ 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 weiner Filter.


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)
        Result(x, y) = weiner2(I(x,y)

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

First, I tried to implement this algorithm for median filter, so that I can extend it to other filters such as wiener, anisotropic diffusion,butterworth etc. I did implement it correctly with the help of 'tbirdal'. Thank you 'tbirdal'. But I am unable to extend the implementation for other filters.

**I don't want to use loop.. Because it takes more time to run .. 20 mins to run a $512 \times 512$ image.. I want to implement this using functions such as nlfilter, imfilter etc ** Program:


fun = @(x) noisedetect_2(x(:));
B = nlfilter(A,[3 3],fun);

imshow(A), figure, imshow(B, [])

The noisedetect_2 function is given as

function nd=noisedetect_2(sub)
[row col]=size(sub);

if (sub(2,2)>=t1 && sub(2,2)<=t2)

I get the following error:

Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Error in nlfilter (line 75)
        b(i,j) = feval(fun,x,params{:});

Error in nd_test_2 (line 10)
B = nlfilter(A,[3 3],fun);

I understand what the error means. the nlfilter is expecting single value but the function returns $3\times3$ matrix. How to solve this issue?. I just want to apply any filter only when the pixel falls out of the range $t_1$ and $t_2$ because it is considered as noisy pixel. Otherwise, it is retained as such because it is considered as noisefree pixel.

Please help me implement the algorithm.

  • $\begingroup$ Migration of answered questions is generally frowned upon: meta.stackexchange.com/a/10250/131640 $\endgroup$
    – Peter K.
    Mar 25, 2014 at 17:51
  • $\begingroup$ How to migrated? $\endgroup$
    – Premnath D
    Mar 25, 2014 at 18:03
  • $\begingroup$ Read the link in my previous comment. $\endgroup$
    – Peter K.
    Mar 25, 2014 at 18:06
  • $\begingroup$ @PremnathD, Could you please review my answer? If it fits, please mark it. $\endgroup$
    – Royi
    Jun 1, 2022 at 9:01

1 Answer 1


I would implement it differently altogether.
Since applying the Wiener filter is pretty "cheap" I would create an Image called mWienerFilter.

vNoisePixels = (abs(I -  mMeanImage) > noiseThr);
I(vNoisePixels) = mWienerFilter(vNoisePixels);

That would be the best thing to do in my opinion.


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.