如何在OpenCV Python中检测红色?

时间:2016-08-10 15:02:48

标签: python python-2.7 opencv computer-vision opencv3.1

我正在尝试检测从我的网络摄像头拍摄的视频中的红色。以下代码示例取自OpenCV Documentation. 代码如下:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

lower_blue = np.array([110,50,50])具有较低范围的蓝色HSV值,而行upper_blue = np.array([130,255,255])具有较高范围的蓝色HSV值。我在互联网上寻找红色的上限和下限,但我找不到它。如果有人能告诉OpenVV的Red的HSV值(OpenCV H值范围从0到179),将会非常有帮助。 非常感谢您的帮助(In Advance)。

我也试过运行以下内容来查找Red的范围,但我无法选择正确的值。我试过的是这个(红色):

>>> green = np.uint8([[[0,255,0 ]]])
>>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>>> print hsv_green
[[[ 60 255 255]]]

这也取自OpenCV文档。 请告诉我或帮我找到OpenCV的RED COLOR范围。

2 个答案:

答案 0 :(得分:2)

运行相同的红色代码似乎有效:

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

然后你可以试试看起来偏红的不同颜色。请注意,红色范围包括略大于0的数字和略小于179的数字(例如red = numpy.uint8([[[0,31,255]]])会产生[[[ 4 255 255]]],而red = numpy.uint8([[[31,0,255]]])会产生[[[176 255 255]]]

答案 1 :(得分:1)

这是一个通过选择6个数组参数来确定所需颜色的程序。(适用于Opencv 3.2)。 您选择了自己的图像或“色彩范围”#34;输入图像并移动光标,并查看哪些数组值是您需要隔离颜色的值! Color range program screen pic

这里是代码:(可以很容易地适应视频输入)。 image.jpg->(您的图片) color_bar.jpg->(你想要显示窗户的任何图像,尝试任何东西)

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