#include #include #include #include IplImage *image = 0, *mask = 0; int select_object = 0; int track_object = 0; CvPoint origin; CvRect selection; CvRect track_window; CvBox2D track_box; CvConnectedComp track_comp; void on_mouse( int event, int x, int y, int flags, void* param ) { if( !image ) return; if( image->origin ) y = image->height - y; if( select_object ) { selection.x = MIN(x,origin.x); selection.y = MIN(y,origin.y); selection.width = selection.x + CV_IABS(x - origin.x); selection.height = selection.y + CV_IABS(y - origin.y); selection.x = MAX( selection.x, 0 ); selection.y = MAX( selection.y, 0 ); selection.width = MIN( selection.width, image->width ); selection.height = MIN( selection.height, image->height ); selection.width -= selection.x; selection.height -= selection.y; } switch( event ) { case CV_EVENT_LBUTTONDOWN: origin = cvPoint(x,y); selection = cvRect(x,y,0,0); select_object = 1; break; case CV_EVENT_LBUTTONUP: select_object = 0; if( selection.width > 0 && selection.height > 0 ) track_object = -1; break; } } int main( int argc, char** argv ) { IplImage* frame = 0; IplImage* clip =0; int c; cvNamedWindow( "DemoSelection", 1 ); cvNamedWindow( "Selection", 1 ); cvNamedWindow( "Clip", 1 ); cvSetMouseCallback( "Selection", on_mouse, 0 ); if( argc == 2 && (frame=cvLoadImage(argv[1], 1))!= 0) { if( !frame ) exit(0); if( !image ) { /* allocate all the buffers */ image = cvCreateImage( cvGetSize(frame), 8, 3 ); image->origin = frame->origin; clip = cvCreateImage( cvGetSize(frame), 8, 3 ); clip->origin = frame->origin; // mask = cvCreateImage( cvGetSize(frame), 8, 1 ); } } else exit(0); for(;;) { cvCopy( frame, image, 0 ); cvCopy( frame, clip, 0 ); if( select_object && selection.width > 0 && selection.height > 0 ) { // Destaca a regiao selecionada cvSetImageROI( image, selection ); cvXorS( image, cvScalarAll(255), image, 0 ); cvResetImageROI( image ); // Exibe uma janela com a regiao selecionada cvSetImageROI( clip, selection ); cvShowImage( "Clip", clip ); cvResetImageROI( clip ); } cvShowImage( "DemoSelection", frame ); cvShowImage( "Selection", image ); c = cvWaitKey(10); if( (char) c == 27 ) break; if( (char) c == 's') { cvSetImageROI( clip, selection ); cvSaveImage("out.png",clip); cvResetImageROI( clip ); } } // cvWaitKey(0); cvDestroyWindow("DemoSelection"); cvDestroyWindow("Selection"); return 0; }