Home > Software engineering >  This is the process of moving targets detection, want to ask you a great god, and do the frame diffe
This is the process of moving targets detection, want to ask you a great god, and do the frame diffe

Time:10-08

# include "CV. H"
# include "highgui. H"
# include & lt; Time. H>
# include & lt; Math. H>
# include & lt; Stdio. H>
# include & lt; String. H>

//various tracking the parameters (in seconds)//tracking parameters (in seconds)
Const double MHI_DURATION=0.5;//0.5 s for tracking the maximum duration of
Const double MAX_TIME_DELTA=0.5;//maximum time increment of 0.5 s
Const double MIN_TIME_DELTA=0.05;//minimum time increment 0.05 s
Const int N=3;
//
Const int CONTOUR_MAX_AERA=16;

//ring image buffer circle image buffer
Buf IplImage * *=0;//pointer to a pointer to the
int last=0;

//temporary images' temporary image
Mhi IplImage *=0;//MHI: motion history image motion history image


CvConnectedComp * cur_comp min_comp;//connected components
CvConnectedComp comp;
CvMemStorage * storage;//memory storage
CvPoint pt [4];//2 d coordinates of the point, of type integer, usually with zero as the origin, coordinates x and y coordinates,

Int nCurFrameIndex=0;

//parameters:
//img - input video frames//DST - test results
Void update_mhi (img IplImage * and DST IplImage * and an int diff_threshold)
{
Double timestamp=clock ()/100.//get the current time in seconds the timestamp
CvSize size=CvSize (img - & gt; Width, img - & gt; Height);//get the current frame size, the size of the current frame
Int I, idx1 idx2;
IplImage * silh;
IplImage * pyr=cvCreateImage (cvSize ((size, width & amp; - 2)/2, (the size height & amp; - 2)/2), 8, 1);
CvMemStorage * stor.
CvSeq * cont.

/* initialized data first */
if( ! Mhi | | mhi - & gt; Width! .=the size width | | mhi - & gt; Height!=the size, height)
{
If (buf==0)//if there is no initialization then allocates memory to him
{
Buf=(IplImage * *) malloc sizeof (N * (buf [0]));
Memset (buf, 0 N * sizeof (buf [0]));
}

For (I=0; I & lt; N; I++)
{
CvReleaseImage (& amp; Buf [I]);
Buf [I]=cvCreateImage (size, IPL_DEPTH_8U, 1);
CvZero (buf [I]);//the clear Buffer Frame at the beginning
}
CvReleaseImage (& amp; Mhi);
Mhi=cvCreateImage (size, IPL_DEPTH_32F, 1);
CvZero (mhi);//the clear MHI at the beginning
}//end of the if (mhi)

/* will have to deal with the current frame into a gray level in the last frame buffer in the */
CvCvtColor (img, buf [last], CV_BGR2GRAY);//the convert frame to grayscale

/* set the frame serial number */
Idx1=last;
Idx2=(last + 1) % N;//the index of (the last - (N - 1)) th frame
The last=idx2;

//do frame differential
Silh=buf [idx2];//difference to idx2
CvAbsDiff (buf [idx1], buf [idx2], silh);//get difference between frames

//do to bad image binarization
CvThreshold (silh silh, 50, 255, CV_THRESH_BINARY);//threshold it, binarization

//get rid of the image of the timeout to update the motion history image
CvUpdateMotionHistory (silh, mhi, timestamp, MHI_DURATION);//update MHI

CvConvert (mhi, DST);//convert mhi to DST, DST=mhi

//median filtering, to eliminate the noise of the small
CvSmooth (DST, DST, CV_MEDIAN, 3, 0, 0, 0).

CvPyrDown (DST, pyr CV_GAUSSIAN_5x5);//down sampling, remove noise, image is a quarter of the original image
CvDilate (pyr, pyr, 0, 1);//do expansion operation, eliminate goal of discontinuous hollow
CvPyrUp (pyr, DST, CV_GAUSSIAN_5x5);//sampling up, restore image, image is four times the size of the original image

//the following program segment is used to find outline
//Create the dynamic structure and sequence.
Stor=cvCreateMemStorage (0);
Cont=cvCreateSeq (CV_SEQ_ELTYPE_POINT, sizeof (CvSeq), sizeof (CvPoint), stor);

//find all contour
CvFindContours (DST, stor, & amp; Cont, sizeof (CvContour),
CV_RETR_LIST CV_CHAIN_APPROX_SIMPLE, cvPoint (0, 0));

//directly using the rectangle from the CONTOUR to CONTOUR
for(; Cont. Cont=cont - & gt; H_next)
{
CvRect r=((CvContour *) cont) - & gt; The rect.
If (r.h. eight * truly idth & gt; CONTOUR_MAX_AERA)//small size square away
{
CvRectangle (img, cvPoint (r.x, r.y),
CvPoint (r.x + truly idth, r.y + r.h. eight),
CV_RGB (0, 255), 1, CV_AA, 0);
}
}
//free memory
CvReleaseMemStorage (& amp; Stor);
CvReleaseImage (& amp; Pyr);
}


Int main (int arg c, char * * argv)
{
//save the video file
//! To save the video file name is set to "F://VideoSave. Avi"
Char szVideoSaveName []="F://VideoSave. Avi";

CvVideoWriter * pVideoWriter=NULL;//used to save video files
//IplImage * pFrame=NULL;
//IplImage * pImage=NULL;
IplImage * motion=0;
CvCapture * capture=0;

The capture=cvCaptureFromAVI (" video. Avi ");//AVI video source
If (capture)
{
CvNamedWindow (" Motion ", 1);//set up window

//create video writer
PVideoWriter=cvCreateVideoWriter (/* */"VideoSave. Avi" szVideoSaveName,/* CV_FOURCC (' M ', 'J', 'P', 'G') */- 1, 25, cvSize (640480), 1);
For (; ; )
{
IplImage * image;
if( ! CvGrabFrame (capture))//capture slide
break;
Image=cvRetrieveFrame (capture); Remove this frame//
If (image)//if to judge whether the motion is the empty
{
if( ! Motion)
{
Motion=cvCreateImage (cvSize (image - & gt; Width of the image - & gt; Height), 8, 1);//create motion frame, eight, a channel
CvZero (motion);//zero padding motion
Motion - & gt; Origin=image - & gt; Origin;//memory storage and take out the frame of the same order of
}
}
Update_mhi (image, motion, 60);//update the historical images
CvShowImage (" Motion ", image);//show the processed image

//pVideoWriter=cvCreateVideoWriter (/* */szVideoSaveName "VideoSave. Avi", 1, 10, cvSize (640480), 1);//on a video will only be in one frame note
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related