This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
(3) Has read the previous tutorials in this series
The rest of the tutorial is presented as follows:

void my_mouse_callback( int event, int x, int y, int flags, void* param );
CvRect box;
bool drawing_box = false;
void draw_box( IplImage* img, CvRect rect ){
cvRectangle( img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height),
cvScalar(0xff,0x00,0x00) );
}
// Implement mouse callback
void my_mouse_callback( int event, int x, int y, int flags, void* param ){
IplImage* image = (IplImage*) param;
switch( event ){
case CV_EVENT_MOUSEMOVE:
if( drawing_box ){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = cvRect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
draw_box( image, box );
break;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Box Example";
box = cvRect(-1,-1,0,0);
IplImage* image = cvLoadImage( "MGC.jpg" );
cvZero( image );
IplImage* temp = cvCloneImage( image );
cvNamedWindow( name );
// Set up the callback
cvSetMouseCallback( name, my_mouse_callback, (void*) image);
// Main loop
while( 1 ){
cvCopyImage( image, temp );
if( drawing_box )
draw_box( temp, box );
cvShowImage( name, temp );
if( cvWaitKey( 15 )==27 )
break;
}
cvReleaseImage( &image );
cvReleaseImage( &temp );
cvDestroyWindow( name );
return 0;
}

int g_switch_value = 0;
int colorInt = 0;
// Trackbar/switch callback
void switch_callback( int position ){
if( position == 0 ){
colorInt = 0;
}else{
colorInt = 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Demo Window";
int radius = 30;
int thickness = 2;
int connectivity = 8;
CvScalar green = CV_RGB(0,250,0);
CvScalar orange = CV_RGB(250,150,0);
IplImage* src1 = cvLoadImage( "MGC.jpg" );
CvPoint pt2 = cvPoint(405,195);
cvNamedWindow( name, 1 );
cvShowImage(name, src1);
// Create trackbar
cvCreateTrackbar( "Switch", name, &g_switch_value, 1, switch_callback );
// Loop to update the circle color
while( 1 ) {
if( colorInt == 0)
cvCircle(src1,pt2,radius,green,thickness,connectivity);
else
cvCircle(src1,pt2,radius,orange,thickness,connectivity);
cvShowImage(name, src1);
if( cvWaitKey( 15 ) == 27 )
break;
}
cvReleaseImage( &src1 );
cvDestroyWindow( name );
return 0;
}
Click here to email me.
Click here to return to my Tutorials page.