can any one suggest any corrections or changes for the code witten by me.
Mostra commenti meno recenti
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*
Risposte (2)
Image Analyst
il 25 Mag 2013
Explain why you are doing this to a gray scale image:
%demosaic of image
rgb = demosaic(i,'bggr');
3 Commenti
ARUN SAI
il 25 Mag 2013
Image Analyst
il 25 Mag 2013
Modificato: Image Analyst
il 25 Mag 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.
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?
10 Commenti
Image Analyst
il 26 Mag 2013
Sometimes as beginners start tweaking their code to perfect it, changing values and changing variable names etc., if they didn't change every variable name, then without a clearvars at the start, they could end up using a variable from a prior run without realizing it because that variable with the old name is still hanging around in memory. I remember experiencing that problem before when I didn't clear out old variables. This is less likely with later versions of MATLAB where you can type shift-Enter to change all occurrences of that variable name. Actually I often run several test scripts at a time and so I just made a shortcut button called "Clean up" (following a Mathworker's recommendation) that does
clc;
clearvars;
close all force;
imtool close all;
Without having clear in my scripts, I can accumulate clutter, so when I need to, I just click that shortcut button to clean everything out.
ARUN SAI
il 26 Mag 2013
Image Analyst
il 26 Mag 2013
There's lots of things wrong, so where should I start? For example you have loops over A1, B1, A2, and B2 where you just repeatedly overwrite them without doing anything with them. What are you thinking there??? Shouldn't they have indexes?
Next, you've reversed x and y. y should be the first index and x the second because arrays are (row, column) which is (y, x), not (x, y). Etc. etc.
Why don't you step through with the debugger. Once you can run it and everything looks like what you want and expect then you can begin using it.
ARUN SAI
il 26 Mag 2013
Image Analyst
il 26 Mag 2013
You're funny.
ARUN SAI
il 26 Mag 2013
Jan
il 26 Mag 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.
ARUN SAI
il 27 Mag 2013
Categorie
Scopri di più su Lighting, Transparency, and Shading in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!