簡體   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