OpenCV视频I/O(20)视频写入类VideoWriter之用于将图像帧写入视频文件函数write()的使用

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

算法描述

cv::VideoWriter::write() 函数用于将图像帧写入视频文件。

该函数/方法将指定的图像写入视频文件。图像的大小必须与打开视频编写器时指定的大小相同。

函数原型

cpp 复制代码
virtual void cv::VideoWriter::write
(
	InputArray 	image
)	

参数

  • 参数image 被写入的帧。一般来说,期望的是 BGR 格式的彩色图像。

代码示例

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

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

    // 再次创建一个不同的帧
    cv::Mat anotherFrame = cv::Mat::ones( frameHeight, frameWidth, CV_8UC3 ) * 255;

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

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

    return 0;
}
相关推荐
sleven fung几秒前
Whisper库
开发语言·人工智能·python·算法·ai·whisper
Agilex松灵机器人6 分钟前
什么是具身智能底盘?4 类主流 AI 机器人底盘选型|VLA/ROS2 项目硬件指南
人工智能·机器人·具身智能·vla·aloha·松灵科研案例
vvzh20268 分钟前
南京区域 GEO 优化落地周期与 AI 收录规律详解(豆包、DeepSeek 适配指南)
人工智能
Black蜡笔小新11 分钟前
自动化AI算法训练服务器DLTM零代码私有化一站式AI训练平台技术解析
人工智能·算法·自动化
Bruce_Liuxiaowei11 分钟前
高考护航AI押题虚假宣传_AI滥用风险民生场景深度分析
人工智能·安全·ai·高考·智能体
vvzh202618 分钟前
2026南京初创企业GEO知识库落地思路:一站式结构化知识库搭建落地参考
人工智能
澄旭26 分钟前
一篇讲清 Agent Skills:把经验变成可调用的能力
人工智能
合合技术团队41 分钟前
2026中国图像图形大会召开,合合信息推出多模态可信AI鉴伪系统
图像处理·人工智能·aigc鉴伪
俊哥V41 分钟前
AI一周事件 · 2026-05-27 至 2026-06-02
人工智能·ai
G***技41 分钟前
极寒也能跑AI?LM2-100-V0算力模组为电网巡检终端注入AI动能
人工智能