How to crop biggest rectangle out of an image
<p>I have a few images of pages on a table. I would like to crop the pages out of the image. Generally, the page will be the biggest rectangle in the image, however, all four sides of the rectangle might not be visible in some cases. </p>

<p>I am doing the following but not getting desired results:</p>

<pre><code>import cv2
import numpy as np

im = cv2.imread('images/img5.jpg')
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)
_,contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("Show",im)
cv2.imwrite("images/img5_rect.jpg", im)
cv2.waitKey(0)
</code></pre>

<p>Below are a few examples:</p>

<p><strong>1st Example</strong>: I can find the rectangle in this image , however, would like if the remaining part of the wood can be cropped out as well.
<a href="https://i.stack.imgur.com/OBF2r.jpg"><img src="https://i.stack.imgur.com/OBF2r.jpg" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/hx8vL.jpg"><img src="https://i.stack.imgur.com/hx8vL.jpg" alt="enter image description here"></a></p>

<p><strong>2nd Example</strong>: Not finding the correct dimensions of the rectangle in this image. 
<a href="https://i.stack.imgur.com/z8TQm.jpg"><img src="https://i.stack.imgur.com/z8TQm.jpg" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/uWx6n.jpg"><img src="https://i.stack.imgur.com/uWx6n.jpg" alt="enter image description here"></a></p>

<p><strong>3rd Example</strong>: Not able to find the correct dimensions in this image either.
<a href="https://i.stack.imgur.com/ofWJG.jpg"><img src="https://i.stack.imgur.com/ofWJG.jpg" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/AaJ5r.jpg"><img src="https://i.stack.imgur.com/AaJ5r.jpg" alt="enter image description here"></a></p>

<p><strong>4th Example</strong>: Same with this as well.
<a href="https://i.stack.imgur.com/dSIqG.jpg"><img src="https://i.stack.imgur.com/dSIqG.jpg" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/VginS.jpg"><img src="https://i.stack.imgur.com/VginS.jpg" alt="enter image description here"></a></p>