# Image Noise Detection Using MATLAB

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.

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

A=imread('npan3.jpg');

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)
sub=vec2mat(sub,3);
[row col]=size(sub);
%siz=size(sub)
s=std2(sub);
m=mean2(sub);
t1=m-s;
t2=m+s;
nd=zeros(row,col);

if (sub(2,2)>=t1 && sub(2,2)<=t2)
nd=sub;
else
nd=wiener2(sub);
end


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.

• Migration of answered questions is generally frowned upon: meta.stackexchange.com/a/10250/131640 – Peter K. Mar 25 '14 at 17:51
• How to migrated? – Premnath D Mar 25 '14 at 18:03
• Read the link in my previous comment. – Peter K. Mar 25 '14 at 18:06
• Could you please review the answer? – Royi Oct 5 '20 at 15:11

Since applying the Wiener filter is pretty "cheap" I would create an Image called mWienerFilter.
vNoisePixels = (abs(I -  mMeanImage) > noiseThr);