OpenCV video and picture sequences convert each other

Source: Internet
Author: User

In computer vision-related experiments, we need to convert the video and the image sequence, in peacetime experiments, often need to save the video as a frame of a picture, to obtain experimental results; On the other hand, many standard algorithm test database is a picture sequence (file name with frame number + image extension), We may need to convert it to video, so the conversion between the video and the picture is a very common operation. Find a better test program on the Internet, on the one hand it is packaged as a function, convenient to call; On the other hand, video to image can be arbitrarily set image output directory, function will automatically create the relevant directory, image sequence to video can set more parameters, you can better control, and can set the video output directory, The function automatically detects and creates the relevant directory. Suitable for Windows platform +OPENCV;

The code is as follows:

1//ImgToVideo_test.cpp:Defines the entry point for the console application.2//34 #include"StdAfx.h"5 #include <cv.h>6 #include 7 #include <iostream>8 #include <direct.h>//For Mk_dir9 #include <io.h>//For _acess ()Ten #include <String>1112int Recursive_mkdir (Char *dir);//Create a Multilevel Catalog13int Imagetovideo (Char* OutDir,Char* Videoname,Char* InputDir,int Startframe,int Endframe,IntIMGW,14int IMGH,Char* Imgext,Double fps,int IsColor,int FourCC);//Image sequence converted to video15int Videotoimage (Char* Videoname,Char* OutDir,Char* Imgext,int maxframecount);//Video into a sequence of pictures161718int main (int argc,char**argv19{20//Picture to video F:\carrdisp_test\origin21stchar* inputdir="f:\\carrdisp_test1\\colorfore\\";22char* videoname="Carrdisp_clofr.avi";23char* outdir="f:\\carrdisp_test1\\colorfore\\";24int Frames=imagetovideo (Outdir,videoname,inputdir,2,308,448,336,"_fr.jpg",24,1,CV_FOURCC (‘X‘,‘V‘,‘I‘,‘D‘));std::cout<<"Total Frames"<<frames<<"has been write to video."<<Std::endl;26IntPStd::cin>>P2829Return0;30}3132//Converts a sequence of pictures to a video, returning the number of video frames33int Imagetovideo (Char* OutDir,Char* Videoname,Char* InputDir,int Startframe,int Endframe,int IMGW,int IMGH,Char* Imgext,Double fps=24,int iscolor=1,int FOURCC=CV_FOURCC (‘X‘,‘V‘,‘I‘,‘D‘))34{35//Determine if the input folder exists36if (_access (InputDir,0) ==-1)37{std::cout<<"The input directory does not exist!"<<Std::endl;39Return0;40}41//Determines whether the output folder is created if it is not, or if NULL is the default current working directory42Char fullvideoname[255];//Full file name for Output Video: path + file namestrcpy_s (Fullvideoname,"");44if (outdir==NULL)45 {sprintf_s (Fullvideoname, "%s", videoname);//Print the Videoname as a string to save in Fullvideoname (_access ir,0) (==-1) (sprintf_s, "Fullvideoname",%s%s) {recursive_mkdir (OutDir); The string outdir and Videoname are concatenated, printed, saved in the fullvideoname of the framecount=0; Cvvideowriter *pwriter=null; Cvsize size=cvsize (IMGW,IMGH); Pwriter=cvcreatevideowriter (Videoname,fourcc,fps,size,iscolor),//create WRITER, Iplimage *pImg=NULL; The cur_fn[255];//char indicates the path of a picture in the Startframe<=endframe (strcpy_s) (CUR_FN, ""), and the sprintf_s (CUR_FN,%s%d %s ", inputdir,startframe,imgext);//need to change pimg=cvloadimage (Cur_fn,iscolor); !pimg {std::cout<< "can ' t open an image file" <<std::endl; "return framecount; Cvwritef Rame (PWRITER,PIMG); Cvwaitkey (1); std::cout<< "Write Frame" <<startFrame<<std::endl; startframe++; Cvreleaseimage (&AMP;PIMG); framecount++; Cvreleasevideowriter}(&pwriter); Bayi rename (videoname,fullvideoname);//move files to a specified folder Framecount return; 83} 84//convert video to Picture sequence returns the total number of pictures that were obtained by video decomposition the current OPENCV only supports AVI format so it takes 85//To convert video into an AVI format before using Videotoimage (char* Videoname,cha r* outdir,char* imgext,int maxframecount) (cvcapture *cap= cvcapturefromfile videoname): 91 (Cap==NULL) {92 return 0; 93} 94//The folder path to save the picture must be there, because OPENCV does not automatically create folders if (_access (outdir,0) ==-1) (Recursive_mkdir); OutDir 98 < "The ouput directory does not exist, and the been created autonomously!" <<std::endl; }100 Char cur_fn[255];//Save the file name of the picture obtained from the current frame 101 iplimage* pimg=null;102 int frame=0;103 while ((Pimg=cvqueryframe (CAP))! = null&& (Frame<maxframecount)) 104 {frame++;106 strcpy_s (CUR_FN, "") 107 sprintf_s (CUR_FN, "%s%d%s", Outdir,frame,imgext);//The settings here are suitable for shapes such as 123.jpg 124.jpg picture sequences 108 cvsaveimage (cur_fn,pimg,null); 109}110 Cvreleaseimage ( &AMP;PIMG); 111 cvreleasecapture (&AMP;CAP); frame;113}114 115 116//This function draws on the online data, automatically creates the multilevel directory 117 int Recursive_mkdir (char *dir) 118 {119//decomposition path name e:\\aa\\bb\\cc\120//121 std::string str = dir;122 int index = 0;123 int i = 0;124 while (1) 126 Std::string::size_type pos = str.find ("\ \", index), 127 std::string str1;128 str1 = str.substr (0, POS); 129 if (pos! =-1 && i > 0) (_access (Str1.c_str (), 0) ==-1) 131 {133 _mkdir (STR1.C_STR ()); 134}13 5}136 if (pos==-1) 137 {138 break;139}140 i ++;141 index = pos+1;142}143 return 0;144}

The above program in Vs2010+opencv2.3.1+windows XP environment test Pass, use the attention to set the start frame, end frame, picture format can be. I picture directory such as:

F:\carrdisp_test1\colorfore\2_Fr.jpg

It only calls the function that the picture sequence is written as a video, and the other function calls the method similar to the following example:

  //Video to pictureschar* videoname1="E:\\videos\\test\\videos\\woman.avi"; char* outdir1="e:\\videos\\test\\sequences\\woemensequence\\"; int Images=videotoimage (VIDEONAME1,OUTDIR1,". jpg",+); std::cout<<"Totalframes has been extracted from video." <<Std::endl; int p; std::cin>>p;               

OpenCV video and picture sequences convert each other

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.