I have explained the method and code for converting videos to images in opencv. This time, I want to explain how opencv converts images to videos:
Download the complete code in this article
1 videowriter description
Here we mainly use a class in opencv: videowriter
Its api reference link: API link.
Function prototype:
VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
Parameter description:
Filename: the name of the output video file, for example, "Out. Avi"
Fourcc: it is the encoding method defined by four characters. For details about the encoding method, refer to: encoding method reference.
It should be noted that this value is defined by cv_fourcc, for example:Cv_fourcc ('P', 'I', 'M', '1') indicatesMPEG-1 Coding
FPS: Video Frame Rate
Framsize: Video size
2. The image is written into the video in two ways: 1. Call the write function 2. Stream operator <
C++: VideoWriter& VideoWriter::operator<<(const Mat& image)C++: void VideoWriter::write(const Mat& image)
3 main code example
/// <Video parameters int width = 0, Height = 0, FPS = 0, fourcc = 0; MAT frame; // <image frame int framecounter = 0; /// <image frame counter namedwindow (windows_name_original); // <read the first image to obtain the image width and height, set FPS and encoding method string framename = namegenerate (framecounter); frame = imread (framename); width = frame. cols; // <obtain the image width and height = frame. rows; FPS = 15; // <FPS is 15 fourcc = cv_fourcc ('x', 'V', 'I', 'D '); /// <set the image encoding method cout <width <Endl
Duanxx opencv learning: Converting images into videos