当前位置: 首页 > article >正文

OpenCV视频I/O(18)视频写入类VideoWriter之初始化 VideoWriter 对象的函数open()的使用

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

算法描述

初始化或重新初始化视频编写器。
该方法打开视频编写器。参数与构造函数 VideoWriter::VideoWriter 中的相同。
cv::VideoWriter::open() 函数用于初始化 VideoWriter 对象,使其能够将视频帧写入文件或视频流。

函数原型1


virtual bool cv::VideoWriter::open
(
	const String & 	filename,
	int 	fourcc,
	double 	fps,	
	Size 	frameSize,	
	bool 	isColor = true 
)		

参数1

  • 参数 filename:输出视频文件的路径或名称。
  • 参数 fourcc:四字符代码(FourCC code),用于指定视频编码器。
  • 参数fps:视频的帧率(每秒帧数)。
  • 参数frameSize:视频帧的大小(宽度和高度)。
  • 参数isColor:布尔值,表示视频是否为彩色,默认值为 true(彩色视频)。

函数原型2

这是一个重载的成员函数,提供方便。它与上述函数的不同仅在于接受的参数。


bool cv::VideoWriter::open
(
	const String & 	filename,
	int 	apiPreference,
	int 	fourcc,
	double 	fps,
	Size 	frameSize,
	bool 	isColor = true 
)		

函数原型3

这是一个重载的成员函数,提供方便。它与上述函数的不同仅在于接受的参数。


bool cv::VideoWriter::open	 
(
	const String & 	filename,
	int 	fourcc,
	double 	fps,
	const Size & 	frameSize,
	const std::vector< int > & 	params 
)	

函数原型4

bool cv::VideoWriter::open	
(
	const String & 	filename,
	int 	apiPreference,
	int 	fourcc,
	double 	fps,
	const Size & 	frameSize,
	const std::vector< int > & 	params 
)		

代码示例


#include <iostream>
#include <opencv2/opencv.hpp>
#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 );

    // 检查视频文件是否存在
    std::ifstream file( "output.avi" );
    if ( file.good() )
    {
        std::cout << "Video file created successfully." << std::endl;
    }
    else
    {
        std::cerr << "Failed to create video file." << std::endl;
    }

    // 关闭文件流
    file.close();

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

    return 0;
}

运行结果

Video file created successfully.

http://www.kler.cn/news/337121.html

相关文章:

  • 《大规模语言模型从理论到实践》第一轮学习笔记
  • Mybatis plus快速使用
  • 机器学习框架(含实例说明)
  • 如何用python抓取豆瓣电影TOP250
  • 各省份消费差距(城乡差距)数据(2005-2022年)
  • 【数学二】一元函数微分学-微分的计算
  • Gitea 数据迁移
  • 慢接口分析与优化总结
  • IO系列-3 NIO基本概念:Buffer和Channel和Selector
  • iOS 18.1 將於 2024 年 10 月 28 日發布,並包含 Apple Intelligence 功能
  • @KafkaListener的作用
  • MySQL中NULL值是否会影响索引的使用
  • 操作系统 | 学习笔记 | 王道 | 3.1 内存管理概念
  • 如何配置 Redis 缓存以加速 WordPress:详细教程与实战指南
  • 攀爬数据集,约500张 !VOC格式,yolo可直接使用~真实场景特征明显高清图,yolo可直接使用!
  • 2025秋招LLM大模型多模态面试题(九)-- LoRA 面试问题大全:从理论到实践
  • 基于SpringBoot的音乐网站系统
  • 《Linux从小白到高手》理论篇补充:深入理解Linux中的输入输出及重定向
  • PTA:6-3 圆柱体计算JAVA
  • 国外电商系统开发-运维系统文件上传-快速上传