[学习OpenCV攻略][010][写入AVI文件]
cvSize(文件宽度,文件高度)
通过图片或视频文件的宽高得到尺寸信息,返回值是CvSize
cvCreateVideoWriter(输出文件名,编码格式,帧率,图像大小)
通过设置输出视频的格式信息得到输出的视频,返回值是CvVideoWriter
CV_FOURCC(编码格式)
设置编码格式,('D','X','5','0') mpeg4格式,('M','J','P','G') mjpeg
cvWriteToAVI(输出视频流,输入视频流)
将输出视频流(IplImage类型)写成AVI格式到输出视频流(CvVideoWriter类型)中
cvReleaseVideoWriter(视频写入器)
释放视频写入器
#include <stdio.h> #include "cv.h" #include "highgui.h" int main(int argc, char **argv){ cvNamedWindow("hello", CV_WINDOW_AUTOSIZE); cvNamedWindow("log_polar", CV_WINDOW_AUTOSIZE); CvCapture *capture = cvCreateFileCapture(argv[1]); if(!capture){ return -1; } double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); printf("fps = %d\n", (int)fps); CvSize size = cvSize( (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) ); printf("frame (w,h) = (%d,%d)\n", size.width, size.height); CvVideoWriter *write = cvCreateVideoWriter(argv[2], CV_FOURCC('M','J','P','G'), fps, size); //CV_FOURCC('D','X','5','0') mpeg格式 CV_FOURCC('M','J','P','G') mjpeg格式 IplImage *logpolar_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage *gray_frame = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage *bgr_frame; while(1){ bgr_frame = cvQueryFrame(capture); if(!bgr_frame){ break; } cvShowImage("hell0", bgr_frame); cvConvertImage(bgr_frame, gray_frame, CV_RGB2GRAY); cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2,bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvShowImage("log_polar", logpolar_frame); cvWriteToAVI(write, logpolar_frame); //输出的文件名为.avi格式 char c = cvWaitKey(10); if(c == 27){ break; } } cvReleaseVideoWriter(&write); cvReleaseImage(&gray_frame); cvReleaseImage(&logpolar_frame); cvReleaseCapture(&capture); cvDestroyWindow("hello"); cvDestroyWindow("log_polar"); return 0; }