What is wrong with the following piece of code? Im trying to detect light and dark green vegetation. It just displays a black empty image

조회 수: 2 (최근 30일)
Image1 = im2double(imread('image\test.png'));
g=rgb2gray(Image1);
b=imread('image\1.jpg');
imshow(b);
[row column page] = size(Image1)
for i = row
for j = column
if (Image1 (i, j, 2) > Image1(i, j, 1) && Image1(i, j, 2) > Image1(i, j, 3))
Image2 (i, j, 1) = Image1 (i, j, 1);
Image2 (i, j, 2) = Image1 (i, j, 2);
Image2 (i, j, 3) = Image1 (i, j, 3);
else
Image2 (i, j, 1) = 0;
Image2 (i, j, 2) = 0;
Image2 (i, j, 3) = 0;
end
end
end
[row column page] = size(Image1)
for i = row
for j = column
Green = Image1 (i, j, 2) + 20;
if (Green > Image1(i, j, 1) && Green > Image1(i, j, 3))
Image2 (i, j, 1) = Image1 (i, j, 1);
Image2 (i, j, 2) = Image1 (i, j, 2);
Image2 (i, j, 3) = Image1 (i, j, 3);
else
Image2 (i, j, 1) = 0;
Image2 (i, j, 2) = 0;
Image2 (i, j, 3) = 0;
end
end
end
[row column page] = size(Image2)
imshow(Image2);

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 2일
Your Image1 has been produced by im2double() so the range of Image1 is 0 to 1. But your second set of code has
Green = Image1 (i, j, 2) + 20;
so Green is going to be in the range (20+0) to (20+1), which is always going to be greater than the 0 to 1 entries already in Image1, so your "if" is always going to be true.
Note that your second block of code, involving "Green" is going to completely overwrite the output of the first block of code. Your first block might as well not be there.
Your first block could be rewritten without loops as
mask = Image1(:,:,2) > Image1(:,:,1) & Image1(:,:,2) > Image1(:,:,3);
Image2 = Image1 .* mask(:,:,[1 1 1]);
and your second block could be rewritten without loops as
mask = Image1(:,:,2) + 20 > Image1(:,:,1) & Image1(:,:,2) + 20 > Image1(:,:,3);
Image2 = Image1 .* mask(:,:,[1 1 1]);

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by