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 23 May 2013

As iam doing my project on color filtering method for CFA images based on gradient,i have a problem with my code written so guide me if there is any changes in the code.Even though the noise is added to the image ,but it is still showing that there is noise according to the code.

i = imread('mandi.tif');

imshow(i);

**%demosaic of image**

rgb = demosaic(i,'bggr'); *%raw image is converted into true color image*

figure,imshow(rgb);

m1=mean2(rgb);

SD1=mean2(stdfilt(rgb));

snr1=(m1/SD1) *%signal to noise ratio calculation of noise free image*

for i=500:1:1000

for j=500:1:1000

rgb(i,j)=imnoise(rgb(i,j),'gaussian',9); *%noise addition*

end

end figure,imshow(rgb);

m2=mean2(rgb);

SD2=mean2(stdfilt(rgb));

snr2=(m2/SD2) *%signal to noise ratio calculation of noisy image*

r=rgb;g=rgb;b=rgb;

r(:,:,2)=0; r(:,:,3)=0;

g(:,:,1)=0; g(:,:,3)=0;

b(:,:,1)=0; b(:,:,2)=0;

m = size(rgb,1) n = size(rgb,2)

for i=2:1:m-1

for j=2:1:n-1

A1=abs(r(i-1,j-1)-r(i+1,j+1)); *% horizontal internal gradient*

B1=abs(r(i-1,j+1)-r(i+1,j-1)); *% vertical internal gradient*

end

end

for i=3:1:m-2

for j=3:1:n-2

A2=abs(2*g(i,j)-g(i-1,j-2)-g(i+1,j+2)); *% horizontal external gradient*

B2=abs(2*g(i,j)-g(i-2,j-1)-g(i+2,j+1)); *% vertical external gradient*

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=(A1<B1) & (eq(A2,B2)) & eq(max(abs(2*r(k,y)-r(k-1,l)-r(k+1,l))),l-1); *%up 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); *%down says that g(i,j+1) 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); %left 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); *%right says that g(i+1,j) is the noise*

no=(eq(A1,B1)) & (eq(A2,B2)); *%no says that there is no noise and also no edge *

level=(A1>B1) & (A2>B2); *% level says that there is a edge in vertical direction*

erect=(A1>B1) & (A2<B2); *% erect says that there is a edge in horizontal direction*

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);

m3=mean2(s);

SD3=mean2(stdfilt(s));

snr3=(m3/SD3) *%signal to noise ratio calculation of image after removal of noise*

Answer by Image Analyst
on 25 May 2013

Explain why you are doing this to a gray scale image:

%demosaic of image rgb = demosaic(i,'bggr');

ARUN SAI
on 25 May 2013

my project *color filtering method for CFA images based on gradient *is about the process of removing the noise caused during demosaicking or artifacts caused during image acquisition.So in a single sensor digital camera,the sensor can sense only one of the color ie, R or G or B and the remaining colors are predicted through demosaic function.The demosaic function converts raw image into true color image.

Image Analyst
on 25 May 2013

OK - I didn't realize mandi was a raw image until I zoomed in on it. But the lack of comments make it difficult to follow what you are doing and don't really make me want to try. Add comments to explain each step.

Nonetheless I tried to run it and it put up two images then waited a long time and put up a third image and then took many many minutes before I killed it. How long is it supposed to take to finish?

For example, why on earth are you adding noise in a loop one pixel at a time?

%noise addition for i=500:1:1000 for j=500:1:1000 rgb(i,j)=imnoise(rgb(i,j),'gaussian',9); end end

Just do it for the whole 501x501 matrix all in one shot. It will be so much faster.

Answer by Jan Simon
on 26 May 2013

Edited by Jan Simon
on 26 May 2013

I suggest (as usual) to omit the brute clearing header:

clc; clear all; close all;

Closing all windows might be convenient in some rare situation, but as soon as you will use several different programs with GUIs, such habits will make you live much harder.

Clearing the command line let warning and error messages disappear. Is this useful?

`clear all` removes all loaded functions from the memory and reloading them from the hard disk wastes a lot of time. But even worse, it delete all debugger breakpoints, and anything, which impedes debugging is a bad programming pattern. Sometimes `clear` might help to find bugs, because it cleans up formerly defined variables. E.g. if the variable "iO" (i Oh) was defined earlier, then in a script "i0" (i Zero) is defined, but "iO" (i Oh) used due to a typo. But using functions instead of scripts are much better to catch such problems, because then MLint helps finding such problems.

In spite of these drawbacks the brute clearing header are frighteningly wide-spread in the code of Matlab beginners. Who on earth suggests such stuff?!

Concerning:

switch p_TH *% switches according to the TH value*

Both stars are misplaced here. I guess, this was thought for formatting in forum?

Show 7 older comments

Jan Simon
on 26 May 2013

@Jose-Luis: Co-author? I'm sure Image Analyst does not want to be responsible for the outcomes of this project. I'd guess, that hard dollars might be more encouraging.

@ARUN SAI: The voluntary contributors of this forum offer assistance for free. Therefore a commanding tone like "rectify those problems" is not welcome here. Your aim to solve your (!) academic project is no reason or excuse to omit a polite style of conversation.

## 3 Comments

## José-Luis

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76726#comment_150499

And why should anyone do your own work? Do you have a

specificquestion?## ARUN SAI

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76726#comment_150926

suggest me if there is any changes in the code that is in the computing the enumeration variable TH=up,down,left,right,level,erect,no.As iam indication that there is no noise in the image after running the program

## Image Analyst

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/76726#comment_150966

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup