my code doesnt go into the loop
1 次查看(过去 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
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
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File 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!