title: Setting ROI with mouse from a rectangle on a video
body: <p>I have video, when the program is run the video's first frame is taken as an image and the user is allowed to draw a rectangle on the image, after the rectangle is drawn, the user must right click on the image to confirm the rectangle. When the mouse the right-clicked the image disappears and the video starts to play with the drawn rectangle on it.</p>
  
  <p>I am able to draw the rectangle perfectly but I can't set that rectangle as ROI.</p>
  
  <p>What I want to do is to set that rectangle as Region of Interest (ROI) to do some image processing on that ROI. I am unable to set the rectangle which I draw as ROI.</p>
  
  <p>I am using OpenCV with Visual Studio 2010. Later on I will try to integrate this program in QT creator.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <p>My full code is as follows:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  #include&lt;opencv2\opencv.hpp&gt;
  #include &lt;opencv2\highgui\highgui.hpp&gt;
  #include &lt;opencv/highgui.h&gt;
  #include &lt;opencv/cxcore.h&gt;
  #include &lt;opencv\cvaux.h&gt;
  
  using namespace cv;
  using namespace std;
  void my_mouse_callback( int event, int x, int y, int flags, void* param );
  bool destroy=false;
  CvRect box;
  IplImage* image;
  IplImage* frame2;
  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(0,0,255) ,2);
  
  CvRect rect2=cvRect(box.x,box.y,box.width,box.height);
  //cvSetImageROI(image, rect2);   //here I wanted to set the drawn rect as ROI
  }
  
  // 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 &lt; 0 )
          {
              box.x += box.width;
              box.width *= -1;
          }
          if( box.height &lt; 0 )
          {
              box.y += box.height;
              box.height *= -1;
          }
          draw_box( image, box);
          break;
      case CV_EVENT_RBUTTONUP:
          destroy=true;
     }
  }
  
  int main()
  {
      const char* name = "Box Example";
      cvNamedWindow( name );
  
     box = cvRect(0,0,1,1);
  
     CvCapture* capture = cvCreateFileCapture( "C:\\video.mp4" );
     image = cvQueryFrame( capture );
  
    IplImage* temp = cvCloneImage( image );
   // Set up the callback
    cvSetMouseCallback( name, my_mouse_callback, (void*) image);
  
  
  //IplImage *img2 = cvCreateImage(cvGetSize(temp),temp-&gt;depth,temp-&gt;nChannels);
  
  //cvNot(temp,temp);
     /* copy subimage */
     //cvCopy(temp, temp, NULL);
  
    // Main loop
     while( 1 )
  {
      if(destroy) {cvDestroyWindow(name); break;}
      cvCopyImage( image, temp );
      if( drawing_box ) 
          draw_box( temp, box );
      cvMoveWindow(name, 200, 100);
      cvShowImage( name, temp );
  
      if( cvWaitKey( 15 )==27 ) 
          break;
   }
  
   //cvReleaseImage( &amp;image );
    cvReleaseImage( &amp;temp );
   cvDestroyWindow( name );
  
   cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
    cvMoveWindow("Example2", 150, 150);
  
  
    while(1) 
      {
  frame2 = cvQueryFrame( capture );
  draw_box(frame2,box);
      if( !frame2 ) break;
          cvShowImage( "Example2", frame2 );
          char c = cvWaitKey(33);
      if( c == 27 ) break;
     }
  cvReleaseCapture( &amp;capture );
  cvDestroyWindow( "Example2" );
     return 0;
  }
  </code></pre>