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;
}
相关推荐
冬奇Lab44 分钟前
每日一个开源项目(第148篇):obsidian-skills - Obsidian CEO 亲写的 AI Agent 格式规范,让 Agent 不再破坏你的 Vault
人工智能·开源·资讯
ethantan1 小时前
AI Agent 组成:像人一样思考的智能体
人工智能·程序员·架构
冬奇Lab1 小时前
Workflow 系列(05):评测体系——三层测试结构与 Trace 追踪
人工智能·工作流引擎
ethantan2 小时前
一篇讲解AI Agent 组成:像人一样思考的智能体
人工智能·后端·程序员
Cosolar4 小时前
vLLM 生产级部署完全指南
人工智能·后端·架构
CodePlayer竟然被占用了4 小时前
被美国政府封杀18天,Claude Fable 5 回来了——但代价是什么?
人工智能
IT_陈寒4 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
smartpi5 小时前
SmartPi GPIO 脉冲与回复语执行时序指南
人工智能
阿里云大数据AI技术5 小时前
PAI支持一键部署GLM-5.2,Coding能力比肩Claude Opus 4.8
人工智能
吾鳴5 小时前
腾讯版贾维斯(Marvis),用过就回不去了
人工智能