2014-03-31 82 views
0

我刚刚开始在Python中使用OpenCV,并且正在运行一个断言错误。 我从tutorial复制了以下代码,但它不适用于我。OpenCV Python错误断言失败(scn == 3 || scn == 4)

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的错误:断言在CV :: cvtColor失败(SCN == 3 || SCN == 4)” ......

当打印变量RET和框架从上面我得到(False,None),所以它甚至没有正确地捕获帧。

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

答:补充if not ret : continue因为有时第一帧是“死”

+2

在读取后添加一个'如果不是ret:continue',因为某些凸轮驱动程序返回无效第一帧(相机预热) – berak

+0

谢谢,修正了它! – pushkin

回答

2

ret, frame = cap.read()后需要加if not ret: continue

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

相关问题