【发布时间】: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