In fact, the video to the picture in the previous article has some clues, in fact, is to read the video frame, and then save the frame is OK. Then you add a progress bar to beautify ... This code is easy to understand, or directly on the code bar.
Video to pictures
1 /// <summary>2 ///convert video to Picture3 /// </summary>4 /// <param name= "path" ></param>5 Public voidVideo2image (Objectpath)6 {7 Try8 {9 //determine if a folder existsTen if(! Directory.Exists (filepath + filename +"\\")) One { A Try - { - //does not exist create folder theDirectory.CreateDirectory (filepath + filename +"\\"); - } - Catch { } - } + -IntPtr Catchframe =cvinvoke.cvcreatefilecapture (path. ToString ()); + //get the total number of frames A varCount =Cvinvoke.cvgetcaptureproperty (Catchframe, Emgu.CV.CvEnum.CAP_PROP. Cv_cap_prop_frame_count); at //Video Width - intWD = (int) Cvinvoke.cvgetcaptureproperty (Catchframe, Emgu.CV.CvEnum.CAP_PROP. Cv_cap_prop_frame_width); - //Video Height - intHG = (int) Cvinvoke.cvgetcaptureproperty (Catchframe, Emgu.CV.CvEnum.CAP_PROP. Cv_cap_prop_frame_height); - ////Current frame position - //Cvinvoke.cvgetcaptureproperty (Catchframe, Emgu.CV.CvEnum.CAP_PROP. Cv_cap_prop_pos_frames); in ////Frame Frequency - //Cvinvoke.cvgetcaptureproperty (Catchframe, Emgu.CV.CvEnum.CAP_PROP. CV_CAP_PROP_FPS); to + IntPtr frameimg; - inti =0; the *IntPtr grayimg = Cvinvoke.cvcreateimage (NewSize (WD, Hg), Emgu.CV.CvEnum.IPL_DEPTH. IPL_DEPTH_8U,1); $ while((frameimg = Cvinvoke.cvqueryframe (catchframe))! =IntPtr.Zero)Panax Notoginseng { - //here I am converted to grayscale, save grayscale, small partners can directly save Frameimg the Cvinvoke.cvcvtcolor (frameimg, grayimg, Emgu.CV.CvEnum.COLOR_CONVERSION. Bgr2gray); + A stringPicname = filepath + filename +"\\image"+ (++i) +". jpg"; the + cvinvoke.cvsaveimage (Picname, grayimg, IntPtr.Zero); - $ } $ - } - Catch(Exception ex) the { - MessageBox.Show (ex. ToString ());Wuyi } the -}
Well, the important thing is that the picture goes to the video, here comes the decoder. I need to convert the video to MP4 format, so I chose the decoder for XviD.
1 /// <summary>2 ///convert pictures to videos3 /// </summary>4 Public voidImage2video ()5 {6 Try7 {8 varFiles = Directory.GetFiles (filepath,"*.jpg");9 intCount =files. Count ();Ten intIsColor =1; One //Frame Frequency A intfps =5; - inti =0; - stringPicname = files[0]; theBitmap map =NewBitmap (picname); - intFramew =map. Width; - intFrameh =map. Height; - stringVideoname = filepath +"\\out.mp4"; + varwriter = Cvinvoke.cvcreatevideowriter (Videoname, CVINVOKE.CV_FOURCC ('X','V','I','D'), FPS,NewSystem.Drawing.Size (Framew, Frameh), iscolor); - map. Dispose (); +Cvinvoke.cvnamedwindow ("Mainwin"); A while(I <count) at { -Picname =Files[i]; - varIMG =cvinvoke.cvloadimage (Picname, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE. Cv_load_image_anycolor); - if(img = =NULL) - { -Cvinvoke.cvreleaseimage (refimg); in Continue; - } toCvinvoke.cvshowimage ("Mainwin", IMG); + BOOLFlag =cvinvoke.cvwriteframe (writer, img); - if(!flag) the { *Cvinvoke.cvreleaseimage (refimg); $ Continue;Panax Notoginseng } -Cvinvoke.cvwaitkey ( -); theCvinvoke.cvreleaseimage (refimg); +i++; A } theCvinvoke.cvreleasevideowriter (refwriter); +Cvinvoke.cvdestroywindow ("Mainwin"); - } $ Catch(Exception e) $ { - MessageBox.Show (e.tostring ()); - } the}
That ' s all!
The best time to plant a tree is ten years ago, followed by the present.
EMGUCV Learning-Video and image transfer