How To Detect Red Color In OpenCV Python?

后端 未结 2 326
我在风中等你
我在风中等你 2021-01-07 09:11

I am trying to detect red color from the video that\'s being taken from my webcam. The following code example given below is taken from OpenCV Documentation. The code is gi

相关标签:
2条回答
  • 2021-01-07 10:02

    Here is a program to determine color you need by choosing the 6 arrays parameters.(work on Opencv 3.2). You chose your image or a "color range barre" input image and you move cursors and see which arrays values are the ones you need to isolate your color! Color range program screen pic

    here is the code:(can easily be adapted for video input). image.jpg->(your image) color_bar.jpg->(any image you want just to display a windows,try anything)

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    def nothing(x):
        pass
    
    def main():
    
        window_name='color range parameter'
        cv2.namedWindow(window_name)
        # Create a black image, a window
        im = cv2.imread('image.jpg')
        cb = cv2.imread('color_bar.jpg')
        hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
    
        print ('lower_color = np.array([a1,a2,a3])')
        print ('upper_color = np.array([b1,b2,b3])')
    
    
        # create trackbars for color change
        cv2.createTrackbar('a1',window_name,0,255,nothing)
        cv2.createTrackbar('a2',window_name,0,255,nothing)
        cv2.createTrackbar('a3',window_name,0,255,nothing)
    
        cv2.createTrackbar('b1',window_name,150,255,nothing)
        cv2.createTrackbar('b2',window_name,150,255,nothing)
        cv2.createTrackbar('b3',window_name,150,255,nothing)
    
        while(1):
            a1 = cv2.getTrackbarPos('a1',window_name)
            a2 = cv2.getTrackbarPos('a2',window_name)
            a3 = cv2.getTrackbarPos('a3',window_name)
    
            b1 = cv2.getTrackbarPos('b1',window_name)
            b2 = cv2.getTrackbarPos('b2',window_name)
            b3 = cv2.getTrackbarPos('b3',window_name)
    
            # hsv hue sat value
            lower_color = np.array([a1,a2,a3])
            upper_color = np.array([b1,b2,b3])
            mask = cv2.inRange(hsv, lower_color, upper_color)
            res = cv2.bitwise_and(im, im, mask = mask)
    
            cv2.imshow('mask',mask)
            cv2.imshow('res',res)
            cv2.imshow('im',im)
            cv2.imshow(window_name,cb)
    
            k = cv2.waitKey(1) & 0xFF
            if k == 27:         # wait for ESC key to exit
                break
            elif k == ord('s'): # wait for 's' key to save and exit
                cv2.imwrite('Img_screen_mask.jpg',mask)
                cv2.imwrite('Img_screen_res.jpg',res)
                break
    
    
        cv2.destroyAllWindows()
    
    
    #Run Main
    if __name__ == "__main__" :
        main()
    
    0 讨论(0)
  • 2021-01-07 10:05

    Running the same code for red seems to work:

    >>> red = numpy.uint8([[[0,0,255]]])
    >>> hsv_red = cv2.cvtColor(red,cv2.COLOR_BGR2HSV)
    >>> print(hsv_red)
    [[[  0 255 255]]]
    

    And then you can try different colors that appear reddish. Beware that the red range includes both numbers slightly greater than 0 and numbers slightly smaller than 179 (e.g. red = numpy.uint8([[[0,31,255]]]) results in [[[ 4 255 255]]] whereas red = numpy.uint8([[[31,0,255]]]) results in [[[176 255 255]]].

    0 讨论(0)
提交回复
热议问题