5

我将 OpenCV 2.4.6 与 VS2010 一起使用。

我认为我的网络摄像头无法捕获帧。当我执行它成功构建的代码时,但我没有得到输出。我认为,当我检查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;

}

4

6 回答 6

4

添加这一行cap.retrieve(frame);

在符合之前bool bSuccess = cap.read(frame);

于 2013-09-23T01:47:44.590 回答
3

修复 VideoCapture 失败

在一些 openCV 库中,VideoCapture cap(0);bool bSuccess = cap.read(frame);将首先返回一个0. 所以 for 在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;

}
于 2014-12-28T09:11:42.273 回答
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 - 请参阅此处的答案:Mac 上的 OpenCV 未打开 USB 网络摄像头

于 2013-08-22T22:44:47.067 回答
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;
    }
}
于 2014-11-23T01:32:23.423 回答
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;
}

于 2014-11-06T00:59:02.403 回答
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);

它对我有用。

于 2015-02-28T14:43:56.897 回答