我刚刚开始在Python中使用OpenCV,并且遇到了一个断言错误。我从一个教程复制了下面的代码,但是它不适合我。
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),所以它甚至没有正确地捕获帧。
问题到底是什么,我该如何解决呢?谢谢。
发布于 2016-10-13 19:53:47
在ret, frame = cap.read()之后添加if not ret: continue。
一些凸轮驱动器返回一个无效的第一帧。
https://stackoverflow.com/questions/22752002
复制相似问题