問題描述
我正在嘗試檢測從我的網絡攝像頭拍攝的視頻中的紅色.下面給出的以下代碼示例取自 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])
具有下限 Blue HSV 值,行 upper_blue = np.array([130,255,255])
具有更高范圍的藍色 HSV 值.我在互聯網上查找了紅色的上限值和下限值,但找不到.如果有人能說出 OpenCV 的紅色 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).
我也嘗試過運行以下命令來查找 Red 的范圍,但我可能無法選擇合適的值.我試過的是這個(紅色):
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 的 RED COLOR 范圍.
This was also taken from OpenCV documentation. Please tell me or help me find the RANGE of RED COLOR for OpenCV.
推薦答案
對 red 運行相同的代碼似乎可行:
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 中檢測紅色?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!