今天仔细研究了一下差分法求运动的轮廓,简单用程序实现了一下,结果如下,
差分法比较容易获得运动的轮廓,对于不运动的身体部分则不会显示,
这样的好处是可以得到需要关注的运动部分,不运动的则不关心,
但是如果需要得到整个人体的轮廓,该如何呢?
我试着用程序记录前4帧的数据,然后叠加出来显示,看来效果不是很好。 还要继续考虑...
关键部分的代码如下:
main.cpp
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
// motion templates sample code
#include "cv.h"
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#endif
// various tracking parameters (in seconds)
const double MHI_DURATION = 1;
const double MAX_TIME_DELTA = 0.5;
const double MIN_TIME_DELTA = 0.05;
// number of cyclic frame buffer used for motion detection
// (should, probably, depend on FPS)
const int N = 4;
// ring image buffer
IplImage **buf = 0;
int last = 0;
// temporary images
IplImage *mhi = 0; // MHI
IplImage *orient = 0; // orientation
IplImage *mask = 0; // valid orientation mask
IplImage *segmask = 0; // motion segmentation map
CvMemStorage* storage = 0; // temporary storage
IplImage* abs_image = 0;
IplImage* add_abs_image = 0;
IplImage* abs_images[3];
IplImage* grey =0;
IplImage* pre_grey = 0;
IplImage* dst = 0;
CvSeq* contour = 0;
int test( IplImage* src,IplImage* pre_src );
int main(int argc, char** argv)
{
//IplImage* motion = 0;
CvCapture* capture = 0;
IplImage* pre_image = 0;
IplImage* image = 0;
int frame_count =0;
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
//capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
capture = cvCaptureFromFile("e:\\abc.avi");
else if( argc == 2 )
capture = cvCaptureFromAVI( argv[1] );
if( capture )
{
//cvNamedWindow( "Motion", 1 );
cvNamedWindow( "Source", 1 );
cvNamedWindow( "Components", 1 );
for(;;)
{
if( !cvGrabFrame( capture ))
break;
image = cvRetrieveFrame( capture );
if (!pre_image)
{
pre_image = cvCreateImage( cvGetSize(image), 8, 3 );
abs_image = cvCreateImage( cvGetSize(image), 8, 1 );
add_abs_image = cvCreateImage( cvGetSize(image), 8, 1 );
grey = cvCreateImage( cvGetSize(image), 8, 1 );
pre_grey = cvCreateImage( cvGetSize(image), 8, 1 );
dst = cvCreateImage( cvGetSize(image), 8, 3 );
abs_image->origin = image->origin;
add_abs_image->origin = image->origin;
dst->origin = image->origin;
storage = cvCreateMemStorage(0);
abs_images[0] = cvCreateImage( cvGetSize(image), 8, 1 );
abs_images[1] = cvCreateImage( cvGetSize(image), 8, 1 );
abs_images[2] = cvCreateImage( cvGetSize(image), 8, 1 );
abs_images[3] = cvCreateImage( cvGetSize(image), 8, 1 );
cvZero(abs_images[0]);
cvZero(abs_images[1]);
cvZero(abs_images[2]);
cvZero(abs_images[3]);
}
frame_count++;
test(image,pre_image);
cvCopy(abs_images[frame_count%3],abs_image,0);
cvCopy(image,pre_image,0);
if( cvWaitKey(10) >= 0 )
break;
}
cvReleaseCapture( &capture );
//cvDestroyWindow( "Motion" );
}
return 0;
}
int test( IplImage* src,IplImage* pre_src )
{
cvCvtColor(src, grey, CV_BGR2GRAY);
cvZero(add_abs_image);
cvCvtColor(pre_src, pre_grey, CV_BGR2GRAY);
cvAbsDiff( grey,pre_grey, abs_image );
cvThreshold( abs_image, abs_image, 20, 255, CV_THRESH_BINARY );
cvAdd(abs_images[0],abs_image,add_abs_image);
cvThreshold( add_abs_image, add_abs_image, 10, 255, CV_THRESH_BINARY );
cvAdd(abs_images[1],add_abs_image,add_abs_image);
cvThreshold( add_abs_image, add_abs_image, 10, 255, CV_THRESH_BINARY );
cvAdd(abs_images[2],add_abs_image,add_abs_image);
cvThreshold( add_abs_image, add_abs_image, 10, 255, CV_THRESH_BINARY );
cvAdd(abs_images[3],add_abs_image,add_abs_image);
cvThreshold( add_abs_image, add_abs_image, 10, 255, CV_THRESH_BINARY );
//cvWaitKey(0);
cvFindContours( abs_image, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
cvZero( dst );
for( ; contour != 0; contour = contour->h_next )
{
CvScalar color = CV_RGB( 255, 255, 255 );
/* replace CV_FILLED with 1 to see the outlines */
cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );
}
//cvNamedWindow( "Source", 1 );
cvShowImage( "Source", src );
//cvNamedWindow( "Components", 1 );
cvShowImage( "Components", dst );
//cvWaitKey(0);
return 0;
}
#ifdef _EiC
main(1,"motempl.c");
#endif