my code doesnt go into the loop

2 次查看(过去 30 天)
picture = imread ('C:\Users\prempreet\Desktop\lena512color.tiff');
noisy_picture= imnoise (picture,'salt & pepper', 0.10)
% image = picture(: , :);
image_red = picture (:, :, 1);
image_green = picture (:, :, 2);
image_blue = picture (:, :, 3);
redChannel = noisy_picture(:, :, 1);
greenChannel = noisy_picture(:, :, 2);
blueChannel = noisy_picture(:, :, 3);
%median = medfilt2 ( image );
red_median = medfilt2 (redChannel);
blue_median = medfilt2 (blueChannel);
green_median = medfilt2 (greenChannel);
difference_red = abs (redChannel - red_median);
difference_blue = abs (blueChannel - blue_median);
difference_green = abs (greenChannel - green_median);
difference_red = im2double (difference_red);
difference_green = im2double (difference_green);
difference_blue = im2double (difference_blue);
[m n] = size (difference_red);
while ( i == 2:m-1 && j == 2:n-1 )
a1 = [difference_red(i-1,j-1), difference_red(i-1,j), difference_red(i-1,j+1), difference_red(i,j-1), difference_red(i,j), difference_red(i,j+1),difference_red(i+1,j-1), difference_red(i+1,j), difference_red(i+1,j+1)];
b1 = [difference_blue(i-1,j-1), difference_blue(i-1,j), difference_blue(i-1,j+1), difference_blue(i,j-1), difference_blue(i,j), difference_blue(i,j+1), difference_blue(i+1,j-1), difference_blue(i+1,j), difference_blue(i+1,j+1)];
c1 = [difference_green(i-1,j-1), difference_green(i-1,j), difference_green(i-1,j+1),difference_green(i,j-1), difference_green(i,j), difference_green(i,j+1), difference_green(i+1,j-1), difference_green(i+1,j), difference_green(i+1,j+1)];
a2 = a1(a1<=0.7);
b2 = b1 (b1<= 0.7);
c2 = c1 (c1<=0.7);
med_red (i,j)= median (a2);
med_blue (i,j)= median (b2);
med_green (i,j)= median (c2);
the code doesnt go into the loop.......
  1 个评论
Jan
Jan 2011-11-13
The question would be much easier to understand, if yo omit all unnecessary details. Actually you need these two lines only:
[m n] = size (difference_red);
while ( i == 2:m-1 && j == 2:n-1 )
The rest is only confusing.

请先登录,再进行评论。

采纳的回答

Wayne King
Wayne King 2011-11-13
That is not correct syntax for a while loop. For example:
% This does not work!
n = 1;
while (n==1:4)
disp('Hi');
n = n+1;
end
% This works!
n = 1;
while (n>=1 & n<4)
disp('Hi');
n=n+1;
end
Without getting in the specifics of your code, how about trying:
for i=2:m-1
for j = 2:n-1
...
...
end
end
  2 个评论
prem preet
prem preet 2011-11-13
its doesnt work sir ......
the code still dont go into the loop
Jan
Jan 2011-11-13
@prem: Accepting an answer means, that your problem is solved. But you comment sounds, like there are still problems.
Please explain exactly, what you have changed.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2011-11-13
[m, n] = size (difference_red);
while ( i == 2:m-1 && j == 2:n-1 ) % Not working
The argument of while contains vectors: "i == 2:m-1" is a LOGICAL vector. But the && operator works on scalars only. If you use the & or and() operation, the next problem occurs: Then the two logical vectors i==2:m-1 and j==2:n-1 must have the same number of elements or one must be a scalar, because the and works elementwise.
Assuming that both LOGICAL vectors have teh same size accidently, the next problem occurs: If the argument of while or if is not a scalar, a all is inserted automatically:
while all(i == 2:m-1 & j == 2:n-1)
But there is always an element, which is not true in the vector (if it can be evaluated at all, see above). Therefore I assume the two FOR loops suggested by Wayne are doing what your want.

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by