title: How to discard the edges of an image using opencv?
body: <p>I'm pre-processing some images in order to remove the background from my area of ​​interest. However, the images on my bench have rounded edges due to the focus of the camera. How do I discard these rounded edges and be able to remove only my object of interest from the image? The code below I can remove the background of the image, but it does not work right due to the edges around.</p>
  
  <pre><code>import numpy as np
  import cv2
  
  #Read the image and perform threshold and get its height and weight
  img = cv2.imread('IMD408.bmp')
  h, w = img.shape[:2]
  
  # Transform to gray colorspace and blur the image.
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  blur = cv2.GaussianBlur(gray,(5,5),0)
  
  # Make a fake rectangle arround the image that will seperate the main contour.
  cv2.rectangle(blur, (0,0), (w,h), (255,255,255), 10)
  
  # Perform Otsu threshold.
  _,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
  
  # Create a mask for bitwise operation
  mask = np.zeros((h, w), np.uint8)
  
  # Search for contours and iterate over contours. Make threshold for size to
  # eliminate others.
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
  
  for i in contours:
      cnt = cv2.contourArea(i)
      if 1000000 &gt;cnt &gt; 100000:
          cv2.drawContours(mask, [i],-1, 255, -1)
  
  
  # Perform the bitwise operation.
  res = cv2.bitwise_and(img, img, mask=mask)
  
  # Display the result.
  cv2.imwrite('IMD408.png', res)
  cv2.imshow('img', res)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>input image:
  <a href="https://i.stack.imgur.com/RAL7W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RAL7W.png" alt="enter image description here"></a></p>
  
  <p>Exit:
  <a href="https://i.stack.imgur.com/Mh91B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mh91B.png" alt="enter image description here"></a></p>
  
  <p>Error:
  <a href="https://i.stack.imgur.com/7AlQP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7AlQP.png" alt="enter image description here"></a></p>