Find yellow pixels in an image

37 visualizzazioni (ultimi 30 giorni)
Yingxiu Guo
Yingxiu Guo il 25 Giu 2019
Commentato: Image Analyst il 26 Giu 2019
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 Commenti
Jan
Jan il 25 Giu 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 il 26 Giu 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!

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 26 Giu 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 Commenti
Yingxiu Guo
Yingxiu Guo il 26 Giu 2019
Hey thanks for your reply.
I think I made some progress here.
Cheers!
Image Analyst
Image Analyst il 26 Giu 2019
If you want, you can use my code from here: my File Exchange
screenshot.jpg

Accedi per commentare.

Più risposte (1)

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

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by