【问题标题】:Capturing video from a camera从相机捕捉视频
【发布时间】:2014-05-04 23:10:12
【问题描述】:

我正在使用 Macbook Air 2013 参加 OSX Mavericks。

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

int main()
{
   cv::VideoCapture cap;
   cap.open(0);

   if( !cap.isOpened() )
   {
       std::cerr << "***Could not initialize capturing...***\n";
       return -1;
   }

   cv::Mat frame;

   while(1){
       cap >> frame;

       if(frame.empty()){
           std::cerr<<"frame is empty"<<std::endl;
           break;
       }

       cv::imshow("", frame);
       cv::waitKey(10);
   }

   return 1;
}

相机正确初始化(isOpened 返回 true),但它不断返回空帧。但是,从文件而不是相机中检索帧可以正常工作。

此外,使用 C API 的 cvQueryFrame 似乎工作正常!

关于如何调试我的问题有什么想法吗?

编辑: 下面的代码似乎可以让相机正常工作。有人知道为什么吗?

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap;
    cap.open(0);
    namedWindow("Window");

    if( !cap.isOpened() )
    {
        std::cerr << "***Could not initialize capturing...***\n";
        return -1;
    }

    cv::Mat frame;

    while(1){
        cap >> frame;

        if(!(frame.empty())){
            imshow("Window", frame);
        }

        waitKey(10);
    }

    return 1;
}

【问题讨论】:

  • 对我来说没问题。你的相机在运行时打开成功了吗?
  • @herohuyongtao 好吧,我不确定。 cap.isOpened() 返回 true,但是我的相机灯没有打开。这说明什么?
  • 您使用的是哪个 OpenCV 版本?视频通话时可以打开摄像头吗?
  • @herohuyongtao 我正在使用 OpenCV 2.4.9。当我进行视频通话时,我的摄像头工作正常。
  • 看看this solution是否有帮助。

标签: c++ opencv osx-mavericks


【解决方案1】:

这与我在笔记本电脑上使用 OpenCv 时遇到的问题相同。

只需在while 循环之前添加cvWaitKey(6700);

代码:

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap;
    cap.open(0);
    namedWindow("Window");

    if( !cap.isOpened() )
    {
        std::cerr << "***Could not initialize capturing...***\n";
        return -1;
    }

    cv::Mat frame;

    waitKey(6700);

    while(1){
        cap >> frame;

        if(!(frame.empty())){
            imshow("Window", frame);
        }

        waitKey(25);
    }

    return 1;
}

希望它应该工作。

【讨论】:

  • 嗯。 waitKey(6700) 没有任何区别。不管它是否存在,我的代码只有在有“namedWindow()”调用时才有效。
【解决方案2】:

试试

while(cap.grab()){

        cap.retrieve(frame);
        waitKey(25);
    }

它不会给你空帧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多