opencv 写入 mp4 文件 C++用VideoWriter写入视频
C++用VideoWriter写入视频warnning:要用resize转换大小.#include <iostream>#include <stdio.h>#include <opencv2/opencv.hpp>using namespace cv;using namespace std;int main() {string img_name = "/*/*.
·
C++用VideoWriter写入视频
warnning:要用resize转换大小.
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
string img_name = "/*/*.png";
string save_path = "*.avi";
cv::Mat img = cv::imread(img_name);
cv::VideoWriter track_writer;
track_writer.open (save_path.c_str(), cv::VideoWriter::fourcc('M', 'P', '4', '2'), 30.0, cv::Size(960, 540));
if(!track_writer.isOpened())
{
assert("track writer open failed!\n");
}
for (int i = 0; i < 100; ++i)
{
cv::imshow("test", img);
cv::resize(img, img, cv::Size(960, 540));
track_writer.write(img);
cout << "frame" << i << endl;
}
return 0;
}
opencv读取视频时跳帧处理,grab只缓存帧数据,retrieve做对应的解码
cv::Mat frame;
while (true) {
for (size_t i = 0; i < 100; i++)
{
cap.grab();
}
bool grabbed = cap.grab();
if (!grabbed) {
std::cout << "Error: Video stream ended or unable to grab the frame." << std::endl;
continue;
}
// 解码图像数据并获取图像
int ret = cap.retrieve(frame);
if (ret){
cv::imshow("frame", frame);
cv::waitKey(0);
}
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)