VideoCapture类

来源:互联网 发布:手机免费宅男看片软件 编辑:程序博客网 时间:2024/06/05 03:37
VideoCapture类是OpenCV中从视频文件或摄像头中提取图像帧,获得视频相关属性的一个类;下面详细介绍下这个类及其使用方法。

VideoCapture类的构造函数:

// 这个是默认构造函数VideoCapture::VideoCapture() // 这个构造函数用来处理视频文件,参数为视频文件名VideoCapture::VideoCapture(const string& filename) // 这个构造函数用来从摄像头设备中获取视频流,参数为设备号VideoCapture::VideoCapture(int device)// Example:VideoCapture cap;               // 调用默认构造函数VideoCapture cap(“test.avi”);// 调用第二个构造函数VideoCapture cap(0);            // 调用第三个构造函数
VideoCapture类的成员函数:

1、打开视频文件或视频捕获设备

bool VideoCapture::open(const string& filename) bool VideoCapture::open(int device)// Example:VideoCapture cap;cap.open(“test.avi”);// 打开视频cap.open(0);        // 打开摄像头
2、判断视频是否打开成功

bool VideoCapture::isOpened()// 如果对构造函数或open函数的调用成功,返回true// Example:VideoCapture cap(0); // open the default cameraif(!cap.isOpened())  // check if we succeeded    return -1;
3、关闭视频或设备,由析构函数自动调用

void VideoCapture::release()
4、捕获下一帧解码并返回视频帧

bool VideoCapture::grab()bool VideoCapture::retrieve(Mat& image, int channel=0)// 这两个一般放在一起连用
5、捕获下一帧解码并返回

// 通过重载的操作符 >>VideoCapture& VideoCapture::operator>>(Mat& image) // 使用read函数,读取失败返回falsebool VideoCapture::read(Mat& image)
6、获取视频属性:

double VideoCapture::get(int propId)// propId有如下选项:CV_CAP_PROP_POS_MSEC        Current position of the video file in milliseconds or video capture timestamp. CV_CAP_PROP_POS_FRAMES      0-based index of the frame to be decoded/captured next. CV_CAP_PROP_POS_AVI_RATIO   Relative position of the video file: 0 - start of the film, 1 - end of the film. CV_CAP_PROP_FRAME_WIDTH     Width of the frames in the video stream. CV_CAP_PROP_FRAME_HEIGHT    Height of the frames in the video stream. CV_CAP_PROP_FPS             Frame rate. CV_CAP_PROP_FOURCC          4-character code of codec. CV_CAP_PROP_FRAME_COUNT     Number of frames in the video file. CV_CAP_PROP_FORMAT          Format of the Mat objects returned by retrieve() . CV_CAP_PROP_MODE            Backend-specific value indicating the current capture mode. CV_CAP_PROP_BRIGHTNESS      Brightness of the image (only for cameras). CV_CAP_PROP_CONTRAST        Contrast of the image (only for cameras). CV_CAP_PROP_SATURATION      Saturation of the image (only for cameras). CV_CAP_PROP_HUE             Hue of the image (only for cameras). CV_CAP_PROP_GAIN            Gain of the image (only for cameras). CV_CAP_PROP_EXPOSURE        Exposure (only for cameras). CV_CAP_PROP_CONVERT_RGB     Boolean flags indicating whether images should be converted to RGB. CV_CAP_PROP_WHITE_BALANCE   Currently not supported CV_CAP_PROP_RECTIFICATION   Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)  
7、设置视频属性

bool VideoCapture::set(int propId, double value);// Example:// 调至第100帧double position = 100.0;cap.set(CV_CAP_PROP_POS_FRAMES,position);
下面通过一个简单的例子加深对这些函数的了解:
// OpenCV 2.4.11#include<opencv2/highgui/highgui.hpp>#include<opencv2/video/video.hpp>using namespace cv;int main(){VideoCapture cap("test.avi");// 另一种方法:// VideoCapture cap;// cap.open("test.avi");// 判断视频视频打开成功if (!cap.isOpened())return -1;// 获取帧速率,计算两帧时间间隔double rate = cap.get(CV_CAP_PROP_FPS);int delay = 1000 / rate;namedWindow("Video");Mat frame;while (1){cap >> frame;if (!frame.data)break;// 或者// if(!cap.read(frame))//break;// 或者// cap.grab();// cap.retrieve(*frame);imshow("Video", frame);if (waitKey(delay) >= 0)break;}}
在使用这些函数时,了解函数功能,注意函数参数和函数返回值使用起来就能得心应手了。

0 0
原创粉丝点击