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.
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
This is actually a really dynamic question and it depends on what you define as the colour percentage. If you just want to calculate how much of your image is red, green and blue you can try the solution in this answer.
First, channels[0] should be the blue channel, because OpenCV is loading the image in BGR space. Second you are counting the non-blue values (channel[0] is blue, and between 0 and 10, there is almost no blue). Third, if you want to count the exact red, blue and green, you should use HSV (careful on full and half HSV), because 255R+255G+255B=white, etc; but if you want to see if there is R, or G or B in a colour of a pixel, then your approach is good if you do the corrections about the channels and intensities.