我正试图从我的网络摄像头拍摄的视频中检测红色。下面给出的代码示例取自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()
lower_blue = np.array([110,50,50])
具有较低范围的Blue HSV值,upper_blue = np.array([130,255,255])
具有较高范围的Blue HSV值。我在网上找过红色的上限和下限,但是我找不到。这将是非常有帮助的,如果有人能告诉红色的HSV值为OpenCV (OpenCV H值范围从0 - 179)。非常感谢你的帮助(提前)。
我也试着运行以下命令来找到红色的范围,但我可能无法选择合适的值。我尝试的是(对于红色):
>>> green = np.uint8([[[0,255,0 ]]])
>>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>>> print hsv_green
[[[ 60 255 255]]]
这也是取自OpenCV文档。请告诉我或帮我找到OpenCV的红色范围
对红色运行相同的代码似乎有效:
>>> 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]]]
。
下面是一个程序,通过选择6个数组参数来确定您需要的颜色。(工作在Opencv 3.2)。你选择你的图像或"颜色范围栏"输入图像,你移动光标,看看哪些数组值是你需要隔离你的颜色!色彩范围程序画面图
下面是代码:(可以很容易地适应视频输入)。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()