【问题标题】:OpenCV can't capture the frame from the webcamOpenCV 无法从网络摄像头捕获帧
【发布时间】:2013-08-22 17:16:53
【问题描述】:

我在 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 &lt&lt "Cannot open the video file" &lt&lt 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 &lt&lt "Frame size : " &lt&lt dWidth &lt&lt " x " &lt&lt dHeight &lt&lt 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 &lt&lt "Cannot read a frame from video file" &lt&lt endl; break; } imshow("MyVideo", frame); //show the frame in "MyVideo" window if (waitKey(30) == 27) { cout &lt&lt "esc key is pressed by user" &lt&lt endl; break; } } return 0; }

【问题讨论】:

  • 2.4.6 中存在一些关于从网络摄像头捕获图像的问题。您可能想尝试他们已修复该问题的 2.4.6.1。

标签: c++ opencv


【解决方案1】:

添加此行cap.retrieve(frame);

bool bSuccess = cap.read(frame);行之前

【讨论】:

    【解决方案2】:

    修复 VideoCapture 失败

    在一些 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;
    
    }
    

    【讨论】:

      【解决方案3】:

      尝试不使用这部分代码:

      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

      【讨论】:

      • 您好,亲爱的,感谢您的回答。我试过没有上面的部分。但没有得到任何结果。
      【解决方案4】:

      不仅是第一帧,还可以跳过许多其他帧;-)

      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;
      }

      【讨论】:

        【解决方案5】:

        试试下面的代码:

        #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;
            }
        }
        

        【讨论】:

          【解决方案6】:

          在所有类似于下面提到的情况下删除“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);
          

          它对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-18
            • 1970-01-01
            • 1970-01-01
            • 2012-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-03
            相关资源
            最近更新 更多