Ask Your Question
-3

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

asked 2018-08-30 22:05:33 -0600

sreevathsabr gravatar image

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():
        img = cv2.imread(r'C:\Test\ImagingOpenCV\DemoFileSS\3.png')
        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 flag offensive close merge delete

Comments

1

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

(your RED ranges look all wrong)

berak gravatar imageberak ( 2018-08-31 05:22:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-31 02:24:25 -0600

Ziri gravatar image

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);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-08-30 21:57:34 -0600

Seen: 1,915 times

Last updated: Aug 31 '18