关于cvUpdataMotionHistory函数中的mhi参数的解释版主: ollydbg23, bebekifis, chai2010 关于cvUpdataMotionHistory函数中的mhi参数的解释昨天看运动目标跟踪的程序,有几点怎么也想不通。把程序发上,向各位请教。不懂的地方跟在程序语句后面。帮我看看啊,实验室只有我一个学opencv 连个问的人都没有。
#include "cv.h" #include "highgui.h" #include <time.h> #include <math.h> #include <ctype.h> #include <stdio.h> #include <string.h> // various tracking parameters (in seconds) const double MHI_DURATION = 0.5; const double MAX_TIME_DELTA = 0.5; const double MIN_TIME_DELTA = 0.05; const int N = 3; // const int CONTOUR_MAX_AERA = 16; // ring image buffer IplImage** buf = 0; int last = 0; // temporary images IplImage *mhi = 0; // MHI: motion history image CvFilter filter = CV_GAUSSIAN_5x5; CvConnectedComp *cur_comp, min_comp; CvConnectedComp comp; CvMemStorage *storage; CvPoint pt[4]; // 参数: // img – 输入视频帧 // dst – 检测结果 void update_mhi( IplImage* img, IplImage* dst, int diff_threshold ) { double timestamp = clock()/100.; // get current time in seconds CvSize size = cvSize(img->width,img->height); // get current frame size int i, idx1, idx2; IplImage* silh; IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 ); CvMemStorage *stor; CvSeq *cont; if( !mhi || mhi->width != size.width || mhi->height != size.height ) { if( buf == 0 ) { buf = (IplImage**)malloc(N*sizeof(buf[0])); memset( buf, 0, N*sizeof(buf[0])); } for( i = 0; i < N; i++ ) { cvReleaseImage( &buf[i] ); buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); cvZero( buf[i] );//把buf都清零了 } cvReleaseImage( &mhi ); mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); cvZero( mhi ); // 把mhi也清零了 } // end of if(mhi) cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale idx1 = last; idx2 = (last + 1) % N; // index of (last - (N-1))th frame last = idx2; // 做帧差 silh = buf[idx2]; cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames ///这里,我不理解是怎么做的帧差,当last刚开始=0时,idx1=0,idx2=1,last=1,此时,buf[1]与buf[0]相 减,buf【0】里面存的是由当前帧转化而来的灰度帧,而buf【1】里面是空的,因为刚开始的时候已经置零了。差值存于buf【1】中,等到主函数再次 for循环一次的时候,又要调用这个函数,此时buf【1】中存的是当前帧的灰度 帧,idx1=last=1,idx2=2,last=2,cvAbsDiff函数将buf【1】与buf【2】相减,buf【2】里面是空的,因为在开 始的时候已经置零了,这样减来减去,都是跟空buf在减,怎么来的帧差?还是我的理解错了?后面的程序,我假设是真的得到了帧差。 // 对差图像做二值化 cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY ); // and threshold it cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI 这里,silh是对以前差值图像做了二值化后的结果,也就是只有零与255两个灰度值,cvUpdataMotionHistory函数中的 silhouette参数的解释是影响mask,运动发生的地方具有非零像素,这个我可以理解,但是第二个参数mhi的解释是运动历史图像为本函数所更 新,我不懂,什么叫运动历史图像?更新了有什么用?为什么要更新?在后面的程序中把mhi当做函数的输入参数,输出参数是dst,接着在dst图像里寻找 轮廓的个数,也就是dst还是个二值化的图像。 cvCvtScale( mhi, dst, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION ); cvCvtScale( mhi, dst, 255./MHI_DURATION, 0 ); 不理解为什么要用到这个函数,而且还是要用两次,为什么要把mhi中的元素转换为8位无符号整型?后面的程序我都懂。 // 中值滤波,消除小的噪声 cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 ); // 向下采样,去掉噪声 cvPyrDown( dst, pyr, 7 ); cvDilate( pyr, pyr, 0, 1 ); // 做膨胀操作,消除目标的不连续空洞 cvPyrUp( pyr, dst, 7 ); // // 下面的程序段用来找到轮廓 // // Create dynamic structure and sequence. stor = cvCreateMemStorage(0); cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor); // 找到所有轮廓 cvFindContours( dst, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); /* for(;cont;cont = cont->h_next) { // Number point must be more than or equal to 6 (for cvFitEllipse_32f). if( cont->total < 6 ) continue; // Draw current contour. cvDrawContours(img,cont,CV_RGB(255,0,0),CV_RGB(255,0,0),0,1, 8, cvPoint(0,0)); } // end of for-loop: "cont" */ // 直接使用CONTOUR中的矩形来画轮廓 for(;cont;cont = cont->h_next) { CvRect r = ((CvContour*)cont)->rect; if(r.height * r.width > CONTOUR_MAX_AERA) // 面积小的方形抛弃掉 { cvRectangle( img, cvPoint(r.x,r.y), cvPoint(r.x + r.width, r.y + r.height), CV_RGB(255,0,0), 1, CV_AA,0); } } // free memory cvReleaseMemStorage(&stor); cvReleaseImage( &pyr ); } int main(int argc, char** argv) { IplImage* motion = 0; CvCapture* capture = 0; if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 ); else if( argc == 2 ) capture = cvCaptureFromAVI( argv[1] ); if( capture ) { cvNamedWindow( "Motion", 1 ); for(;;) { IplImage* image; image = cvQueryFrame( capture ); if( image ) { if( !motion ) { motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 ); cvZero( motion ); motion->origin = image->origin; } } update_mhi( image, motion, 60 ); cvShowImage( "Motion", image ); if( cvWaitKey(10) >= 0 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "Motion" ); } return 0; } 昨天看运动目标检测的程序,里面用到了cvUpdataMotionHistory函数,cvUpdataMotionHistory(const CvArr* silhouette, CvArr* mhi,double timestamp,double duration),该程序用的是帧差法,将上一帧和 |
|