自学内容网 自学内容网

OpenCV视频I/O(19)视频写入类VideoWriter之释放 VideoWriter 对象占用的资源函数release()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

关闭视频编写器。
该方法会在后续的 VideoWriter::open 调用和 VideoWriter 析构函数调用时自动调用。
cv::VideoWriter::release() 函数用于释放 VideoWriter 对象占用的资源。

函数原型

virtual void cv::VideoWriter::release()

参数

此函数不接受任何参数

代码示例


#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

int main() {
    // 设置视频的宽度和高度
    int frameWidth = 640;
    int frameHeight = 480;

    // 设置视频编码器的 FourCC 代码
    // 使用 XVID 编码器作为替代方案
    int fourcc = cv::VideoWriter::fourcc('X', 'V', 'I', 'D');

    // 创建 VideoWriter 对象
    cv::VideoWriter writer;

    // 初始化 VideoWriter 对象
    bool isOpened = writer.open("output.avi", fourcc, 25, cv::Size(frameWidth, frameHeight), true);

    if (!isOpened) {
        std::cerr << "Failed to initialize the video writer." << std::endl;
        return -1;
    }

    // 创建一个示例帧
    cv::Mat frame = cv::Mat::zeros(frameHeight, frameWidth, CV_8UC3);

    // 写入一帧到视频文件
    writer.write(frame);

    // 释放资源
    writer.release();

    return 0;
}

原文地址:https://blog.csdn.net/jndingxin/article/details/142746626

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!