OpenCV — VideoCapture

OpenCV 之 VideoCapture类


VideoCapture 类提供了C++ API,主要用来处理视频文件、图像序列或者摄像机实时拍摄的画面。 本文是作者学了该类的笔记。


头文件

该类放置在头文件 videoio.hpp 中,因此在使用该类之前,通常需要包含该头文件。

#include “videoio.hpp”

类的成员组成

//public:
        VideoCapture();//默认的构造函数 
        VideoCapture (const String &filename);//打开视频文件或者摄像设备或者IP视频流

        VideoCapture (const String &filename, int apiPreference);//使用API来打开视频文件或者摄像设备或者IP视频流
                参数 filename:
                   - 视频文件的名称(eg. 1.avi)
                   - 图像序列(eg. img_%02d.jpg),这会读取相似文件名的所有图片(img_00.jpg, img_01.jpg, img_02.jpg, ...)
                   - 链接或者视频流 (eg. protocol://host:port/script_name?script_params|auth)

                参数 apiPreference:   e.g. cv::CAP_FFMPEG 或 cv::CAP_IMAGES 或 cv::CAP_DSHOW.

        VideoCapture (int index);//打开摄像机
               参数 index: camera_id + domain_offset (CAP_*)

virtual ~VideoCapture ();//析构函数

virtual double  get (int propId) const;//获取视频的一些属性 

virtual bool    grab ();// 提取视频的下一帧

virtual bool    isOpened () const;// 确认视频捕捉已初始化

virtual bool    open (const String &filename);//打开视频文件或者摄像设备或者IP视频流

virtual bool    open (int index);//打开摄像机

bool    open (int cameraNum, int apiPreference);//打开摄像机

virtual bool    open (const String &filename, int apiPreference);//使用API来打开视频文件或者摄像设备或者IP视频流

virtual VideoCapture &  operator>> (Mat &image);// 使用流运算符来读取视频下一帧;

virtual VideoCapture &  operator>> (UMat &image);// 使用流运算符来读取视频下一帧;

virtual bool    read (OutputArray image);// 提取、解码并返回视频下一帧

virtual void    release ();// 关闭视频或捕捉设备

virtual bool    retrieve (OutputArray image, int flag=0);//解码并返回所提取的视频帧

virtual bool    set (int propId, double value);// 设置属性

//proteced

Ptr< CvCapture >    cap;

Ptr< IVideoCapture >    icap;

实例

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**)
{
     Mat frame;
     //--- INITIALIZE VIDEOCAPTURE
     VideoCapture cap;
     // open the default camera using default API
        cap.open(0);
     // OR advance usage: select any API backend
     int deviceID = 0;             // 0 = open default camera
     int apiID = cv::CAP_ANY;      // 0 = autodetect default API
     // open selected camera using selected API
        cap.open(deviceID + apiID);
     // check if we succeeded
     if (!cap.isOpened()) {
            cerr << "ERROR! Unable to open camera\n";
     return -1;
        }
     //--- GRAB AND WRITE LOOP
        cout << "Start grabbing" << endl
            << "Press any key to terminate" << endl;
     for (;;)
        {
     // wait for a new frame from camera and store it into 'frame'
            cap.read(frame);
     // check if we succeeded
     if (frame.empty()) {
                cerr << "ERROR! blank frame grabbed\n";
     break;
            }
     // show live and wait for a key with timeout long enough to show images
     imshow("Live", frame);
     if (waitKey(5) >= 0)
     break;
        }
     // the camera will be deinitialized automatically in VideoCapture destructor
     return 0;
}

Reference

[1] cv::VideoCapture Class Reference

猜你喜欢

转载自blog.csdn.net/xholes/article/details/79725953