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);
        }
	}

GitHub 加速计划 / opencv31 / opencv
162
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
796adf5d Avoid adding value to nullptr 20 小时前
1a6ef7e0 Fixed Android build with Vulkan support. 23 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐