【问题标题】:OpenCV Python Error Assertion failed (scn == 3 || scn == 4)OpenCV Python 错误断言失败 (scn == 3 || scn == 4)
【发布时间】:2014-03-31 00:54:21
【问题描述】:

我刚开始在 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),所以它甚至没有正确捕获帧。

究竟是什么问题,我该如何解决? 谢谢。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    在ret, frame = cap.read()之后,添加if not ret: continue。

    某些凸轮驱动程序返回无效的第一帧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多