title: opencv: Creating a binary mask from the image
body: <p><a href="https://i.stack.imgur.com/tisBy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tisBy.jpg" alt="enter image description here" /></a></p>
  <p>I have this picture and I want to make a binary mask from it. The main (the biggest) rectangle should be white and other parts of the picture shound be black. Like this:</p>
  <p><a href="https://i.stack.imgur.com/shxuX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/shxuX.jpg" alt="enter image description here" /></a></p>
  <p>To solve this problem I want to find a contour of main rectangle. My plan is to find all external contours. Then get the contour with the highest area. Firstly, I tried to find all contours and plot them as a binary image. My attempt:</p>
  <pre><code>import numpy as np 
  import cv2
  import matplotlib.pyplot as plt
  from skimage import data, io
  
  im = cv2.imread('train_1.jpg', 0)
  contours, hierarchy = cv2.findContours(im.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  out = np.zeros_like(im)
  cv2.drawContours(out, contours, -1, 255, 10)
  io.imshow(out)
  plt.show()
  </code></pre>
  <p>But even this simple code didn't find any contours. What's wrong with it? And how to get the contour with the highest area? Maybe I should apply Canny algorithm here? Please, help me.</p>