title: How to find the document edges in various coloured backgrounds using opencv python? [Document Scanning in various backgrounds]
body: <p>I am currently have a document that needs to be smart scanned.</p>
  
  <p>For that, I need to find proper contours of the document in any background so that I can do a warped perspective projection and detection with that image. </p>
  
  <p>The main issue faced while doing this is that the document edge detects any kind of background.</p>
  
  <p>I have tried to use the function HoughLineP and tried to find contours on the grayscale blurred image passed through canny edge detection until now.</p>
  
  <pre class="lang-py prettyprint-override"><code>
              MORPH = 9
              CANNY = 84
              HOUGH = 25
  
              IM_HEIGHT, IM_WIDTH, _ = rescaled_image.shape
  
              # convert the image to grayscale and blur it slightly
              gray = cv2.cvtColor(rescaled_image, cv2.COLOR_BGR2GRAY)
              gray = cv2.GaussianBlur(gray, (7,7), 0)
  
              #dilate helps to remove potential holes between edge segments
              kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(MORPH,MORPH))
              dilated = cv2.dilate(gray, kernel)
  
              # find edges and mark them in the output map using the Canny algorithm
              edged = cv2.Canny(dilated, 0, CANNY)
              test_corners = self.get_corners(edged)
  
              approx_contours = []
  
      (_, cnts, hierarchy) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
              cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
  
              # loop over the contours
              for c in cnts:
                  # approximate the contour
                  approx = cv2.approxPolyDP(c, 80, True)
                  if self.is_valid_contour(approx, IM_WIDTH, IM_HEIGHT):
                      approx_contours.append(approx)
                      break
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/xRfou.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xRfou.jpg" alt="Sample Image of Document"></a>
  How to find a proper bounding box around the document via OpenCV code.
  Any help will be much appreciated.
  (The document is taken from the camera in any angle and any coloured background.)</p>