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

1 view (last 30 days)
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

Accepted Answer

Mahesh Taparia
Mahesh Taparia on 27 Feb 2021
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!

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!