从摄像机捕捉视频

Capturing video from a camera

本文关键字:视频 摄像机      更新时间:2023-10-16

我用的是OSX Mavericks和Macbook Air 2013。

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

相机初始化正确(isopen返回true),但是它一直返回空帧。但是,从文件中检索帧而不是从相机中检索帧效果很好。

此外,使用C API的cvQueryFrame似乎工作得很好!

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

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

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

这是我在笔记本电脑上使用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;
}

Try

while(cap.grab()){
        cap.retrieve(frame);
        waitKey(25);
    }

它不会给你空的框架