下面给出一个opencv处理视频avi文件的例子:
VC建立一个基于dailog的工程,open按钮打开视频文件并进行处理。
class ImageBuffer{
public:
IplImage* last;
IplImage* curr;
IplImage* background;
IplImage* threshold;
IplImage* backDiff;
IplImage* mhi;//运动历史图像
IplImage* mask;//二值图像
long maxTagNumber;//上一帧组件数量
int lastTotal;//上一帧所有潜在目标数量
int lastAppear;//上一帧实际出现的有效rect数量,不包括上一帧之前的rect,为防抖动而设的参数。
CvMemStorage* storage;
CvMemStorage* storage2;
PotObjects PObjects;
double timestamp;//时间戳
};
void CVideoProcDlg::OnOpen()
{
int i;
CString Str1;
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
"Open avi file(*.avi)|*.avi||all file(*.*)|*.*");
if(FileDlg.DoModal()==IDOK)
{
Str1 = FileDlg.GetPathName();
}
else
{
AfxMessageBox("fail to open the file");
return;
}
const char* fileName = (const char*)Str1;
CvCapture* pInput_Video = cvCaptureFromAVI( fileName );
//打开视频帧
cvQueryFrame(pInput_Video);
//读取视频流参数
CvSize frame_size;
frame_size.height = (int) cvGetCaptureProperty(pInput_Video,CV_CAP_PROP_FRAME_HEIGHT);
frame_size.width = (int) cvGetCaptureProperty(pInput_Video,CV_CAP_PROP_FRAME_WIDTH);
long no_frames;
cvSetCaptureProperty(pInput_Video,CV_CAP_PROP_POS_AVI_RATIO,1);
no_frames = (int)cvGetCaptureProperty(pInput_Video,CV_CAP_PROP_POS_FRAMES);
IplImage* orig = NULL;
IplImage* dst = cvCreateImage(frame_size,IPL_DEPTH_8U,3);
IplImage* tmp = NULL;//cvCreateImage(frame_size,IPL_DEPTH_8U,3);
//建立两个窗口用于显示输入视频orig和输出视频result,均为彩色。
cvNamedWindow("mhi video",CV_WINDOW_AUTOSIZE);
cvNamedWindow("orig video",CV_WINDOW_AUTOSIZE);
cvNamedWindow("result video",CV_WINDOW_AUTOSIZE);//for temp display test;
//生成ImageBuffer缓冲区对象。
ImageBuffer myBuffer;
// ObjectList OL;
ImageBuffer* buffer = &myBuffer;
buffer->last = cvCreateImage(frame_size,IPL_DEPTH_8U,1);
buffer->curr = cvCreateImage(frame_size,IPL_DEPTH_8U,1);
buffer->threshold = cvCreateImage(frame_size,IPL_DEPTH_32F,1);
buffer->background = cvCreateImage(frame_size,IPL_DEPTH_8U,1);
buffer->backDiff = cvCreateImage(frame_size,IPL_DEPTH_8U,1);
buffer->mhi = cvCreateImage(frame_size,IPL_DEPTH_32F,1);
cvSetZero(buffer->mhi);
// cvSetZero(buffer->backDiff);
// buffer->no_Components = 0;
buffer->lastTotal = 0;
buffer->lastAppear = 0;
buffer->maxTagNumber = 0;
buffer->storage =cvCreateMemStorage(0);
buffer->storage2 =cvCreateMemStorage(0);
buffer->mask = cvCreateImage(frame_size,IPL_DEPTH_8U,1);
//初始化缓冲区对象成员
cvSetCaptureProperty(pInput_Video, CV_CAP_PROP_POS_FRAMES,1);
tmp = cvQueryFrame(pInput_Video);
cvCvtColor(tmp,buffer->last,CV_RGB2GRAY);
cvCopy(buffer->last, buffer->background,0);
for(i=2;i<no_frames;i=i+2)
{
cvSetCaptureProperty(pInput_Video, CV_CAP_PROP_POS_FRAMES,i);
orig = cvQueryFrame(pInput_Video);
cvShowImage("orig video",orig);
cvCvtColor(orig,buffer->curr,CV_RGB2GRAY);
HVtracker(buffer, 20, dst);//目标跟踪算法
HVclassify(buffer,dst);//目标类型分析算法
dst->origin = 1;
cvShowImage("result video",dst);
buffer->mhi->origin =1;
cvShowImage("mhi video", buffer->mhi);
cvCopy( buffer->curr, buffer->last, 0);
cvWaitKey(100);
}
cvReleaseCapture(&pInput_Video);
cvReleaseImage(&buffer->curr);
cvReleaseImage(&buffer->last);
cvReleaseImage(&buffer->threshold);
cvReleaseImage(&buffer->mhi);
cvClearMemStorage(buffer->storage);
cvClearMemStorage(buffer->storage);
涉及版权问题,tracker和classify算法不便公开,欢迎交流。