本文介绍了cv2.VideoCapture错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于cv2.VideoCapture的问题已经很多,但这对我没有帮助.我正在使用OpenCV 2.4.10和python 2.7.9,我试图从内置网络摄像头(东芝,Windows 7)中捕获视频.我正在使用此代码

There are already many questions exist regarding cv2.VideoCapture but it doesn't help me.I am using OpenCV 2.4.10 and python 2.7.9 , I am trying to capture video from built-in webcam (Toshiba ,Windows 7) . I am using this code

import numpy as np
import cv2

cap = cv2.VideoCapture(1)
print cap.isOpened()
print cap.get(3)
print cap.get(4)
while(True):
    ret,frame=cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

问题是,当我第一次运行此代码时,它引发错误:error: (-215) size.width>0 && size.height>0 in function cv::imshow

And the problem is , When i first run this code it throws error : error: (-215) size.width>0 && size.height>0 in function cv::imshow

等待了近一分钟之后,我再次运行了这段代码,这次很好

and just after waiting for hardly one minute , i again run this code and this time it's fine

这又一次又一次地发生,有时效果很好,但有时会失败并给出错误.有什么解决办法吗?

And this is happening again and again , sometimes it's work good but sometimes fails and give error. Any solution to this ?

我将opencv_ffmpeg2410.dll文件从C:\Applications\opencv\build\x86\vc10\bin复制到了python路径C:\Python27()

I copied the opencv_ffmpeg2410.dll file from C:\Applications\opencv\build\x86\vc10\bin into the python path C:\Python27(OpenCV 2.4 VideoCapture not working on Windows)

我尝试了cv2.VideoCapture(0),但在这种情况下,网络摄像机甚至无法启动(网络摄像机指示灯不闪烁)

I tried cv2.VideoCapture(0) but in this case web-camera didn't even start (web-camera light did not blink)

推荐答案

由于您没有检查ret返回值的值,因此cap.read()可能会给您带来不好的帧.

Since you are not checking the value of the ret return value, it is possible that cap.read() is giving you a bad frame.

替换为类似

while True:
    ret, frame = cap.read()
    if ret:
        cv2.imshow('window', frame)
        # ...

这篇关于cv2.VideoCapture错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 21:35