title: Detection of four corners of a document under different circumstances
body: <p>I have tried 2 methodologies as follows:-</p>
  
  <ol>
  <li><p>conversion of image to Mat</p></li>
  <li><p>apply gaussian blur</p></li>
  <li><p>then canny edge detection</p></li>
  <li><p>find contours</p></li>
  </ol>
  
  <p>The problem with this method is:</p>
  
  <ol>
  <li>too many contours are detected</li>
  <li>mostly open contours</li>
  <li>doesn't detect what I want to detect</li>
  </ol>
  
  <p>Then I changed my approach and tried adaptive thresholding after gaussian blur/median blur and it is much better and I am able to detect the corners in 50% cases</p>
  
  <p>The current problem I am facing is that the page detection requires contrasting and plain background without any reflections. I think it's too idealistic for real world use.</p>
  
  <p>This is where I would like some help. Even a direction towards the solution is highly appreciated especially in java. Thanks in anticipation
  <a href="https://i.stack.imgur.com/7FJDd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7FJDd.jpg" alt=""></a>
  works absolutely fine with a significant contrasting background like this</p>
  
  <p><a href="https://i.stack.imgur.com/4N4XC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4N4XC.png" alt=""></a></p>
  
  <p>Detected 4 corners
  <a href="https://i.stack.imgur.com/Qgw9O.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qgw9O.jpg" alt="enter image description here"></a>
  This picture gives troubles because the background isn't exactly the most contrasting
  <a href="https://i.stack.imgur.com/V0sz1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V0sz1.png" alt="enter image description here"></a></p>
  
  <p>Initial largest contour found</p>
  
  <p><strong>Update:</strong> median blur did not help much so I traced the cause and found that the page boundary was detected in bits and pieces and not a single contour so it detected the biggest contour as a part of the page boundary Therefore performed some morphological operations to close relatively small gaps and the resultant largest contour is definitely improved but its its not optimum. Any ideas how I can improve the big gaps?
  <a href="https://i.stack.imgur.com/Cldj5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cldj5.png" alt="enter image description here"></a></p>
  
  <p>morphed original picture</p>
  
  <p><a href="https://i.stack.imgur.com/24i1D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/24i1D.png" alt="enter image description here"></a></p>
  
  <p>largest contour found in the morphed image</p>
  
  <p>PS morphing the image in ideal scenarios has led to detection of false contour boundaries. Any condition which can be checked before morphing an image is also a bonus. Thank you</p>