【发布时间】:2014-10-11 09:35:45
【问题描述】:
我需要使用一些运动检测代码,然后我使用此链接提供的以下代码: http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/ 。 代码如下:
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(0)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
while True:
cv2.imshow(winName, diffImg(t_minus, t, t_plus) )
#diff = diffImg(t_minus, t, t_plus)
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
#cv2.imshow(winName, diff)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
一开始运行很流畅,现在却报错了:
cv2.error: ........\opencv\modules\imgproc\src\color.cpp:3737: 错误: (-215) scn == 3 ||函数 cv::cvtColor 中的 scn == 4
我在stackoverflow中找到了各种解决方案,但仍然出现错误。据说发生错误是因为源代码(函数调用中的第三个参数)指示的颜色格式不正确。 谁能告诉我为什么会发生错误?还是那个 opencv 的 bug,没有解决办法?
【问题讨论】:
-
请在此处粘贴使用的代码,而不是某个博客的链接。还添加有问题的图像
-
我很抱歉我的错误。我已经编辑了我的问题。图像来自相机——它是来自相机的实时捕捉。你能帮帮我吗?
-
cam.read() 返回:错误,img。你应该不丢弃错误值。您捕获的内容可能出现了一些问题,并且暂时无法传送帧。 (我怀疑,img 只是 empty)
标签: python-2.7 opencv motion-detection