我刚刚开始在Python中使用OpenCV
,并且遇到了断言错误。我从tutorial复制了以下代码,但它对我不起作用。
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)
,因此它甚至无法正确捕获帧。
究竟是什么问题,我该如何解决? 谢谢。
答案 0 :(得分:3)
在ret, frame = cap.read()
之后,添加if not ret: continue
。
某些凸轮驱动程序返回无效的第一帧。