// det_mov.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include //defines the initial value of the thresholds int thresholdh = 0; int thresholds = 0; int thresholdv = 50; int _tmain(int argc, _TCHAR* argv[]) { // Capture from a camera: //CvCapture* capture = cvCreateCameraCapture(-1); // -1 for any camera // Capture from a file: CvCapture* capture = cvCaptureFromAVI("intelligentroom_raw.AVI"); //CvCapture* capture = cvCaptureFromAVI("highwayI_raw.AVI"); //CvCapture* capture = cvCaptureFromAVI("PetsD1TeC1.avi"); IplImage* img = 0; IplImage* img_seg = 0; //creates the windows cvNamedWindow( "img_wnd_seg", 0 ); cvNamedWindow( "img_wnd", 0 ); //verifies if the video has a minimum number of frames int numFrames = (int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT); if (numFrames < 100) { printf("The video must have more than 100 frames\n\7"); exit(0); } //images initialization IplImage* img_hsv=0; IplImage* img_color=0; IplImage* img_hsvA=0; IplImage* img_h=0; IplImage* img_s=0; IplImage* img_v=0; IplImage* img_hA=0; IplImage* img_sA=0; IplImage* img_vA=0; if(cvGrabFrame(capture)) { img_color = cvCreateImage( cvGetSize( cvRetrieveFrame(capture) ), IPL_DEPTH_8U, 3 ); img_hsv = cvCreateImage( cvGetSize( img_color ), IPL_DEPTH_8U, 3 ); img_hsvA = cvCreateImage( cvGetSize( img_color ), IPL_DEPTH_8U, 3 ); img_h = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 180 img_s = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 255 img_v = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 255 img_hA = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 180 img_sA = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 255 img_vA = cvCreateImage( cvGetSize( img_hsv ), IPL_DEPTH_8U, 1 ); //Espaço de 0 a 255 } //defines the number of frames used to train the background int numframesT=20; /* Training the background */ for (int i=0; iwidth; w++ ) { for( int h = 0; h < img_color->height; h++ ) { sT=cvGet2D(img_hsvA,h,w); // get the (i,j) pixel value s=cvGet2D(img_hsv,h,w); // get the (i,j) pixel value if ((abs(s.val[0]-sT.val[0])>thresholdh) && (abs(s.val[1]-sT.val[1])>thresholds) && (abs(s.val[2]-sT.val[2])>thresholdv)) { cvSet2D(img_seg,h,w,sIn); } else { cvSet2D(img_seg,h,w,sOut); } } } /* morphologic operations of opening and closing to enhance the result */ cvDilate( img_seg, img_seg, cvCreateStructuringElementEx( 9, 9, 4, 4, CV_SHAPE_ELLIPSE), 1 ); cvErode( img_seg, img_seg, cvCreateStructuringElementEx( 9, 9, 4, 4, CV_SHAPE_ELLIPSE), 1 ); cvErode( img_seg, img_seg, cvCreateStructuringElementEx( 3, 3, 1, 1, CV_SHAPE_ELLIPSE), 1 ); cvDilate( img_seg, img_seg, cvCreateStructuringElementEx( 3, 3, 1, 1, CV_SHAPE_ELLIPSE), 1 ); /* Find the contours */ // image conversion from rgb to grayscale cvCvtColor( img_seg, img_seg2, CV_BGR2GRAY ); // image thresholding to create a binary image cvThreshold( img_seg2, img_seg2, 1, 255, CV_THRESH_BINARY ); // find the contours CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; cvFindContours( img_seg2, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); for( ; contour != 0; contour = contour->h_next ) { CvScalar color = CV_RGB( (double)rand()/RAND_MAX*255, (double)rand()/RAND_MAX*255, (double)rand()/RAND_MAX*255); /* replace CV_FILLED with 1 to see the outlines */ cvDrawContours( img_seg, contour, color, color, 1, 1, 8 ); } //sets the correct origin so the image doesn't look wrong img_seg->origin = img_color->origin; //show the segmented image cvShowImage( "img_wnd_seg", img_seg); //defines either continuous mode/stepwise mode //int keyCode = cvWaitKey( ); if( cvWaitKey(5) >= 0 ) break; } // Release the capture device: cvReleaseCapture(&capture); /* Destroy windows */ cvDestroyWindow( "img_wnd" ); cvDestroyWindow( "img_wnd_seg" ); /* Destroy images */ cvReleaseImage( &img ); cvReleaseImage( &img_seg ); cvReleaseImage( &img_seg2 ); cvReleaseImage( &img_hsv ); //cvReleaseImage( &img_color ); cvReleaseImage( &img_hsvA ); cvReleaseImage( &img_h ); cvReleaseImage( &img_s ); cvReleaseImage( &img_v ); cvReleaseImage( &img_hA ); cvReleaseImage( &img_sA ); cvReleaseImage( &img_vA ); return 0; }