从视频序列中抓取一帧
CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
CvCapture* capture = cvCaptureFromAVI("infile.avi");
IplImage* img = 0;
if(!cvGrabFrame(capture)){ // 抓取一帧
printf("Could not grab a frame/n/7");
exit(0);
}
img=cvRetrieveFrame(capture); // 恢复获取的帧图像
要从多个摄像头同时获取图像, 首先从每个摄像头抓取一帧. 在抓取动作都结束后再恢复帧图像.
cvReleaseCapture(&capture);
注意由设备抓取的图像是由capture函数自动分配和释放的. 不要试图自己释放它.
获取/设定帧信息
cvQueryFrame(capture); // this call is necessary to get correct
// capture properties
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
所有帧数似乎只与视频文件有关. 用摄像头时不对,奇怪!!!.
float posMsec = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
int posFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
float posRatio = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO);
获取所抓取帧在视频序列中的位置, 从首帧开始按[毫秒]算. 或者从首帧开始从0标号, 获取所抓取帧的标号. 或者取相对位置,首帧为0,末帧为1, 只对视频文件有效.
// 从视频文件相对位置0.9处开始抓取
cvSetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);
只对从视频文件抓取有效. 不过似乎也不成功!!!
存储视频文件
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; // 744 for firewire cameras
int frameH = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter("out.avi",CV_FOURCC(‘P‘,‘I‘,‘M‘,‘1‘),
fps,cvSize(frameW,frameH),isColor);
其他有效编码:
CV_FOURCC(‘P‘,‘I‘,‘M‘,‘1‘) = MPEG-1 codec
CV_FOURCC(‘M‘,‘J‘,‘P‘,‘G‘) = motion-jpeg codec (does not work well)
CV_FOURCC(‘M‘, ‘P‘, ‘4‘, ‘2‘) = MPEG-4.2 codec
CV_FOURCC(‘D‘, ‘I‘, ‘V‘, ‘3‘) = MPEG-4.3 codec
CV_FOURCC(‘D‘, ‘I‘, ‘V‘, ‘X‘) = MPEG-4 codec
CV_FOURCC(‘U‘, ‘2‘, ‘6‘, ‘3‘) = H263 codec
CV_FOURCC(‘I‘, ‘2‘, ‘6‘, ‘3‘) = H263I codec
CV_FOURCC(‘F‘, ‘L‘, ‘V‘, ‘1‘) = FLV1 codec
若把视频编码设为-1则将打开一个编码选择窗口(windows系统下).
IplImage* img = 0;
int nFrames = 50;
for(i=0;i<nFrames;i++){
cvGrabFrame(capture); // 抓取帧
img=cvRetrieveFrame(capture); // 恢复图像
cvWriteFrame(writer,img); // 将帧添加入视频文件
}
若想在抓取中查看抓取图像, 可在循环中加入下列代码:
cvShowImage("mainWin", img);
key=cvWaitKey(20); // wait 20 ms
若没有20[毫秒]延迟,将无法正确显示视频序列.
cvReleaseVideoWriter(&writer);
原文:http://blog.csdn.net/feixiang_john/article/details/27341999