MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by ARUN SAI
on 26 May 2013

matlab code to find existence of noise and edges in a color image using gradient?

Answer by Image Analyst
on 26 May 2013

Try subtracting the image from your noise-free version to get the noise. Then, have you tried to use imgradient? Or you could try a local delta E (color difference) as given in my demo code: http://www.mathworks.com/matlabcentral/answers/73741#comment_145951

ARUN SAI
on 26 May 2013

i have tried this code for finding the existence of noise and edges in a color image named as mandi.tif which is cfa raw image in the matlab library.

for i=2:1:m-1

for j=2:1:n-1

**% horizontal internal gradient**

A1=abs(r(i-1,j-1)-r(i+1,j+1));

**% vertical internal gradient**

B1=abs(r(i-1,j+1)-r(i+1,j-1));

end

end

for i=3:1:m-2

for j=3:1:n-2

**% horizontal external gradient**

A2=abs(2*g(i,j)-g(i-1,j-2)-g(i+1,j+2));

**% vertical external gradient**

B2=abs(2*g(i,j)-g(i-2,j-1)-g(i+2,j+1));

end end

**% to find existence of edge or influence of the noise TH (enumeration variable) is used for noise that in the up and down or in any point of g(i,j)**

for k=2:2:m-1

for l=3:2:n-1

x=[k-1,k+1];

y=[l-1,l+1];

**%up says that g(i,j-1) is the noise**

up=(A1<B1)&(eq(A2,B2))&eq(max(abs(2*r(k,y)-r(k-1,l)-r(k+1,l))),l-1);

**%down says that g(i,j+1) is the noise**

down=(A1<B1)&(eq(A2,B2))&eq(max(abs(2*r(k,y)-r(k-1,l)-r(k+1,l))),l+1);

**%left says that g(i-1,j) is the noise**

left=(A1>B1)&(eq(A2,B2))&eq(max(abs(2*r(x,l)-r(k,l-1)-r(k,l+1))),k-1);

**%right says that g(i+1,j) is the noise**

right=(A1>B1)&(eq(A2,B2))&eq(max(abs(2*r(x,l)-r(k,l-1)-r(k,l+1))),k+1);

*%no says that there is no noise and also no edge *

no=(eq(A1,B1))&(eq(A2,B2));

**% level says that there is a edge in vertical direction**

level=(A1>B1)&(A2>B2);

**% erect says that there is a edge in horizontal direction**

erect=(A1>B1)&(A2<B2);

end

end

p=[1 2 3 4 5 6 7]

TH=[up down left right no level erect]

p_TH=p(TH==1)

for i=2:1:m-1

for j=2:1:n-1

**% switches according to the TH value**
switch p_TH

case 1 r(i,j)=(r(i-1,j)+r(i+1,j)+r(i,j+1))/3; r(i,j-1)=r(i,j);

case 2 r(i,j)=(r(i-1,j)+r(i+1,j)+r(i,j-1))/3; r(i,j+1)=r(i,j);

case 3 r(i,j)=(r(i+1,j)+r(i,j-1)+r(i,j+1))/3; r(i-1,j)=r(i,j);

case 4 r(i,j)=(r(i-1,j)+r(i,j-1)+r(i,j+1))/3; r(i+1,j)=r(i,j);

case 5 r(i,j)=(r(i-1,j)+r(i+1,j)+r(i,j-1)+r(i,j+1))/4;

case 6 r(i,j)=(r(i-1,j)+r(i+1,j))/2;

case 7 r(i,j)=(r(i,j-1)+r(i,j+1))/2;

end

end

end

s=r+g+b;

figure,imshow(s);

**%signal to noise ratio calculation of image after removal of noise**

m3=mean2(s);

SD3=mean2(stdfilt(s));

snr3=(m3/SD3)

## 0 Comments