九、OpenCV中视频的录制 - 指南
2025-10-22 20:20 tlnshuju 阅读(0) 评论(0) 收藏 举报文章目录
- 一、OpenCV 视频录制概述
- 二、示例代码
一、OpenCV 视频录制概述
在 OpenCV 中,视频录制主要靠 cv::VideoWriter 类。它可以把连续的 cv::Mat 帧写入一个视频文件(如 .avi、.mp4 等)。
VideoWriter类
构造函数:
VideoWriter::VideoWriter(const String &filename,
int fourcc,
double fps,
Size frameSize,
bool isColor = true);
参数说明:
- filename:保存的视频文件名,例如 “output.avi”
- fourcc:视频编码方式,如 CV_FOURCC(‘M’,‘J’,‘P’,‘G’) 或 VideoWriter::fourcc(‘M’,‘J’,‘P’,‘G’)
- fps:帧率(帧每秒)
- frameSize:视频每帧的尺寸,Size(width, height)
- isColor:是否为彩色视频,true 彩色,false 灰度
常用编码(fourcc):
int fourcc = VideoWriter::fourcc('M','J','P','G');
// AVI 常用
int fourcc_mp4 = VideoWriter::fourcc('a','v','c','1');
// MP4 H264
二、示例代码
示例 1:从摄像头录制视频
#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;int main() {VideoCapture cap(0);// 打开摄像头if (!cap.isOpened()) {cout <<"Cannot open camera" << endl;return -1;}// 视频参数int frame_width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));int frame_height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));VideoWriter writer("output.avi",VideoWriter::fourcc('M','J','P','G'),30, Size(frame_width, frame_height));Mat frame;while (true) {cap >> frame;if (frame.empty()) break;writer.write(frame);// 写入视频imshow("Camera", frame);if (waitKey(1) == 27) break;// 按 ESC 停止}cap.release();writer.release();destroyAllWindows();return 0;}
示例 2:将视频文件处理后保存
#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;int main() {VideoCapture cap("opencv_demo.mp4");if (!cap.isOpened()) return -1;int width = cap.get(CAP_PROP_FRAME_WIDTH);int height = cap.get(CAP_PROP_FRAME_HEIGHT);VideoWriter writer("output_processed.avi",VideoWriter::fourcc('M', 'J', 'P', 'G'),30, Size(width, height));Mat frame;while (true) {cap >> frame;if (frame.empty()) break;// 处理帧,例如灰度cvtColor(frame, frame, COLOR_BGR2GRAY);cvtColor(frame, frame, COLOR_GRAY2BGR);// 保持三通道以写入writer.write(frame);}cap.release();writer.release();return 0;}
示例3:自动读取指定文件夹下所有图片生成视频
#include <opencv2/opencv.hpp>#include <iostream>#include <filesystem>#include <vector>#include <algorithm>using namespace cv;using namespace std;namespace fs = std::filesystem;int main() {string folderPath = "images";// 图片文件夹路径string outputVideo = "output_video.avi";double fps = 30.0;// 帧率vector<string> imgFiles;// 遍历文件夹,收集图片路径for (const auto& entry : fs::directory_iterator(folderPath)) {if (entry.is_regular_file()) {string ext = entry.path().extension().string();// 支持 jpg/png/bmpif (ext == ".jpg" || ext == ".png" || ext == ".bmp")imgFiles.push_back(entry.path().string());}}if (imgFiles.empty()) {cout <<"No images found in folder!" << endl;return -1;}// 按文件名排序sort(imgFiles.begin(), imgFiles.end());// 读取第一张图片获取尺寸Mat firstImg = imread(imgFiles[0]);if (firstImg.empty()) {cout <<"Failed to read " << imgFiles[0] << endl;return -1;}Size frameSize(firstImg.cols, firstImg.rows);// 创建 VideoWriterVideoWriter writer(outputVideo,VideoWriter::fourcc('M','J','P','G'),fps,frameSize,true);if (!writer.isOpened()) {cout <<"Cannot open VideoWriter!" << endl;return -1;}// 写入每张图片for (const auto& file : imgFiles) {Mat img = imread(file);if (img.empty()) {cout <<"Failed to read " << file << endl;continue;}// 尺寸不一致就 resizeif (img.size() != frameSize) {resize(img, img, frameSize);}writer.write(img);}writer.release();cout <<"Video saved as " << outputVideo << endl;return 0;}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/943710.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!相关文章
高端网站设计中的微交互:细节如何决定用户体验 - 指南
高端网站设计中的微交互:细节如何决定用户体验 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&q…
股票操作统计分析报告 - 2025-10-22
股票操作统计分析报告 - 2025-10-22生成时间: 2025-10-22 20:17:44数据来源: 9db.com股票操作统计分析报告数据统计时间: 2025-10-22
数据来源: 9db.com建仓次数最多的股票: 长江电力 (共2次)建仓详细统计 (前10名):1.…
2025年完整指南:DeepSeek OCR 如何通过“视觉压缩”将AI成本降低20倍?
2025年完整指南:DeepSeek OCR 如何通过“视觉压缩”将AI成本降低20倍?DeepSeek OCR 并非传统的光学字符识别工具,而是一种探索“视觉-文本压缩”的前沿AI模型。它通过将长文本转换成图像进行处理,极大地降低了计算…
LGP5494 [LG TPLT] 线段树分裂 学习笔记
LGP5494 [LG TPLT] 线段树分裂 学习笔记
\(\texttt{Luogu Link}\)
前言\(\texttt{Q:}\) 有什么数据结构是支持用合并&分裂查询答案信息的呢?
\(\texttt{A:}\) \(\text{FHQ-Treap}\)。
\(\texttt{Q:}\) 还有吗?
\…
k8s 常用命令 - 实践
pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …
文档智能信息抽取技术在金融财税领域的应用研究与发展前景
在金融与财税这个由海量文档驱动的领域中,效率与准确性是生命线。从繁复的财务报表、五花八门的发票,到冗长的合同与合规文件,传统的人工处理方式不仅成本高昂、效率低下,还极易出错。随着人工智能技术的成熟,文档…
今日策略:年化436%,回撤7%,夏普比5.28, deap因子挖掘重构,附python代码 - 详解
今日策略:年化436%,回撤7%,夏普比5.28, deap因子挖掘重构,附python代码 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; fo…
申威架构ky10安装php-7.2.10.rpm详细步骤(国产麒麟系统64位)
申威架构ky10安装php-7.2.10.rpm详细步骤(国产麒麟系统64位)1. 先确认系统环境你用的应该是申威平台的银河麒麟系统(版本ky10),系统得是64位的(因为这rpm包是_64的)。打开终端(就是黑框框),先敲命令看…
Unity 虚拟仿真实验中设计模式的使用 ——策略模式(Strategy Pattern) - 指南
pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …
vue2:v-if和v-show的区别以及造成的影响
<div class="form-box-4" v-show="checkedList.includes(0505)"><span class="form-box-4-label"><span style="color: red">*</span>三失一偏类型:…
office2024绿色精简版
一、简介
首先需要说明的是,目前的安装方法只适合Windows10、11,并且只支持64位的操作系统,这点需要大家注意。大家下载压缩包解压后,在文件夹中会得到三个运行程序,分别是dll修复程序、和office残余文件彻底清理…
51单片机实践之数码管电子时钟/时间呈现及其设置
51单片机实践之数码管电子时钟/时间呈现及其设置pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", …
LGP3694 邦邦的大合唱站队 学习笔记
LGP3694 邦邦的大合唱站队 学习笔记
\(\texttt{Luogu Link}\)
前言
状压热身题。\(\texttt{Warm up!}\)
另外,你知道吗,设定上,邦邦已经火了……
题意简述
\(n\) 个偶像排成一列,他们来自 \(m\) 个不同的乐队。每个…
2025.10.22学习记录
2025.10.22课程总结
本次学习聚焦 设计规范、查询技术、高级功能 三大模块,目标是掌握从表结构设计到复杂数据查询的全流程能力,为后续数据分析、系统开发奠定基础。
范式理论
核心观点:范式是电商数据结构化的基础…
LeeCode_101对称二叉树
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:输入:root = [1,2,2,3,4,4,3]
输出:true
class Solution {
public:bool isSameTree(TreeNode* p, TreeNode* q){if(p == nullptr || q == nullptr){retu…
TRAE 设计团队如何玩转 Vibe Coding(上)|高美感页面生成篇
资料来源:火山引擎-开发者社区本内容分为上下两篇,主要和大家分享 TRAE 设计团队基于 TRAE 工具的 Vibe Coding 探索实践,包括三类场景的搭建和价值收益的案例,帮助设计师探索如何通过 Vibe Coding 放大设计价值 /…
详细介绍:观察者模式(Observer Pattern)定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。
详细介绍:观察者模式(Observer Pattern)定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。2025-10-22 19:58
tlnshuju
阅读(0)
评论(0) 收藏
举报pre …
LeeCode_226反转二叉树
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
示例 1:输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
//法一:
class Solution {
public:TreeNode* invertTree(TreeNode* root){if(root =…