title: boundingrect not working for a contour in opencv c++
body: <p>{</p>
  
  <pre><code> #include&lt;opencv2\opencv.hpp&gt;
   #include&lt;iostream&gt;
   #include&lt;conio.h&gt;
  
   using namespace std;
   using namespace cv;
  
   int main()
  {
  int a = 0;
  
  Mat frame, diffimage,back,frame_gray;
  VideoCapture cap("D:\\elance\\check\\Sent3.avi");
  vector&lt;vector&lt;Point&gt;&gt; contours;
  BackgroundSubtractorMOG2 bg;
  vector&lt;int&gt; params;
  params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  params.push_back(9);
  
  for (int i = 0; i &lt; 200;i++)
  {
      cap &gt;&gt; frame;
      if (frame.empty())
          break;
      bg(frame,back);
  }
  
  bg.getBackgroundImage(back);
  cap.set(CV_CAP_PROP_POS_FRAMES,0);
  cvtColor(back, back, CV_BGR2GRAY);
  //for (int f = 0; f &lt; 20;f++)
  while (1)
  {
      a = a + 1;
      cout &lt;&lt; "Frame no : " &lt;&lt; a&lt;&lt;endl;
      cap &gt;&gt; frame;
      if (frame.empty())
          break;
      cvtColor(frame, frame_gray, CV_BGR2GRAY);
      absdiff(frame_gray, back, diffimage);
      threshold(diffimage, diffimage, 10, 255, CV_THRESH_BINARY);
  
      for (int i = 0; i &lt; 2; i++)
      {
          cv::erode(diffimage, diffimage, cv::Mat());
          cv::dilate(diffimage, diffimage, cv::Mat());
      }
      findContours(diffimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
      cout &lt;&lt; "Contour Size : " &lt;&lt; contours.size()&lt;&lt;endl;
      vector&lt;Rect&gt; boundRect(contours.size());
      for (int i = 0; i &lt; contours.size(); i++)
      {
          drawContours(frame, contours, i, cv::Scalar(0, 255, 255), 1);
          Mat smoothCont;
          smoothCont = cv::Mat(contours[i]);
          cout &lt;&lt; smoothCont.rows &lt;&lt; "\t" &lt;&lt; smoothCont.cols &lt;&lt;"\t"&lt;&lt;smoothCont.depth()&lt;&lt; endl &lt;&lt; endl;
          if (smoothCont.rows &gt; 0 &amp;&amp; smoothCont.rows &lt; 10000)
              boundRect[i] = boundingRect(Mat(contours[i]));
      }
      for (int i = 0; i &lt; contours.size(); i++)
          rectangle(frame, boundRect[i], Scalar(0, 255, 255), 1, 8, 0);
      imshow("Diff", diffimage);
      imshow("frame", frame);
      imwrite("D:\\test.jpg", frame, params);
      waitKey(30);
      break;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This code basically takes the contours and results are the rectangles on the contours. But  only one is bounded by the box and other contour is is still not bounded box. </p>
  
  <p>Can anyone help in this matter ? </p>