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)</

在尝试使用OpenCV从摄像头读取视频并进行颜色转换时,遇到cv2.cvtColor错误,提示scn == 3 || scn == 4条件未满足。错误发生在读取和转换连续帧的过程中。尽管有人建议可能是颜色格式问题,但问题可能出在摄像头未能正确传输帧。一位用户指出,忽视cam.read()返回的错误状态可能导致此问题,建议检查是否成功读取帧。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



