title: OpenCV C++/Obj-C: Connect nearby contours
body: <p>Is there a function to connect two (or more) nearby contours? Take a look at my in-/output and you'll see what I mean …</p>
  
  <p>My code:</p>
  
  <pre><code>[... some processing ...]
  
  // getting contours
  std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours;
  findContours(input, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  
  // 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 );
  }
  
  // debugging
  cv::Scalar colors[3];
  colors[0] = cv::Scalar(255, 0, 0);
  colors[1] = cv::Scalar(0, 255, 0);
  colors[2] = cv::Scalar(0, 0, 255);
  for (int idx = 0; idx &lt; contours_poly.size(); idx++) {
    cv::drawContours(output, contours_poly, idx, colors[idx % 3]);
  } 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ilGqD.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ilGqD.jpg" alt="output"></a>
  <a href="https://i.stack.imgur.com/sVG7z.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/sVG7z.jpg" alt="output"></a></p>