OpenCV Python错误声明失败(scn == 3 || scn == 4)
问题内容:
我刚刚开始OpenCV
在Python中玩弄,并遇到断言错误。我从一个教程中复制了以下代码,但是对我来说不起作用。
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0) # use first webcam
if not cap.isOpened(): cap.open()
while True:
# capture frame-by-frame
ret, frame = cap.read()
# our operations on the frame come here
gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
# display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# when everything is done, release the capture
cap.release()
cv.destroyAllWindows()
跑步时我得到 OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor
当打印变量ret
和frame
从上面,我得到的(False,None)
,所以它甚至没有正确地捕捉帧。
问题到底是什么?如何解决?谢谢。
问题答案:
之后ret, frame = cap.read()
,添加if not ret: continue
。
一些凸轮驱动器返回无效的第一帧。