我需要使用一些移动侦测代码,然后我使用以下代码,通过此链接提供: http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/。 下面是代码:cv2.cvtColor错误。它是错误吗?
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: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
我发现在计算器的各种解决方案,但仍然出现错误。有人说错误发生的原因是源代码没有正确的颜色格式,代码(函数调用中的第三个参数)表示它应该。 任何人都可以给我想法,为什么错误发生?或者是opencv的bug,并没有解决方案?
请粘贴在此处使用的代码*,而不是某个博客的链接。还添加有问题的图片 – berak 2014-10-11 09:37:20
我很抱歉我的错误。我已经编辑了我的问题。图像来自相机 - 它是从相机实时捕捉的。你可以帮我吗? – rizkie 2014-10-11 13:34:05
cam.read()返回:err,img。你应该*不*丢弃错误值。你捕获的可能会出现某种呃逆,暂时无法传送帧。 (我怀疑,img只是*空*) – berak 2014-10-11 14:56:11