【问题标题】:OpenCV Video CaptureOpenCV 视频捕获
【发布时间】:2016-01-09 04:23:41
【问题描述】:

我正在使用 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;

}

【问题讨论】:

    标签: c++ opencv visual-studio-2013


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答。我尝试使用 esc 按钮关闭控制台,但第一次后仍然没有捕获帧。还有什么建议吗?
    • 我建议您至少升级到 OpenCV 2.4.11 或更好的 OpenCV 3.0
    • 尝试使用另一台相机或另一台电脑
    【解决方案2】:

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

    Unable to read frames from VideoCapture from secondary webcam with OpenCV

    【讨论】:

    • 我也试过这个解决方案。但我仍然面临同样的问题。会不会是我的相机有问题? ,因为当我使用相同的代码播放视频时,它可以正常工作。 (视频也在第一次之后播放)。
    • 我相信代码应该可以工作。我猜这个问题要么是由于使用了旧版本的opencv,正如其他人所建议的那样;或硬件问题 - 也许相机没有正确释放?相机上是否有任何状态灯可以帮助调试它的状态?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2018-04-17
    • 2014-01-29
    • 2010-11-13
    相关资源
    最近更新 更多