当前位置: 首页 > news >正文

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);

http://www.lryc.cn/news/173984.html

相关文章:

  • ESP32微控制器与open62541库: 详细指南实现OPC UA通信协议_C语言实例
  • 怎样快速打开github.com
  • 【C#】.Net基础语法二
  • C++之this指针总结(二百二十)
  • C++——如何正确的使用STL中的vector?
  • 【C语言】模拟实现内存函数
  • Jenkins学习笔记3
  • 基于单片机火灾报警器仿真设计
  • 阿里测开面试大全(一)附答案完整版
  • STL-常用容器
  • 【owt】关闭microk8s 等无关服务
  • 【面试题】——Spring
  • 【算法思想-排序】根据另一个数组次序排序 - 力扣 1122 题
  • 毕业设计|基于stm32单片机的app视频遥控抽水灭火小车设计
  • 编译原生安卓aosp源码,实现硬改以及定位
  • 找单身狗。一个数组中只有两个数字出现一次,其他数字出现了两次,编写一个函数找出这两个只出现一次的数字
  • Java数据结构技巧
  • easyui disabled 属性设置
  • 使用容器运行Nginx应用及Docker命令
  • fastapi 基本介绍+使用
  • C语言的结构体的认识
  • 只通过在vimrc文件写东西来实现或安装vim的插件
  • 云原生Kubernetes:K8S存储卷
  • “五育”并举育人体系构建的实践研究课题实施方案
  • 小样本目标检测:ECEA: Extensible Co-Existing Attention for Few-Shot Object Detection
  • Android 10.0 系统开启和关闭黑白模式主题功能实现
  • Linux-VI和VIM
  • 【送书】实现可观测性平台的技术要点是什么?
  • AUTOSAR汽车电子嵌入式编程精讲300篇-车载网络 CAN 总线报文异常检测
  • 基于MUSIC算法的二维超声波成像matlab仿真