# Detecting Percentage of Red ,Green , Blue ,Cyan , Magenta , Yellow

I am trying to detect the percentage of the RGB and CMY in the Image .

I am doing this in the Python OpenCV with Numpy . I am providing the Minimum and Maximum Range of Red able to detect the amount of Red in the image .

But the difficulty which i am facing is to provide correct Range to the RED ,Green , Blue ,Cyan , Magenta , Yellow . Please help me to Provide the Range for each Color .

For Ex below is the Program which i have written for Red.

def ColorDetection():
size = img.size
print("Size",size)
SizeRGB=size/3
RED_MIN=np.array([0, 0, 128], np.uint8)
RED_MAX= np.array([250, 250, 255], np.uint8)
CheckRange = cv2.inRange(img, RED_MIN, RED_MAX)
print("CheckRange",CheckRange)
PixelsInRange = cv2.countNonZero(CheckRange)
print("PixelsInRange",PixelsInRange)
frac_red = np.divide(float(PixelsInRange), int(SizeRGB))
print(frac_red)
percent_red = np.multiply((float(frac_red)), 100)
print('Red: ' + str(percent_red) + '%')

I am fine to use HSV range also . Please help over this for other colors also . (By helping me to select the correct Range)

edit retag close merge delete

1

try to visualize the inRange() output using imshow()

(your RED ranges look all wrong)

( 2018-08-31 05:22:36 -0500 )edit

Sort by ยป oldest newest most voted

IF you want to check the percentage of the color red(R=255 , G = 0 , B = 0) the count pixels and calculate the ratio.. but if you'are looking for a certain range you'll have to decide that by yourself. RGB and CMY are both in the range 0~255

this is the conversion formula :

float C = 1 - (R / 255);
float M = 1 - (G / 255);
float Y = 1 - (B / 255);
more

Official site

GitHub

Wiki

Documentation