title: How to find accurate corner positions of a distorted rectangle from blurry image in python?
body: <p>I am looking for a procedure to detect the corners of an distorted rectangle accurately with OpenCV in Python.</p>
  
  <p>I've tried the solution of different suggestions by googling, but through a sinusoidal superposition of a straight line (see the thresholded image) I probably can't detect the corners. I tried findContours and HoughLines so far without good results.
  Unfortunately I don't understand the C-Code from Xu Bin in <a href="https://stackoverflow.com/questions/19511019/how-to-find-blur-corner-position-with-opencv">how to find blur corner position with opencv?</a></p>
  
  <p>This is my initial image:</p>
  
  <p><a href="https://i.stack.imgur.com/OXqOm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OXqOm.png" alt="enter image description here"></a></p>
  
  <p>After resizing and thresholding I apply canny edge detection to get following image:</p>
  
  <p><img src="https://i.stack.imgur.com/FSAZJ.png" width="325"></p>
  
  <pre><code>contours, hierarchy = cv2.findContours(g_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  box = cv2.minAreaRect(contour)
  box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
  box = np.array(box, dtype="float")
  box = perspective.order_points(box)
  </code></pre>
  
  <p>I only get the following result with some extra drawing:</p>
  
  <p><img src="https://i.stack.imgur.com/l8xC7.png" width="325"></p>
  
  <p>I thought line fitting would be a good way to solve the problem, but unfortunately I couldn't get HoughLines working and after looking in <a href="https://stackoverflow.com/questions/40362117/opencv-python-how-to-implement-ransac-to-detect-straight-lines">OpenCV Python - How to implement RANSAC to detect straight lines?</a> RANSAC seems also difficult to apply for my problem.</p>
  
  <p>Any help is highly appreciated.</p>