#include <cv.h>
#include <highgui.h>
#include <stdio.h>
int main
()
{
CvCapture *capture;
IplImage *frame;
capture=cvCreateCameraCapture
(0);
cvNamedWindow
("Webcam",
0);
CvVideoWriter *writer;
char AviFileName
[]=
"Output.avi";
//int AviForamt = -1;
int AviForamt = CV_FOURCC
('P',
'I',
'M',
'1');
//MPEG-1 codec
int FPS =
25;
CvSize AviSize = cvSize
(640,
480);
int AviColor =
1;
writer=cvCreateVideoWriter
(AviFileName,AviForamt,FPS,AviSize,AviColor
);
int i=
0;
while(true)
{
frame = cvQueryFrame
(capture
);
cvWriteFrame
(writer,frame
);
printf("%d\n",i
);
printf("CV_CAP_PROP_FRAME_WIDTH : %.2f\n",cvGetCaptureProperty
(capture,
3));
printf("CV_CAP_PROP_FRAME_HEIGHT : %.2f\n",cvGetCaptureProperty
(capture,
4));
printf("CV_CAP_PROP_FOURCC : %.2f\n",cvGetCaptureProperty
(capture,
6));
cvShowImage
("Webcam",frame
);
if(cvWaitKey
(20)>
0) break;
i++;
}
cvReleaseCapture
(&capture
);
cvReleaseVideoWriter
(&writer
);
cvDestroyWindow
("Webcam");
}
而cvGetCaptureProperty()比较强大的功能就在读取AVI档的时候,它可以做操作的功能用 cvSetCaptureProperty(),可作用的参数为经过时间(毫秒),经过Frame数,AVI档播放百分比,宽度,高度,每秒经过几张 Frame,Frame的总数等.
cvGetCaptureProperty(capture,int property_id), property_id是属性标识,可以是下面之一:
CV_CAP_PROP_POS_MSEC - 影片目前位置,为毫秒数或者视频获取时间戳
CV_CAP_PROP_POS_FRAMES - 将被下一步解压/获取的帧索引,以0为起点
CV_CAP_PROP_POS_AVI_RATIO - 视频文件的相对位置(0 - 影片的开始,1 - 影片的结尾)
CV_CAP_PROP_FRAME_WIDTH - 视频流中的帧宽度
CV_CAP_PROP_FRAME_HEIGHT - 视频流中的帧高度
CV_CAP_PROP_FPS - 帧率
CV_CAP_PROP_FOURCC - 表示codec的四个字符
CV_CAP_PROP_FRAME_COUNT - 视频文件中帧的总数
注:有时候这个函数在cvQueryFrame被调用一次后,再调用cvGetCaptureProperty才会返回正确的数值。这是一个bug,建议在调用此函数前先调用cvQueryFrame。
而它可以做操控的部分,分为重新配置秒数码置,重新配置目前Frame位置,重新配置AVI目前播放百分比
cvSetCaptureProperty(capture,CV_CAP_PROP_POS_MSEC,毫秒数)
cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,Frame数)
cvSetCaptureProperty(capture,CV_CAP_PROP_POS_AVI_RATIO,播放百分比)
AVI视频文件属性修改程序
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
int main
()
{
CvCapture *capture;
IplImage *frame;
char AviFileName
[]=
"Output.avi";
capture = cvCaptureFromAVI
(AviFileName
);
cvNamedWindow
("AVI player",
0);
printf("CV_CAP_PROP_POS_MSEC : %.2f\n",cvGetCaptureProperty
(capture,
0));
printf("CV_CAP_PROP_POS_FRAMES : %.2f\n",cvGetCaptureProperty
(capture,
1));
printf("CV_CAP_PROP_POS_AVI_RATIO : %.2f\n",cvGetCaptureProperty
(capture,
2));
printf("CV_CAP_PROP_FRAME_WIDTH : %.2f\n",cvGetCaptureProperty
(capture,
3));
printf("CV_CAP_PROP_FRAME_HEIGHT : %.2f\n",cvGetCaptureProperty
(capture,
4));
printf("CV_CAP_PROP_FPS : %.2f\n",cvGetCaptureProperty
(capture,
5));
printf("CV_CAP_PROP_FRAME_COUNT : %.2f\n",cvGetCaptureProperty
(capture,
7));
cvWaitKey
(0);
while(true)
{
if(cvGrabFrame
(capture
))
{
frame=cvRetrieveFrame
(capture
);
cvShowImage
("AVI player",frame
);
printf("CV_CAP_PROP_POS_MSEC : %.2f\n",cvGetCaptureProperty
(capture,
0));
printf("CV_CAP_PROP_POS_FRAMES : %.2f\n",cvGetCaptureProperty
(capture,
1));
printf("CV_CAP_PROP_POS_AVI_RATIO : %.2f\n",cvGetCaptureProperty
(capture,
2));
printf("CV_CAP_PROP_FRAME_WIDTH : %.2f\n",cvGetCaptureProperty
(capture,
3));
printf("CV_CAP_PROP_FRAME_HEIGHT : %.2f\n",cvGetCaptureProperty
(capture,
4));
printf("CV_CAP_PROP_FPS : %.2f\n",cvGetCaptureProperty
(capture,
5));
printf("CV_CAP_PROP_FRAME_COUNT : %.2f\n",cvGetCaptureProperty
(capture,
7));
if(cvWaitKey
(10)>=
0) cvSetCaptureProperty
(capture,
2,
0);
}
else
{
break;
}
}
cvWaitKey
(0);
cvReleaseCapture
(&capture
);
cvDestroyWindow
("AVI player");
}
执行结果:
(1)
(2)
由上面程序可以看出 来,CV_CAP_PROP_FRAME_WIDTH,CV_CAP_PROP_FRAME_HEIGHT,CV_CAP_PROP_FPS,CV_CAP_PROP_FRAME_COUNT 的数值是固定不变的,它也具有不可修改的特性,每当程序在播放的时候,只要GUI接口触及键盘事件,AVI播放率就会变成0,因此,影片就会重新播放.接下来介绍全部的参数对照的代号.
#define CV_CAP_PROP_POS_MSEC 0
#define CV_CAP_PROP_POS_FRAMES 1
#define CV_CAP_PROP_POS_AVI_RATIO 2
#define CV_CAP_PROP_FRAME_WIDTH 3
#define CV_CAP_PROP_FRAME_HEIGHT 4
#define CV_CAP_PROP_FPS 5
#define CV_CAP_PROP_FOURCC 6
#define CV_CAP_PROP_FRAME_COUNT 7
"highgui.h"里面cvSetCaptureProperty()有16种参数,但其它都无法在AVI及视频底下使用,也许他在其他的文件格式下有不错的应用.
cvGetCaptureProperty()
取得CvCapture相关属性,区分为视频播放及AVI档的应用.
cvGetCaptureProperty(CvCapture数据结构,属性参数或代号);
cvSetCaptureProperty()
重新设立属性参数的数据,可修改的为时间,框页数,AVI百分比.
cvSetCaptureProperty(CvCapture数据结构,属性参数或代号,修改后数据);