我在VS2010上使用OpenCV 2.4.6。
我认为我的网络摄像头无法捕捉到框架。当我执行它成功构建的代码时,但我没有得到输出。我想,当我检查if(!bSuccess)
时,它已被执行,无法从网络摄像头捕获帧。
如何解决此问题?我的代码如下:
#include "opencv2/highgui/highgui.hpp"
#include
using namespace cv;
using namespace std;
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"
while (1)
{
Mat frame;
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)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
答案 0 :(得分:4)
添加此行cap.retrieve(frame);
在与bool bSuccess = cap.read(frame);
答案 1 :(得分:3)
在某些openCV库中,VideoCapture cap(0);bool bSuccess = cap.read(frame);
将首先返回0
。因此,对于while(1)
循环,它将在第一次迭代时失败。因此,您需要在进入无限循环之前运行cap.read(frame);
行一次
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
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 cam" << 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;
cap.read(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 stream" << 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;
}
答案 2 :(得分: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"
或者在获取或设置相机属性之前尝试从相机获取至少一帧 - 我认为在获得第一帧之前,在opencv中的Windows相机尚未完全优化。
或者您可以尝试使用不同的API - 请参阅此处的回答:OpenCV on Mac is not opening USB web camera
答案 3 :(得分:0)
不仅是第一个,而且可以跳过许多其他帧; - )
opencv 2.4.9 视觉工作室10
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture webcam_0(0); //open stream
if(!webcam_0.isOpened())
{
cout << "error: cannot open stream between webcam_0 and application" << endl;
waitKey(0);
return -1;
}
Mat frame; //Mat header for frame storing from webcam_0
int i = 0; //index, we use it for testing
while ((i++ < 100) && !webcam_0.read(frame)) //skip unread frames
{
cout << "frame " << i << " skipped" << endl;
}
if (i >= 100) //check webcam_0 failure
{
cout << "cannot read frames from webcam_0, check drivers" << endl;
waitKey(0);
return -1;
} else
{
cout << "cam is ready" << endl;
}
char * window_name = "webcam_0 test; press \"ESC\" to exit";
namedWindow(window_name, CV_WINDOW_AUTOSIZE);//make new window
imshow(window_name, frame); //output 1st successfully read frame
waitKey(10);
while(1) //reading frames from webcam_0
{
if(!webcam_0.read(frame))
{
cout << "cannot get frame from webcam_0, check drivers" << endl;
waitKey(0);
return -1;
}
imshow(window_name, frame);
if (waitKey(10) == 27) //check if ESC is pressed
{
cout << "bye!" << endl;
waitKey(0);
break;
}
}
return 0;
}
&#13;
答案 4 :(得分:0)
尝试以下代码:
#include "stdafx.h"
#include "highgui\highgui.hpp"
using namespace cv;
void main()
{
Mat Frame;
VideoCapture cap(0); // change the number to 1 for an external USB webcam
while(1)
{
cap >> Frame;
imshow("Camera Feed", Frame);
if (waitKey(10) == 27) return;
}
}
答案 5 :(得分:0)
在与下述类似的所有情况下删除“cv_
”:
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);
它对我有用。