我正在尝试运行一个在python中使用opencv编写的脚本,它使用网络摄像头来跟踪彩色对象(这里的对象是蓝色的),这在这里的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()
但此代码会产生错误:
OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar) in inRange, file /build/buildd/opencv-2.4.2+dfsg/modules/core/src/arithm.cpp, line 2527
Traceback (most recent call last):
File "blue.py", line 19, in <module>
mask = cv2.inRange(hsv, lower_blue, upper_blue)
cv2.error: /build/buildd/opencv-2.4.2+dfsg/modules/core/src/arithm.cpp:2527: error: ( (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange
我试过解决堆栈溢出相关问题的方法,但是没有一个有用。代码有什么问题?为什么会出现这个错误?
我在ubuntu上使用opencv 2.4.2和python 2.7
3条答案
按热度按时间5kgi1eie1#
HSV中蓝色的范围应如下给出:
z2acfund2#
这里有一个HSV颜色阈值脚本来确定下限和上限范围,而不是猜测和检查
pn9klfpd3#
为了在OpenCV-python中检测基于颜色的对象,我想这个repo可以帮助你查看这个GitHub repo:
https://github.com/shashiben/Opencv/blob/master/Object%20Detection/object_detect_with_hsv.py
我使用跟踪条根据HSV颜色跟踪对象