[英]OpenCV Python Error Assertion failed (scn == 3 || scn == 4)
I just started playing around with OpenCV
in Python and am running into an assertion error. 我刚刚开始在Python中使用
OpenCV
,并且遇到了断言错误。 I copied the following code from a tutorial , but it's not working for me. 我从教程中复制了以下代码,但对我而言不起作用。
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()
When running, I get OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor
运行时,出现
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor
When printing the variables ret
and frame
from above, I get (False,None)
, so it's not even capturing the frame correctly. 从上方打印变量
ret
和frame
时,我得到(False,None)
,所以它甚至无法正确捕获帧。
What exactly is the issue, and how can I resolve it? 问题到底是什么?如何解决? Thank you.
谢谢。
After ret, frame = cap.read()
, add if not ret: continue
. ret, frame = cap.read()
, if not ret: continue
,则添加if not ret: continue
。
Some cam-drivers return an invalid first frame. 一些凸轮驱动器返回无效的第一帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.