title: Find contours of a square table (matrix shape) in an image using Python OpenCV
body: <p>I am new and I wonder how can I find the contours of the image like the below with Python OpenCV (cv2 library):</p>
  
  <p><a href="https://i.stack.imgur.com/mD9xw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mD9xw.jpg" alt="enter image description here"></a></p>
  
  <p>I am going to fill in each square a number and then convert it into numpy array, so I think I need to figure out how to get the contours of each square in the matrix first (maybe the coordinates of the square in the picture)</p>
  
  <p>I try to use some code snippet:</p>
  
  <pre><code>img = cv2.imread(img_path, 1)
  
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  binary = cv2.bitwise_not(gray)
  
  contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  
  for contour in contours:
      (x, y, w, h) = cv2.boundingRect(contour)
      cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
  </code></pre>
  
  <p>But it doesn't work</p>