VideoCapture类
2015-11-12 18:32
316 查看
VideoCapture类是OpenCV中从视频文件或摄像头中提取图像帧,获得视频相关属性的一个类;下面详细介绍下这个类及其使用方法。
VideoCapture类的构造函数:
1、打开视频文件或视频捕获设备
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; } }在使用这些函数时,了解函数功能,注意函数参数和函数返回值使用起来就能得心应手了。
相关文章推荐
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系
- JSON和数据集互相转换单元
- MongoDB windows系统安装
- hdu 1806 Frequent values rmq+游标编码
- iOS 定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等授权检测
- Xilinx Spartan6 系列产品(FPGA+DSP+ARM
- 日报2015/11/12(第一行代码读书笔记)
- 一个时代的落幕
- 网站性能测试工具
- gets()函数的应用
- easyui datagrid 前后台交互
- ASP.NET两个常见的异常 如未能创建 Mutex
- 修改LR自带的示例程序端口号,Cannot create GUI process-program not found
- DB2数据库常见问题汇总
- ssh密钥认证原理
- 批量替换和删除
- Nginx反向代理https
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- hdu2031
- vmdk repair