title: How to find the coordinates of the outside corners of these 4 squares? (morphological closing/opening does not conserve squares if image is rotated)
body: <p>One of the first processing steps in a tool I'm coding is to <strong>find the coordinates of the outside corners of 4 big black squares</strong>. They will then be used to do a homographic transform, in order to deskew / unrotate the image (a.k.a perspective transform), to finally get a rectangular image. Here is an example of - rotated and noisy - input (<a href="https://i.stack.imgur.com/qErnm.jpg" rel="nofollow noreferrer">download link here</a>):</p>
  
  <p><a href="https://i.stack.imgur.com/iOK64.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iOK64.jpg" alt="enter image description here"></a></p>
  
  <p>To keep the big squares only, I'm using <a href="https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html" rel="nofollow noreferrer">morphological transformations</a> like closing/opening:</p>
  
  <pre><code>import cv2, numpy as np
  img = cv2.imread('rotatednoisy-cropped.png', cv2.IMREAD_GRAYSCALE)
  kernel = np.ones((30, 30), np.uint8)
  img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
  cv2.imwrite('output.png', img)
  </code></pre>
  
  <p>Input file (<a href="https://i.stack.imgur.com/m8Nya.jpg" rel="nofollow noreferrer">download link</a>):</p>
  
  <p><a href="https://i.stack.imgur.com/m8Nya.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m8Nya.jpg" alt="enter image description here"></a></p>
  
  <p>Output, after morphological transform:</p>
  
  <p><a href="https://i.stack.imgur.com/BrJEu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BrJEu.png" alt="enter image description here"></a></p>
  
  <p>Problem: the output squares are not square anymore, and therefore <em>the coordinates of the top left corner of the square will be not precise at all</em>!</p>
  
  <p>I could reduce the kernel size, but then it would keep more unwanted small elements.</p>
  
  <p><strong>Question: how to get a better detection of the corners of the squares?</strong></p>
  
  <hr>
  
  <p>Note: </p>
  
  <ul>
  <li><p>As a <a href="https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html#closing" rel="nofollow noreferrer">morphological closing</a> is just a dilatation + an erosion, I found the culprit:</p>
  
  <pre><code>import cv2, numpy as np
  img = cv2.imread('rotatednoisy-cropped.png', cv2.IMREAD_GRAYSCALE)
  kernel = np.ones((30, 30), np.uint8)
  img = cv2.dilate(img, kernel, iterations = 1)
  </code></pre>
  
  <p>After this step, it's still ok:</p>
  
  <p><a href="https://i.stack.imgur.com/ez2NN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ez2NN.png" alt="enter image description here"></a></p>
  
  <p>Then</p>
  
  <pre><code>img = cv2.erode(img, kernel, iterations = 1)
  </code></pre>
  
  <p>gives</p>
  
  <p><a href="https://i.stack.imgur.com/Dokjy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dokjy.png" alt="enter image description here"></a></p>
  
  <p>and it's not ok anymore!</p></li>
  </ul>