【发布时间】:2015-06-06 05:38:37
【问题描述】:
我正在尝试使用 OpenCV 和 python 检测对象。这是我尝试运行的代码。
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture(1)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
while True:
cv2.imshow( winName, diffImg(t_minus, t, t_plus) )
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_GRAY2BGR)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
当我运行此代码时,它会出现以下错误。
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3739
Traceback (most recent call last):
File "C:/Users/Ravi/PycharmProjects/Test/thread1.py", line 14, in <module>
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
cv2.error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
我尝试过改变 color.BRG2GRAY 的几种方式(RGB2GRAY...等),并且我尝试过使用我的默认网络摄像头和其他 USB 网络摄像头。但是两种方式都会给出相同的错误。我该怎么做才能解决这个问题?
当我在 Ubuntu 平台上运行 same code 时,出现以下错误。
Traceback (most recent call last):
File "/home/ravi/PycharmProjects/Test/thread1.py", line 11, in <module>
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
AttributeError: 'module' object has no attribute 'CV_WINDOW_AUTOSIZE'
【问题讨论】:
-
该错误意味着您的输入图像没有 3 或 4 个通道,这是假设为彩色图像。我不了解 python api,但 cam.read()[1] 是否意味着您只使用通道号 1?请改用 cam.read()。
-
我试过了,但它给出了
src is not a numerical tuple错误@Micka -
您可以将图像读取到变量中并显示未修改的图像吗?并提前测试捕获的图像是否为空?
标签: python opencv numpy object-detection