如何在OpenCV Python中检测红色? [英] How To Detect Red Color In OpenCV Python?
问题描述
我正在尝试从网络摄像头拍摄的视频中检测红色.下面给出的以下代码示例摘自 OpenCV文档. 代码如下:
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 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()
行lower_blue = np.array([110,50,50])
具有较低范围的蓝色HSV值,行upper_blue = np.array([130,255,255])
具有较高范围的蓝色HSV值.我在互联网上寻找红色的上限值和下限值,但找不到.如果有人可以告诉OpenCV Red的HSV值(OpenCV H值范围为0-179),这将非常有帮助.
非常感谢您的帮助(预先).
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. 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).
Thanks a lot for help (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]]]
这也来自OpenCV文档. 请告诉我或帮助我找到OpenCV的红色范围.
This was also taken from OpenCV documentation. Please tell me or help me find the RANGE of RED COLOR for OpenCV.
推荐答案
为红色运行相同的代码似乎可行:
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]]]
然后您可以尝试显示红色的不同颜色.请注意,红色范围同时包含略大于0的数字和略小于179的数字(例如,red = numpy.uint8([[[0,31,255]]])
会导致[[[ 4 255 255]]]
,而red = numpy.uint8([[[31,0,255]]])
会导致[[[176 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]]]
.
这篇关于如何在OpenCV Python中检测红色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!