视频读、写

该博客介绍了如何使用OpenCV库在C++中实现视频的读取与写入功能。通过`cv::VideoCapture`读取视频文件,并利用`cv::imshow`显示帧,同时展示了如何利用`cv::VideoWriter`创建新的视频文件。代码示例详细解释了关键步骤,包括获取视频的帧率、分辨率等信息。
摘要由CSDN通过智能技术生成

原创文章点击这里

1 读视频

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

void main()
{
	namedWindow("Example2", WINDOW_AUTOSIZE);
	cv::VideoCapture cap;
	cap.open("H:\\vs2017\\opencv_learning\\ConsoleApplication1\\video.mp4");
	if(!cap.isOpened())
	    return 0;
	//int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);  //帧宽度
	//int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //帧高度
	//int frameRate = cap.get(CV_CAP_PROP_FPS);  //每秒的帧数
	//int totalFrames = cap.get(CV_CAP_PROP_FRAME_COUNT); //总帧数
	cv::Mat frame;
	while (1) {
		cap >> frame;
		if (frame.empty()) break;
		cv::imshow("Exameple2",frame);
		if (cv::waitKey(33) >= 0) break;
	}
	cap.release();
}

2 写视频:

void main()
{
    std::string ori_path = "C:/Users/Administrator/Desktop/3.mp4";
    std::string cur_path = "C:/Users/Administrator/Desktop/5.mp4";

    cv::VideoCapture cap(ori_path);

    int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);  //帧宽度
    int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //帧高度
    int frameRate = cap.get(CV_CAP_PROP_FPS);  // 每秒的帧数

	// 读取 mp4 格式的视频
    cv::VideoWriter new_cap(cur_path, CV_FOURCC('D', 'I', 'V', 'X'), frameRate,
                            cv::Size(width, height), true);
    cv::Mat frame;
    cv::namedWindow("CXK", CV_WINDOW_AUTOSIZE);
    while (1) {
        cap >> frame;
        if (frame.empty()) break;
        new_cap.write(frame); // 写视频
        cv::imshow("Exameple2",frame);
        if (cv::waitKey(1) >= 0) break;
    }
    cap.release();
    new_cap.release();
}

3 视频稳定的方法

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值