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;
}
相关推荐
啾啾Fun9 分钟前
【AI原生组织】6-AI Native团队组建与基础设施搭建
人工智能·chatgpt·ai-native·ai agent·ai原生组织·人机混编
IT_陈寒11 分钟前
Redis集群这个坑,差点让我通宵
前端·人工智能·后端
Elastic 中国社区官方博客11 分钟前
Elasticsearch:使用 AI Agent 来创建 workflow
大数据·运维·人工智能·elasticsearch·搜索引擎·自动化·全文检索
阿图灵24 分钟前
Agentic AI 架构入门(九):Agent 通信协议全景——ACP/A2A/AG-UI/MCP
人工智能·ui·架构·ai agent·智能体·mcp·agentic ai
极创信息26 分钟前
系统安全隐患全方位排查方案:标准化渗透测试全流程
java·opencv·struts·数据挖掘·eclipse·语音识别·hibernate
阿里云大数据AI技术44 分钟前
AI Search+ES 9.4.X最佳实践:“更快、更准、更安全的企业级搜索引擎”"为AI Agent提供坚实底座”
人工智能·elasticsearch·agent
575771 小时前
AI搜索品牌曝光怎么做?从结构化数据到可引用内容的工程路径
人工智能
Hrain-AI1 小时前
2026 编码智能体三强对比:Trae/Qoder CN/CodeBuddy 安全护栏
人工智能·安全
叠层归一研究院1 小时前
AGI 系统(十一):二阶网络 — 二阶递归 × 多主体 (元符号网络)
开发语言·人工智能·算法·php·agi
alwaysrun1 小时前
AI Agent之执行中幻觉问题与应对方案
人工智能·agent