Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
# 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) // The trace parameter (in seconds) const double mhi_duration = 0.5; // 0.5 is the maximum duration of Motion Tracking. Const double max_time_delta = 0.05; const double min_time_delta = 1000; const int n = 3; // const int contour_max_aera =; // ring image buffer circles the image buffer iplimage ** Buf = 0; // indicates Pointer int last = 0; // temporary images temporary image iplimage * MHI = 0; // MHI: Motion history imagecvfilter filter = success; cvconnectedcomp * cur_comp, min_comp; cvconnectedcomp; comp; cvmemstorage * storage; cvpoint PT [4]; // parameter: // IMG-input video frame // DST-detection result void update_mhi (iplimage * IMG, iplimage * DST, int diff_threshold) {double timestamp = clock ()/100 .; // get current time in seconds timestamp cvsize size = CV Size (IMG-> width, IMG-> height); // get the current frame size to get the int I, idx1, idx2; iplimage * silh; iplimage * Pyr = cvcreateimage (cvsize (size. width &-2)/2, (size. height &-2)/2), 8, 1); cvmemstorage * stor; cvseq * cont;/* initialize data first */If (! MHI | MHI-> width! = Size. Width | MHI-> height! = Size. height) {If (BUF = 0) // if no Initialization is available, allocate the memory to him {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]); // clear buffer frame at the beginning} cvreleaseimage (& MHI); MHI = cvcreateimage (size, ipl_depth_32f, 1); cvzero (MHI); // clear MHI at the B Eginning} // end of IF (MHI)/* converts the frame to be processed into a grayscale frame and puts it in the last frame of the buffer */cvcvtcolor (IMG, Buf [last], cv_bgr2gray); // convert frame to grayscale/* set the frame sequence number * // * Last ----> idx1 ^ | idx2 <----- (last + 1) % 3 */idx1 = last; idx2 = (last + 1) % N; // index of (last-(N-1) Th frame last = idx2; // do the frame difference silh = Buf [idx2]; // difference to idx2 | idx2-idx1 | --> idx2 (<-silh) cvabsdiff (BUF [idx1], buf [idx2], silh); // get difference Between frames // binarization cvthreshold (silh, silh, 30,255, cv_thresh_binary) for the differential image; // threshold it, binarization cvupdatemotionhistory (silh, MHI, timestamp, mhi_duration); // update MHI cvconvert (MHI, DST); // convert MHI to DST, DST = MHI // median filter to eliminate small noise cvsmooth (DST, cv_median, 3, 0, 0, 0); cvpyrdown (DST, Pyr, cv_gaussian_5x5); // downward sampling to remove noise. The image is 1/4 cvdilate (Pyr, Pyr, 0, 1); // perform expansion to eliminate the non-continuous holes of the target cvpyrup (P YR, DST, cv_gaussian_5x5); // sample upwards to restore the image, the image is four times the original image. //// the program section below is used to find the contour /// create dynamic structure and sequence. stor = cvcreatemstorage (0); cont = cvcreateseq (cv_seq_eltype_point, sizeof (cvseq), sizeof (cvpoint), STOR); // locate all outlines of cvfindcontours (DST, STOR, & Cont, sizeof (cvcontour), cv_retr_list, cv_chain_approx_simple, cvpoint (); // draw the contour for (; cont = cont-> h_next) {cvre CT r = (cvcontour *) cont)-> rect; If (R. height * r. width> pai_max_aera) // discard the {cvrectangle (IMG, cvpoint (R. x, R. y), cvpoint (R. X + R. width, R. Y + R. height), cv_rgb (255, 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); // the camera is the video source else if (argc = 2) Capture = cvcapturefromavi (argv [1]); // Avi is the video source if (capture) {cvnamedwindow ("motion", 1); // creates a window for (;) {iplimage * image; If (! Cvgrabframe (capture) // capture a sequence break; image = cvretrieveframe (capture); // retrieve this frame if (image) // if it is obtained, judge whether the motion is null {If (! Motion) {motion = cvcreateimage (cvsize (image-> width, image-> height), 8, 1); // create a motion frame, eight bits, channel cvzero (motion); // zero-fill motion-> origin = image-> origin; // The memory storage order is the same as the retrieved frame} update_mhi (image, motion, 60); // update the history image cvshowimage ("motion", image); // display the processed image if (cvwaitkey (10)> = 0) // press any key in 10 ms to exit break;} cvreleasecapture (& capture); // release the cvdestroywindow ("motion"); // destroy the window} return 0 ;}
Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/