您的位置:首页 > 其它

VideoCapture类

2015-11-12 18:32 316 查看
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 camera
if(!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函数,读取失败返回false
bool 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
4000
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;
}
}
在使用这些函数时,了解函数功能,注意函数参数和函数返回值使用起来就能得心应手了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: