如何在 OpenCV Python 中检测红色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38877102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Detect Red Color In OpenCV Python?
提问by Omee
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:
我试图从我的网络摄像头拍摄的视频中检测红色。下面给出的以下代码示例取自OpenCV 文档。代码如下:
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. 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).
线lower_blue = np.array([110,50,50])
具有较低范围的蓝色 HSV 值,而线upper_blue = np.array([130,255,255])
具有较高的范围蓝色 HSV 值。我在互联网上寻找红色的上限值和下限值,但找不到。如果有人能说出 OpenCV 的 Red 的 HSV 值(OpenCV H 值范围为 0 - 179),那将非常有帮助。非常感谢您的帮助(提前)。
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):
我也试过运行以下命令来找到 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. Please tell me or help me find the RANGE of RED COLOR for OpenCV.
这也取自 OpenCV 文档。请告诉我或帮我找到 OpenCV 的 RED COLOR 范围。
回答by Aaron Klein
Running the same code for red seems to work:
为 red 运行相同的代码似乎有效:
>>> 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 的数字和略小于 179 的数字(例如red = numpy.uint8([[[0,31,255]]])
导致[[[ 4 255 255]]]
而red = numpy.uint8([[[31,0,255]]])
导致[[[176 255 255]]]
.
回答by bastoon
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
这是一个通过选择 6 个数组参数来确定您需要的颜色的程序。(在 Opencv 3.2 上工作)。您选择了图像或“颜色范围barre”输入图像,然后移动光标并查看哪些数组值是隔离颜色所需的值! 颜色范围程序屏幕图片
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()