title: Finding 4 corner polygon around contours, OpenCV
body: <p>I am trying to find a coordinates of a shape(s) in a white-black image. I am using findContours method for contour finding and approxPolyDP for optimizing them to a polygon. The shape in the input image (see below) is an processed text, I need to find a 4 corner polygon for each field, which would fit around this shape using less outside space. ApproxPolyDP function rarely gives me a 4 corners (despite changing parameters), which I need to use to apply perspective transform on an original image and skip the deskewing algorythm and to crop out the text. How can i find the best fitting 4 corner polygons for each field (<strong>not rectangles</strong>)? I could not find any proper tutorial on how to do that, is it really hard? Below I present my current code in java; desired result; input; current output. <em>NOTE</em>: I would highly appreciate if you could give me a method where HoughLines are not involved, this method is slow (for mobile phones; that's why I am asking this question), but if it is the only one possibility you know to get the result I need, please, post it, it would be appreciated.</p>
  
  <p>Code for finding current shape(s):</p>
  
  <pre><code>Mat mask = new Mat(src.size(), CvType.CV_8UC3, new Scalar(0,0,0));
  List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();
  Imgproc.findContours(src, contours, new Mat(), Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
  
  for (int i = 0 ; i &lt; contours.size() ; i++)
  {
      int contourSize = (int)contours.get(i).total();
  
      MatOfPoint2f curContour2f = new MatOfPoint2f(contours.get(i).toArray());
      Imgproc.approxPolyDP(curContour2f, curContour2f, 0.04 * Imgproc.arcLength(curContour2f, true), true);
      contours.set(i, new MatOfPoint(curContour2f.toArray()));
  
      Imgproc.drawContours(mask, contours, i, new Scalar(0, 255, 0), 3);
  }
  </code></pre>
  
  <p>Average input:</p>
  
  <p><a href="https://i.stack.imgur.com/QMTHv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QMTHv.jpg" alt="enter image description here"></a></p>
  
  <p>Desired result (it's not a rectangle, corners do not have to be 90 degrees, but here must be 4 of them):</p>
  
  <p><a href="https://i.stack.imgur.com/Qnpqg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qnpqg.jpg" alt="enter image description here"></a></p>
  
  <p>Average current output:</p>
  
  <p><a href="https://i.stack.imgur.com/tO6LM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tO6LM.jpg" alt="enter image description here"></a></p>
  
  <p>Other output example: the input picture here was more detailed (with some gaps), so the output is much worse depending on what I want it to be. Polygons in other polygons is not a problem, but the main shape of a whole block has to much corners:</p>
  
  <p><a href="https://i.stack.imgur.com/kwvz3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kwvz3.jpg" alt="enter image description here"></a></p>
  
  <p>Thank you in advance.</p>