OpenCV image sequence generates video, MATLAB image generates avi video, image2video.

Explain how to use OpenCV to synthesize avi format video.

In the process of processing images and videos, it is usually encountered converting the video into image processing, or synthesizing the processed image into the original video. In the process of video-based processing, the video is usually converted into each frame of image first, and then processed based on the image. OpenCV has a lot of related algorithm codes about Video2Image. Note that during the video conversion process, as long as OpenCV is configured, it can be converted to an image under normal conditions. Do not rely on other libraries, do not rely on third-party libraries.

In the process of converting an image into a video, you need to save the image in a folder, then use the videowriter class from OpenCV to add a frame of images, and finally synthesize an avi video. Note that OpenCV can only synthesize videos in avi format. Follow the usual steps to synthesize the video. However, you can see at this time that the video file is generated, but the size is 0kb. The algorithm will not report an error, but the video is not generated. Note that this is a lack of third-party libraries, as shown in the figure below. no message.

The above is a video generated with OpenCV.

 

As you can see from this, there is no information. It means that it was not generated successfully.

Through various searches, the two dll files, opencv_ffmpeg247.dll and opencv_ffmpeg247_64.dll, need to be placed under the project directory and under the exe file. As shown below. You can download these two files at this link. You can change the name of the supported version yourself. For example, opencv3.1.0 will change the dll to opencv_ffmpeg310.dll.  opencv_ffmpeg247 image to video library download. Based on OpenCV.

Now you can see that the generated file has a size and is ready for use. But from the final result, although there is specific size information, there is no frame rate, pixel size and other information in the detailed information. Many mobile phones can't see it yet, and can only be viewed with the help of a player, such as qq video.

The specific code is as follows:

    //Create a video writing class, the format is set to MJPG, there are other formats, you can change it yourself;
    //Note that the suffix can only be avi; the frame rate can be set by yourself, set to 50fps, or 80. You can modify it yourself.

    //The size should be consistent with the original image as much as possible.

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/core/core.hpp>

using namespace std;
using namespace cv;

int main(int argc,char **argv)
{
	//创建一个写视频类,格式设定为MJPG,还有其他格式,自己可以改;
	//注意,后缀只能是avi;
	VideoWriter writer("E:\\high_speed_vision\\rereslut_50fps.avi", CV_FOURCC('M', 'J', 'P', 'G'), 50, Size(512,512),1);
	//int i=0;
	int count=0;
	for (int i = 0; i <172; i++)
	{
		//读取原始图像;
		Mat src=imread("E:\\high_speed_vision\\original\\"+to_string(i)+".png",1);
		if(src.empty())
		{
			cout<<"null"<<endl;
		}
		resize(src,src,Size(512,512));
		//将图像写入视频;
		writer.write(src);

		namedWindow("show image");
		imshow("show image", src);
		waitKey(10);
		count++;
	}
	cout<<"count is:"<<count<<endl;
	waitKey(0);
	writer.release();
}

                                                                                                                                                                                                                                                                                           MATLAB composite video                                                                                              

In fact, in order to make it convenient to generate videos, you can also use MATLAB to make videos. It may be more convenient to write videos in MATLAB, without the need for third-party dependent libraries.

I couldn't watch the video on my mobile phone at the time. Although I used OpenCV to generate the video, I finally used MATLAB to synthesize the video. code show as below:

clc; clear all;
%定义生成视频的名称和位置;
writerObj = VideoWriter('tracking_50fps.avi');
%设置帧率;
writerObj.FrameRate=50;
open(writerObj);
%图像存放地点;
pt = 'F:\video_generate\result_every\';


%循环遍历所有图像,写进去就是;
for k = 1:171
    image = imread([pt,num2str(k),'.png']);
    writeVideo(writerObj,image);
end

close(writerObj);

Finally, you can see the detailed information as follows:

 

 

Guess you like

Origin blog.csdn.net/qq_32998593/article/details/82585532