简体   繁体   English

如何在OpenCV Python中检测红色?

[英]How To Detect Red Color In OpenCV Python?

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. 下面给出的以下代码示例来自OpenCV文档。 The code is given below: 代码如下:

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

The line lower_blue = np.array([110,50,50]) has the lower range Blue HSV value and the line upper_blue = np.array([130,255,255]) has the higher range Blue HSV value. lower_blue = np.array([110,50,50])具有较低范围的Blue HSV值,而行upper_blue = np.array([130,255,255])具有较高范围的Blue HSV值。 I have looked for the upper value and lower value of Red color on internet but I couldn't find it. 我在互联网上寻找红色的上限和下限,但我找不到它。 It would be very helpful if anyone could tell the HSV value of Red for OpenCV (OpenCV H value ranges from 0 - 179). 如果有人能告诉OpenVV的Red的HSV值(OpenCV H值范围从0到179),将会非常有帮助。 Thanks a lot for help (In Advance). 非常感谢您的帮助(In Advance)。

I have also tried running the following to find the range of Red but I was unable to pick proper value maybe. 我也试过运行以下来找到红色的范围,但我无法选择适当的值。 What I tried was this(for red): 我试过的是这个(红色):

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

This was also taken from OpenCV documentation. 这也取自OpenCV文档。 Please tell me or help me find the RANGE of RED COLOR for OpenCV. 请告诉我或帮我找到OpenCV的RED COLOR范围。

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 (eg red = numpy.uint8([[[0,31,255]]]) results in [[[ 4 255 255]]] whereas red = numpy.uint8([[[31,0,255]]]) results in [[[176 255 255]]] . 请注意,红色范围包括略大于0的数字和略小于179的数字(例如, red = numpy.uint8([[[0,31,255]]])导致[[[ 4 255 255]]]red = numpy.uint8([[[31,0,255]]])导致[[[176 255 255]]]

Here is a program to determine color you need by choosing the 6 arrays parameters.(work on Opencv 3.2). 这是一个通过选择6个数组参数来确定所需颜色的程序。(在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) 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM