欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV3视频的分帧并以任意的fps合帧处理

程序员文章站 2022-03-25 14:55:07
...

环境:OpenCV320(3.0以上版本都可以)

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

#include <iostream>

using namespace std;
using namespace cv;

void Image_to_video()
{
	int i = 0;
	Mat img;
	
	cout << "------------- image to video ... ----------------" <<endl;
	int fps = 8;		// or 25
	int frameW = 500;	//原视频的大小宽高
	int frameH = 500;	 
	
	VideoWriter writer("111.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, Size(frameW , frameH ), 1);
	
	while (i<150)
	{
		char image_name[256] = {0};
		sprintf(image_name, ".\\frame\\1 (%d).jpg",i);
		img = imread(image_name,IMREAD_ANYCOLOR);
		if (img.empty())
		{
			cout << "Could not load image file" << endl;
			return ;
		}
		

		cout << i << endl;
		i++;

		imshow("mainWin", img);
		waitKey(30);
		writer << img;
	}
}

void video_to_image()
{
	//视频分帧处理
	VideoCapture cap("123.avi");
	if (!cap.isOpened())
	{
		cout << "未能成功读取视频文件error" << endl;
		return ;
	}
	Mat image;
	int i = 0;
	while (1)
	{
		cap >> image;
		//int nRows = image.rows;				//500
		//int nCols = image.cols;				//500
		//cout << nRows << "," << nCols << endl;
		if (image.empty())
		{
			break;
		}
		char ch1[256] = { 0 };
		sprintf_s(ch1, ".\\frame\\1 (%d).jpg", i);
		i++;//i=150
		imwrite(ch1, image);

	}
}

int main()
{
	video_to_image();//视频转图像并进行保存

	Image_to_video();//通过读取图像按一定的帧率转视频
	
	return 0;
}

 

 

 

相关标签: 分帧合帧