title: OpenCV: Incorrect contour around blobs
body: <p>I'm trying to draw contours around blobs in a binary image, however, sometimes, openCV draws a single contour around two distinct blobs. below is an example. How can i solve this issue?
  <img src="https://i.stack.imgur.com/ypoae.png" alt="alt text"></p>
  
  <p>Here it should draw two bounding boxes for the blob on the right and separately for the one of the left. I agree they are close but enough distance in between them. I'm only drawing External contours instead of the tree or list. I'm also using cvFindNextContour(contourscanner) as this is a easier implementation for my case.</p>
  
  <p>Thanks</p>
  
  <p>EDIT:
  Image displayed in the "output" window is from a different function which does just image subtraction. Image displayed in the "contours" window is in the function pplfind(). "output" image is passed to img_con().
  <pre><code>
  IplImage* img_con(IplImage* image){
      int ppl;
      CvMemStorage* memstr = cvCreateMemStorage();
      IplImage* edges = cvCreateImage(cvGetSize(image),8,1);
      cvCanny(image,edges,130,255);
      CvContourScanner cscan = cvStartFindContours(image,memstr,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,cvPoint(0,0));</p>
  
  <pre><code>ppl = pplfind(cscan,cvGetSize(image));
  if (ppl !=0 )
      printf("Estimated number of people: %d\n",ppl);
  cvEndFindContours(&amp;cscan);
  cvClearMemStorage(memstr);
  
  return edges;
  </code></pre>
  
  <p>}</p>
  
  <p>int pplfind(CvContourScanner cscan, CvSize frSize){
      ofstream file; char buff[50];
      file.open("box.txt",ofstream::app);
      int ppl =0;
      CvSeq* c;
      IplImage *out = cvCreateImage(frSize,8,3);
      while (c = cvFindNextContour(cscan)){
          CvRect box = cvBoundingRect(c,1);
          if ((box.height > int(box.width*1.2))&amp;&amp;(box.height>20)){//&amp;&amp;(box.width&lt;20)){//
              ppl++;
              cvRectangle(out,cvPoint(box.x,box.y),cvPoint(box.x+box.width,box.y+box.height),CV_RGB(255,0,50),1);</p>
  
  <pre><code>        cvShowImage("contours",out);
          //cvWaitKey();
      }
      //printf("Box Height: %d , Box Width: %d ,People: %d\n",box.height,box.width,ppl);
      //cvWaitKey(0);
      int coord = sprintf_s(buff,"%d,%d,%d\n",box.width,box.height,ppl);
      file.write(buff,coord);
  }
  file.close();
  cvReleaseImage(&amp;out);
  return ppl;
  </code></pre>
  
  <p>}
  </pre></code></p>