Searching for Yellow Error, And is this the right way to write the 'if' Statement

조회 수: 2 (최근 30일)
Im trying to have it search for the color yellow on a black background and this code doesn't function how I thought it would. The print statement was just a test so I could see if it was finding a yellow pixel but it seems to print out the statment regardless. Eventually I want it to be able to pick up a yellow ball with a little more chaotic of a background tho.
%Error in imageUpload (line 12)
% Red = [R(x,y)];
I = imread('yellow.jpg');
imshow(I)
A = imread('yellow.jpg');
R = A(:,:,1);
G = A(:,:,2);
B = A(:,:,3);
for x = 1:720
x = x+1;
for y = 1:720
y = y+1;
Red = [R(x,y)];
Gre = [G(x,y)];
Blu = [B(x,y)];
if Red > 150 && Gre > 150 && Blu < 150
fprintf("i")
else
end
end
end

채택된 답변

Mahesh Taparia
Mahesh Taparia 2021년 2월 27일
Hi
I assume, you want to find the pixels which satisfy the condition mentioned in your code. You can change your code to the below code:
A = imread('yellow.jpg');
R = A(:,:,1);
G = A(:,:,2);
B = A(:,:,3);
finalImage = zeros(720,720);
for x = 1:720
for y = 1:720
Red = [R(x,y)];
Gre = [G(x,y)];
Blu = [B(x,y)];
finalImage(x,y) = (Red > 150)&&(Gre > 150)&&(Blu < 150);
end
end
Hope it will help!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Antenna and Array Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by