title: how to extract the detected region in opencv?
body: <p>Everyone. I have shown below my code for tracking objects and it shows background subtraction result also. Here I am using frame differencing method. now my problem is that I have to extract that moving object from the color video file. I have done segmentation. But for detection I want to extract the region on which I have drawn bounding box. So can anybody help me in this...please. thank you in advance.</p>
  
  <pre><code> int main(int argc, char* argv[])
                 {
  
  
  
                  CvSize imgSize;
              //CvCapture *capture =     cvCaptureFromFile("S:\\offline object detection database\\video1.avi");
              CvCapture *capture =     cvCaptureFromFile("S:\\offline object detection database\\SINGLE PERSON Database\\Walk1.avi");
  
                  if(!capture){
                   printf("Capture failure\n");
                   return -1;
                  }
  
                   IplImage* frame=0;
                   frame = cvQueryFrame(capture);           
                   if(!frame)
           return -1;
  
                   imgSize = cvGetSize(frame);
  
                   IplImage* greyImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
                   IplImage* colourImage;
                   IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
                   IplImage* difference;
                   IplImage* temp;
                   IplImage* motionHistory = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
  
                   CvRect bndRect = cvRect(0,0,0,0);
                   CvPoint pt1, pt2;
                   CvFont font;
                   int prevX = 0;
                   int numPeople = 0;
                   char wow[65];
                   int avgX = 0;
                   bool first = true;                   
                   int closestToLeft = 0;
                   int closestToRight = 320;
  
  
                   for(;;)
                  {
                  colourImage = cvQueryFrame(capture);
                  if( !colourImage )
                  {
                       break;
                  }
                  if(first)
                  {
                   difference = cvCloneImage(colourImage);
                   temp = cvCloneImage(colourImage);
                   cvConvertScale(colourImage, movingAverage, 1.0, 0.0);
                   first = false;
                  }
                  else
                  {
                  cvRunningAvg(colourImage, movingAverage, 0.020, NULL);
                  }
                  cvConvertScale(movingAverage,temp, 1.0, 0.0);
              cvAbsDiff(colourImage,temp,difference);     
                  cvCvtColor(difference,greyImage,CV_RGB2GRAY);       
                  cvThreshold(greyImage, greyImage, 80, 250, CV_THRESH_BINARY);
          cvSmooth(greyImage, greyImage,2);
                  cvDilate(greyImage, greyImage, 0, 1);
                  cvErode(greyImage, greyImage, 0, 1);
          cvShowImage("back", greyImage);
                  CvMemStorage* storage = cvCreateMemStorage(0);
                  CvSeq* contour = 0;     
                  cvFindContours( greyImage, storage, &amp;contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  
          for( ; contour != 0; contour = contour-&gt;h_next )
                  {
                  bndRect = cvBoundingRect(contour, 0);          
              pt1.x = bndRect.x;
                  pt1.y = bndRect.y;
                  pt2.x = bndRect.x + bndRect.width;
                  pt2.y = bndRect.y + bndRect.height;         
                  avgX = (pt1.x + pt2.x) / 2;
  
                  if(avgX &gt; 90 &amp;&amp; avgX &lt; 250)
                  {
                  if(closestToLeft &gt;= 88 &amp;&amp; closestToLeft &lt;= 90)
                  {
                   if(avgX &gt; prevX)
                  {
                   numPeople++;
                   closestToLeft = 0;
                  }
                  }
                  else if(closestToRight &gt;= 250 &amp;&amp; closestToRight &lt;= 252)
                  {
                  if(avgX &lt; prevX)
                  {
                  numPeople++;
                  closestToRight = 220;
                  }
                  }                          
                  cvRectangle(colourImage, pt1, pt2, CV_RGB(255,0,0), 1);
                  }
                  if(avgX &gt; closestToLeft &amp;&amp; avgX &lt;= 90)
                  {
                   closestToLeft = avgX;
                   }
                   if(avgX &lt; closestToRight &amp;&amp; avgX &gt;= 250)
                   {
                   closestToRight = avgX;
                   }
                   prevX = avgX;
                   }
                   cvInitFont(&amp;font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
                   cvPutText(colourImage, _itoa(numPeople, wow, 10), cvPoint(60, 200), &amp;font, cvScalar(0, 0, 300));
                   cvShowImage("My Window", colourImage);
           cvShowImage("fore", greyImage); 
                   cvWaitKey(10);                     
                    }
                    cvReleaseImage(&amp;temp);
                    cvReleaseImage(&amp;difference);
                    cvReleaseImage(&amp;greyImage);
                    cvReleaseImage(&amp;movingAverage);
                    cvDestroyWindow("My Window");
                    cvReleaseCapture(&amp;capture);   
                    return 0;     
                    }
  </code></pre>