繁体   English   中英

OpenCV视频捕获

[英]OpenCV Video Capture

我正在使用OpenCV 2.4.8和Visual Studio 2013运行以下简单的VideoCapture程序。 该程序旨在从网络摄像头捕获视频并显示它。 但是,该程序只能在第一次(在登录Windows之后)正常运行,而第二次则无法运行。 我在调试时遇到的问题是:执行此行后-“ bool bSuccess = cap.read(frame);” 框架变量仍为NULL。

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;
char key;
int main(int argc, char* argv[])
{
   VideoCapture cap(0); // open the video camera no. 0

    if(!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    Mat frame;
    while(1)
    {


        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break; 
        }
    }
    return 0;

}

发生这种情况的原因是,在第一个程序实例之后,相机未正确关闭。 您应该尝试通过esc按钮而不是通过单击X关闭控制台。

在中断循环之前,您可以尝试阅读多个帧吗? 这可能类似于此问题,唯一的问题是第一帧损坏/设置缓慢的相机。

无法从带有OpenCV的辅助网络摄像头的VideoCapture中读取帧

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM