2

I have some RGB images.

What's the best way to know most color used in an image is Red or Yellow or White? The input images must have more than 50% red, yellow or white pixels and it's impossible to an image have two colors in same percentage. Other colors in image may be black or blue.

Is there any function in MATLAB for this?

Note that i need a method with good performance for this!

Thanks in advance...

Jalal
  • 6,594
  • 9
  • 63
  • 100
  • 1
    Related question: [How can I convert an RGB image to grayscale but keep one color?](http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color) Parts of [my answer there](http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color/4064205#4064205) can be adapted for selecting red and yellow based on the *hue*, and white can be selected for based on the *value* (i.e. lightness or brightness). – gnovice Feb 28 '11 at 15:31

1 Answers1

3

Convert your image to the HSV colorspace (rgb2hsv) and find appropriate thresholds for Red, Yellow and White on the Hue values. E.g.

[H S V] = rgb2hsv(I);
num_red_pixels = nnz(H>=red_min & H<=red_max);
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • Thank you for answer, but is there a better way? I think, converting an image for just knowing max color used is not a good idea. Can i use `histogram`? – Jalal Feb 28 '11 at 14:36
  • Are you worried about speed? First calculate the amount of time it takes to convert an RGB image to HSV and **then** pursue other algorithms if it is a problem. Profile it first. You can use `tic` and `toc`. – Jacob Feb 28 '11 at 15:28
  • And yes, you can just use a threshold based on RGB, but depending on the situation, the results can vary. HSV is more robust. If you want to stick with RGB, you have to find the right thresholds for the R,G and B components instead of just one threshold for H. – Jacob Feb 28 '11 at 15:29