Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To count how many pixel has a given colour you have to look at HSV color whell and define an aceptable bias for your colors or use standard bias for primary, secondary or tertiary colors. For example how do you consider orange, and magenta, and...?

  • Use only primary colors (red green blue) => 360°/3color = 120° each color
  • Using secondary colors (red, yellow, green, cyan,blue magenta)=> 360°/6color = 60° each color
  • Using tertiary colors (red, orange, yellow,...) => 360°/12 color = 30° each color

Remeber that because in OpenCV an HSV image is 3chan x8bit, the Hue value is divided by 2 to fit 360° in 1 byte.

HSV color whell

In the HSV color whell with secondary colors

H =   0°...30° => RED
H = 30°..90° => Yellow
H = 90°..150° => Green
H = 150°..210° => Cyan
H = 210°..270° => Blue
H = 270°..330° => Magenta
H = 330°..360° => RED

If you want to use only primary color (RGB) the above table becomes:

H =   0°...60° => RED
H =  60°..180° => Green
H = 180°..300° => Blue
H = 300°..360° => RED

Dividing Hue by 2 you have corresponding value ready for OpenCV:

Red   : 0<= H <30 and 150<= H <180
Green : 30<= H < 90
Blu   : 90<= H < 150

this are values you are looking for if you accept to count magenta and orange as red, cool yellow as green and so on.

Tips to count red pixel you can count not red than subtract to total pixel count

RedCount = numPixel - (greenCount-BlueCount)

I hope this helps