title: How can I find contours in specific area of image using opencv and C++?
body: <p>I am new to opencv. I am trying to detect vehicles in a specific are of the image like x=0 to 139 and y=0 to 300 of a image with size (300, 400) approximately.</p>
  
  <p>So what condition should I use to detect it in specific area and to count the vehicle once it pass enters into the frame?</p>
  
  <pre><code>enter code herevoid processVideo(char* videoFilename) {
  //create the capture object
  `VideoCapture capture(videoFilename);
  if(!capture.isOpened()){
      //error in opening the video input
      cerr &lt;&lt; "Unable to open video file: " &lt;&lt; videoFilename &lt;&lt; endl;
      exit(EXIT_FAILURE);
  }
  //read input data. ESC or 'q' for quitting
  while( (char)keyboard != 'q' &amp;&amp; (char)keyboard != 27 ){
      //read the current frame
      if(!capture.read(frame)) {
          cerr &lt;&lt; "Unable to read next frame." &lt;&lt; endl;
          cerr &lt;&lt; "Exiting..." &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
      //update the background model
      pMOG2-&gt;apply(frame, fgMaskMOG2);
      Mat im_th, src_gray;
      cvtColor( frame, src_gray, COLOR_BGR2GRAY );
      medianBlur( fgMaskMOG2, fgMaskMOG2, 15 );
      threshold( fgMaskMOG2, im_th,230 , 255, THRESH_BINARY_INV);
  
          Mat im_floodfill = im_th.clone();
         floodFill(im_floodfill, cv::Point(0,0), Scalar(255));
  
         Mat im_floodfill_inv;
         bitwise_not(im_floodfill, im_floodfill_inv);
         fgMaskMOG2= im_floodfill_inv;
  
         dilate(fgMaskMOG2, fgMaskMOG2,Mat ());
         erode(fgMaskMOG2, fgMaskMOG2,Mat ());
         medianBlur( fgMaskMOG2, fgMaskMOG2, 15 );
         //adaptiveThreshold(fgMaskMOG2,fgMaskMOG2 ,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,11,2);
  
                 rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                           cv::Scalar(255,0,255), -1);
  
                 stringstream ss;
                 ss &lt;&lt; capture.get(CAP_PROP_POS_FRAMES);
                 string frameNumberString = ss.str();
                 putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
                         FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
                 //show the current frame and the fg masks
                 imshow("Frame", frame);
                 imshow("FG Mask MOG 2", fgMaskMOG2);
  
                vector&lt;vector&lt;Point&gt; &gt; contours;
                 vector&lt;Vec4i&gt; hierarchy;
  
                 findContours( fgMaskMOG2, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0,0) );
  
                 Mat drawing = frame;
  
                     Rect bounding_rect;
  
                     vector&lt;Rect&gt; boundRect( contours.size() );
                     vector&lt;vector&lt;Point&gt; &gt; contours_poly( contours.size() );
  
  
                     Scalar color( 255,0,0);  // color of the contour in the
                     //Draw the contour and rectangle
  
  
                     for( int i = 0; i &lt; s; i++ )
                         { //approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
                         approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
                           boundRect[i] = boundingRect( Mat(contours_poly[i]) );
  
                         }
  );
  
                     for( int i = 0; i&lt; s; i++ )
                         {
                         drawContours( frame, contours_poly, i, color, 1, 8, vector&lt;Vec4i&gt;(), 0, Point() );
                         rectangle( frame, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
                         }
                     imshow( "Display window", frame);
                 }
  
                 keyboard = waitKey(39);
  }
  </code></pre>