OpenCV之VideoCapture

VideoCaptrue类对视频进行读取操作以及调用摄像头。

头文件:

复制代码
#include <opencv2/video.hpp>

主要函数如下:

构造函数

复制代码
C++: VideoCapture::VideoCapture();
C++: VideoCapture::VideoCapture(const string& filename);
C++: VideoCapture::VideoCapture(int device);

参数:

filename -- 打开的视频文件名。

device -- 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。

基本功能

打开视频文件或者设备

复制代码
C++: bool VideoCapture::open(const string& filename);
C++: bool VideoCapture::open(int device);

打开一个视频文件或者打开一个捕获视频的设备(也就是摄像头)

参数:

filename -- 打开的视频文件名。

device -- 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。

判断打开是否成功

复制代码
C++: bool VideoCapture::isOpened();

成功返回true,,否则false.

关闭视频文件或者摄像头

复制代码
C++: void VideoCapture::release();

抓取下一帧

复制代码
C++: bool VideoCapture::grab();//需与retrieve结合使用
C++: bool VideoCapture::retrieve(Mat& image, int channel=0);
C++: VideoCapture& VideoCapture::operator>>(Mat& image);
C++: bool VideoCapture::read(Mat& image);

获取视频属性

复制代码
C++: double VideoCapture::get(int propId);

如果属性不支持,将会返回0。

参数:属性的ID。

属性的ID可以是下面的之一:

复制代码
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)

设置属性

复制代码
bool VideoCapture::set(int propertyId, double value)

成功返回true,否则返回false

参数:第一个是属性ID,第二个是该属性要设置的值。

属性ID如下:

复制代码
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
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 unsupported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

实例:获取任意一帧

1 实例及初始化:

复制代码
cv::VideoCapture capture.open("Old Film Effect.mp4");

2 设置需要读取帧的位置:

复制代码
capture.set(cv::CAP_PROP_POS_FRAMES,10);//设置读取第10帧

3 读取帧

复制代码
Mat frame;
if (capture.read(frame))
   imwrite("d:/a.bmp",frame);
相关推荐
JT8583967 分钟前
AI GEO 优化能否快速提升网站在搜索引擎的排名?
人工智能·搜索引擎
幂律智能9 分钟前
吾律——让普惠法律服务走进生活
人工智能·经验分享
IT_陈寒13 分钟前
Java性能优化:从这8个关键指标开始,让你的应用提速50%
前端·人工智能·后端
yzx99101317 分钟前
构建未来:深度学习、嵌入式与安卓开发的融合创新之路
android·人工智能·深度学习
非门由也28 分钟前
《sklearn机器学习——特征提取》
人工智能·机器学习·sklearn
机器学习之心1 小时前
基于CNN的航空发动机剩余寿命预测 (MATLAB实现)
人工智能·matlab·cnn
钝挫力PROGRAMER1 小时前
AI中的“预训练”是什么意思
人工智能
Godspeed Zhao2 小时前
自动驾驶中的传感器技术39——Radar(0)
人工智能·机器学习·自动驾驶·毫米波雷达
idealmu3 小时前
知识蒸馏(KD)详解一:认识一下BERT 模型
人工智能·深度学习·bert
Cathyqiii3 小时前
生成对抗网络(GAN)
人工智能·深度学习·计算机视觉