title: Join close enough contours in openCV
body: <p>I have a set of detected contours/blobs from an image.
  The problem is that some of the blobs are a split during blob detection and smoothing.
  I have tried to use the following code.</p>
  
  <pre><code>Mat outlines=Mat::zeros(m3.size(),CV_8UC3);
  findContours(m3,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point(0,0));
  //Approximate Contours
  std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours_poly( contours.size() );
  for( int i = 0; i &lt; contours.size(); i++ ) {
      approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 5, true );
      std::vector&lt;cv::Point&gt; hull;
      cv::convexHull(cv::Mat(contours_poly[i]),hull);
      cv::Mat hull_points(hull);
      cv::RotatedRect rotated_bounding_rect = minAreaRect(hull_points);
      Point2f vertices[4];
      if(rotated_bounding_rect.size.area()==0){
          continue;
      }
      rotated_bounding_rect.points(vertices);
      for (int i = 0; i &lt; 4; ++i)
      {
          cv::line(outlines, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
      }
  }
  </code></pre>
  
  <p>The problem is that even though these contours are detected and joined using the <code>approxPolyDP</code> method it leads to the disappearance of some small blobs even when they are alone and do not have any other blobs in the vicinity.</p>
  
  <p>What I want is for those blobs who are very near to be joined or at least one of them should be deleted from my list of contours and all the rest should be preserved.</p>
  
  <p>Below are the initial and final images which would make my question more clear.
  Initial Image:<br>
  <a href="http://sdrv.ms/1bp8x89" rel="nofollow noreferrer">http://sdrv.ms/1bp8x89</a> <br>
  Final Image:<br>
  <a href="http://sdrv.ms/1bp8tp5" rel="nofollow noreferrer">http://sdrv.ms/1bp8tp5</a></p>
  
  <p>As we can see in the final image the small blob on the right side of the image goes missing even when some of the blobs have not been joined properly.</p>
  
  <p>Also this technique is taking a few seconds for a single frame. Given a video this technique becomes very inefficient.</p>
  
  <p>Please suggest some remedies. </p>