title: OpenCV: detect flawed rectangle
body: <p>currently I'm working on a project where I try to find the corners of the rectangle's surface in a photo using OpenCV (<strong>Python</strong>, Java or C++)</p>
  
  <p>I've selected desired surface by filtering color, then I've got mask and passed it to the <code>cv2.findContours</code>:</p>
  
  <pre><code>cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
  peri = cv2.arcLength(cnt, True)
  approx = cv2.approxPolyDP(cnt, 0.02*peri, True)
  if len(approx) == 4:
      cv2.drawContours(mask, [approx], -1, (255, 0, 0), 2)
  </code></pre>
  
  <p>This gives me an inaccurate result:
  <a href="https://i.stack.imgur.com/wdhrH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wdhrH.png" alt="findContours result"></a></p>
  
  <p>Using <code>cv2.HoughLines</code> I've managed to get 4 straight lines that accurately describe the surface. Their intersections are exactly what I need:</p>
  
  <pre><code>edged = cv2.Canny(mask, 10, 200)
  hLines = cv2.HoughLines(edged, 2, np.pi/180, 200)
  lines = []
  for rho,theta in hLines[0]:
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a*rho
      y0 = b*rho
      x1 = int(x0 + 1000*(-b))
      y1 = int(y0 + 1000*(a))
      x2 = int(x0 - 1000*(-b))
      y2 = int(y0 - 1000*(a))
      cv2.line(mask, (x1,y1), (x2,y2), (255, 0, 0), 2)
      lines.append([[x1,y1],[x2,y2]])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/MvY15.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MvY15.png" alt="HoughLines result"></a></p>
  
  <p>The question is: is it possible to somehow tweak <code>findContours</code>?</p>
  
  <p>Another solution would be to find coordinates of intersections. Any clues for this approach are welcome :)</p>
  
  <p>Can anybody give me a hint how to solve this problem?</p>