Ask Your Question
1

How to find R,G,B percentage in an image

asked 2015-05-28 00:40:29 -0600

Deepak Kumar gravatar image

updated 2015-10-12 04:27:36 -0600

pklab gravatar image

hi, i want to find red green and blue percentage in an image. i used the following code which shows 0%age for the red. help to improve my code.

below is code :

vector<Mat> channels;
split(hsv_img,channels);

Mat red, blue, green;
inRange(channels[0], Scalar(0), Scalar(10), red); // red
// ... do the same for blue, green, etc only changing the Scalar values and the Mat

double image_size = hsv_img.cols*hsv_img.rows;
double red_percent = ((double) cv::countNonZero(red))/image_size;

what shoud i change in scalar to have green and blue value.

thanks !!

edit retag flag offensive close merge delete

Comments

1

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.

theodore gravatar imagetheodore ( 2015-05-28 06:45:04 -0600 )edit

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.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-28 06:46:30 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2015-05-28 06:20:52 -0600

pklab gravatar image

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

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-05-28 00:40:29 -0600

Seen: 4,878 times

Last updated: May 28 '15