• OpenCV之VideoCapture


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

    头文件:

    #include <opencv2/video.hpp>

    主要函数如下:

    构造函数

    1. C++: VideoCapture::VideoCapture();
    2. C++: VideoCapture::VideoCapture(const string& filename);
    3. C++: VideoCapture::VideoCapture(int device);

    参数:
    filename – 打开的视频文件名。

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

    基本功能

    打开视频文件或者设备

    1. C++: bool VideoCapture::open(const string& filename);
    2. C++: bool VideoCapture::open(int device);

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

    参数: 
    filename – 打开的视频文件名。
    device – 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。

    判断打开是否成功

    C++: bool VideoCapture::isOpened();

    成功返回true,,否则false.

    关闭视频文件或者摄像头

    C++: void VideoCapture::release();

    抓取下一帧

    1. C++: bool VideoCapture::grab();//需与retrieve结合使用
    2. C++: bool VideoCapture::retrieve(Mat& image, int channel=0);
    3. C++: VideoCapture& VideoCapture::operator>>(Mat& image);
    4. C++: bool VideoCapture::read(Mat& image);

    获取视频属性

    C++: double VideoCapture::get(int propId);

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

    参数:属性的ID。

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

    1. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
    2. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
    3. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
    4. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
    5. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
    6. CV_CAP_PROP_FPS Frame rate.
    7. CV_CAP_PROP_FOURCC 4-character code of codec.
    8. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
    9. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
    10. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
    11. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
    12. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
    13. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
    14. CV_CAP_PROP_HUE Hue of the image (only for cameras).
    15. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
    16. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
    17. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
    18. CV_CAP_PROP_WHITE_BALANCE Currently not supported
    19. 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如下:

    1. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
    2. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
    3. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
    4. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
    5. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
    6. CV_CAP_PROP_FPS Frame rate.
    7. CV_CAP_PROP_FOURCC 4-character code of codec.
    8. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
    9. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
    10. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
    11. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
    12. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
    13. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
    14. CV_CAP_PROP_HUE Hue of the image (only for cameras).
    15. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
    16. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
    17. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
    18. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
    19. 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 读取帧

    1. Mat frame;
    2. if (capture.read(frame))
    3. imwrite("d:/a.bmp",frame);

  • 相关阅读:
    抖音播映量破500的原因找到了,不是内容不好,而是这5个功能
    高薪程序员&面试题精讲系列131之Eureka如何实现高可用?自我保护机制是怎么回事?
    Docker从入门到精通|2022版
    (针对 G1 )JVM常用参数讲解:-XX,-X,-D 的区别与常用JVM配置 +应用程序配置实例+ 调优思路
    NPDP产品经理国际认证报名有什么要求?
    每日刷题|贪心算法初识
    腾讯云GPU服务器详细介绍_GPU工智能_深度学习全解析
    世界杯中隐藏的IoT物联网黑科技
    自制肥鲨HDO2电源升压延长线
    vmstat-内存性能分析常用命令
  • 原文地址:https://blog.csdn.net/hulinhulin/article/details/133231781