Basic Learning notes-opencv (6): video generation

Source: Internet
Author: User

During the experiment, it is inevitable to read the images in the video for processing. On the contrary, it is also very common to organize and output the processed images into a video file. The following describes how the image output video is implemented in the C ++ version based on opencv.

The data for this test is the Swing Tree leaf image, waving trees, its source URL: http://research.microsoft.com/en-us/um/people/jckrumm/WallFlower/TestImages.ht

The data is composed of BMP images.

The project environment for this test is: opencv2.3.1 + vs2010

Experiment function: generate a gray video file in AVI format from multiple BMP color images.

Note the following points during the test:

1. Processing the output video file name and selecting the Video Frame Rate

2. Select the format of the output video file. It seems that opencv supports two formats: mip1 and mjpg. However, during the test, we found that if we select mip1, not only the output video quality is very poor, but the frame rate cannot be controlled.

3. For more information about whether the video output is color, see the code comments section.

The following is the project code:

// Generate_vedio.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> # include <stdio. h ># include <iostream> using namespace CV; using namespace STD; int main (INT argc, unsigned char * argv []) {string str_name = "wavingtrees/B00 "; char STR [4]; MAT img_src, img_dst; vector <mat> img_gray; img_src = imread ("wavingtrees/b00000.bmp"); // If videowriter is used If the file to be written to the video does not exist, create a new one. If yes, it must be a valid video file; otherwise, the following statement will report an error // if you select the pim1 format, the frame rate of the output video cannot be manually selected, and the output video effect is very bad. // videowriter output_src ("demo_src.avi", cv_fourcc ('P', 'I', 'M ', '1'), 2, IMG. size (), 1); // output color video // videowriter output_dst ("demo_dst.avi", cv_fourcc ('P', 'I', 'M', '1 '), 25, IMG. size (), 0); // output grayscale video // when using the mjpg format, you can select the output frame rate, and the video output effect is much better, therefore, this format is generally used to output // if the last parameter iscolor is not 0, it indicates that three-channel color videos are output, otherwise, when iscolor = 0, the output is a single channel black/white grayscale video. // However, when the image to be added to the video is a single channel image Although the output video is black-and-white or grayscale, three images are displayed at the same time, that is, the width is reduced by three times videowriter output_src ("demo_src.avi", cv_fourcc ('M ', 'J', 'P', 'G'), 10, img_src.size (), 1); // output the videowriter output_dst ("demo_dst.avi", cv_fourcc ('M ', 'J', 'P', 'G'), 10, img_src.size (), 1); // output the gray video int I =-1; namedwindow ("src ", window_autosize); While (1) {I ++;/*** processing before image Name Reading ***/_ itoa_s (I, STR, 10 ); if (I <10) str_name + = "00"; else if (I <100) str_name + = "0"; str_name + = STR; Str_name + = ". BMP "; if (256 = I) return 0;/***** output the original color video *****/img_src = imread (str_name); If (img_src.empty ()) return 0; output_src  variable. If this statement is not available, // The following uses img_gray [1] to consider the split (img_src, img_gray) as an error; cvtcolor (img_src, img_gray [1], cv_bgr2gray ); // color videos are output in this way. // If 0 is assigned to other channels, it is not a true black-and-white grayscale image, it is a grayscale image with a blue background. // img_gray [0] = img_gray [2] = mat: zeros (img_src.size (), I Mg_gray [1]. type (); img_gray [0] = img_gray [2] = img_gray [1]; // This assignment is a true black-and-white gray map Merge (img_gray, img_dst ); // However, although the output is a grayscale video, a pair of images contain three identical images, that is, the image width is reduced by three times, why? // At this time, 3-channel images cannot be output; otherwise, the generated video cannot play output_dst 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.