Video Production
In the demo below, we mainly use the following special APIs, includingCvcreatevideowriter
AndCvwriteframe
Cvvideowriter * cvcreatevideowriter (
Const char * filename,
Int fourcc,
Double FPS,
Cvsize frame_size,
Int is_color = 1 );
This function is used to initialize a video file writer,
Needless to say, filename is the name of the video file to be created,
Fourcc indicates the encoding format of video compression,
Cv_fourcc ('P', 'I', 'M', '1') is the MPEG-1 codec,
Cv_fourcc ('M', 'J', 'P', and 'G') is motion-JPEG codec.
In Win32, if-1 is input, you can select the compression method and parameters from a dialog box.
FPS indicates the frame rate.
Is_color indicates whether to get a color frame or a gray frame.
Int cvwriteframe (
Cvvideowriter * Writer, const iplimage * image );
The role of this API is very obvious, that is, writing a frame to a video file. As for the parameters, I don't need to explain them much.
# Include "stdafx. H "# include" CV. H "# include" highgui. H "# include" stdio. H "int main (INT argc, char ** argv) {cvnamedwindow (" vedio "); cvcapture * capture = 0; // obtain a cvcapture object through the camera device if (1 = argc) {// obtain the initialization object cvcapturecapture = cvcreatecameracapture (0) from the camera );} else {// obtain the initialization object cvcapturecapture = cvcreatecameracapture (atoi (argv [1]) from the video file;} // if no valid cvcapture object is obtained, -1 is returned to terminate the program running if (! Capture) {return-1 ;}iplimage * frame; // specify the size of each frame in the video (the image taken by my camera is 160*120) cvsize size = cvsize (160,120); // You Need to initialize an object that writes a video file, note that the decoder format is set to 5cvvideowriter * videowriter = cvcreatevideowriter ("test. avi ", cv_fourcc ('M', 'J', 'P', 'G'), 5, size); char keycode; // every 30 ms, retrieve a frame from the camera while (keycode = cvwaitkey (30) {If (keycode = 27) {break ;} // get the frame = cvqueryframe (capture) obtained from the camera; // write the frame into the video file cvwriteframe (videowriter, frame); cvshowimage ("vedio ", frame);} cvreleasevideowriter (& videowriter); cvreleaseimage (& frame); cvdestroywindow ("video"); Return 0 ;}
F5 execution result:
The recorded video can be played and will not be uploaded here.
Reference: http://www.cnblogs.com/BoyXiao/archive/2010/10/27/1862928.html