Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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():
        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)