Find yellow pixels in an image

7 views (last 30 days)
So I have an image, jpg file, which means it is RGB?
I want to find out how many pixels are yellow, the following code is what I have now. Shouldn't be too hard except I don't know the syntax
img=imread('MyImage');
[a,b,c]=size(img);
count_y=0;
for i=1:a
for j=1:b
if img(a,b)==yellow %%% How do I write this line??
count_y=count_y+1;
end
end
end
Thanks folks!
  2 Comments
Jan
Jan on 25 Jun 2019
if img(a, b, :) == yellow
% ^
You have to consider the RGB values, therefore you need to extract the [1x3] RGB vector for each pixel.
How do you define "yellow" exactly? There are different RGB colors looking very yellowish, so "yellow" means a range of colors usually. Then the HSV color space is smarter. There are different methods to determine the distance between colors in the HSV space, but it is your turn to define, what "yellow" is for your needs.
Yingxiu Guo
Yingxiu Guo on 26 Jun 2019
Hi Jan, thanks for your reply. I want bright yellow, a little bit greenish
Right now in the loop I have
if img(i,j,1) > 200 && img(i,j,2) > 200 && img(i,j,3) < 50
So the first component is red, the second is green, and the third is blue? Does this selects the yellow colors?
Thanks again!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Jun 2019
Use the Color Thresholder on the Apps tab of the tool ribbon. Select HSV color space and load your image then select thresholds and then export the function.
  2 Comments
Yingxiu Guo
Yingxiu Guo on 26 Jun 2019
Hey thanks for your reply.
I think I made some progress here.
Cheers!
Image Analyst
Image Analyst on 26 Jun 2019
If you want, you can use my code from here: my File Exchange
screenshot.jpg

Sign in to comment.

More Answers (1)

KSSV
KSSV on 25 Jun 2019
  2 Comments
Yingxiu Guo
Yingxiu Guo on 25 Jun 2019
I tried that. Not sure it works for my purpose
Image Analyst
Image Analyst on 26 Jun 2019
How could we know? -- you forgot to attach your image.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!