title: Removing partial borders with openCV
body: <p>I'm using OpenCV to find tabular data within images so that I can use an OCR on it. So far I have been able to find the table in the image, find the columns of the table, then find each cell within each column. It works pretty well, but I'm having an issue with the cell walls getting stuck in my images and I'm unable to remove them reliably.<a href="https://i.stack.imgur.com/Clpc7.png" rel="nofollow noreferrer">This is one example that I'm having difficulty with.</a><a href="https://i.stack.imgur.com/FFRLN.png" rel="nofollow noreferrer">This would be another example.</a></p>
  
  <p>I have tried several approaches to get these images better. I have been having the most luck with finding the contours.</p>
  
  <pre><code>img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
  image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
  ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY_INV)
  
  kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 3))
  
  # dilate , more the iteration more the dilation
  dilated = cv2.dilate(new_img, kernel, iterations=3)
  cv2.imwrite('../test_images/find_contours_dilated.png', dilated)
  </code></pre>
  
  <p>I have been toying with the kernel size and dilation iterations and have found this to be the best configuration.</p>
  
  <p>Another approach I used was with PIL, but it is only really good if the border is uniform around the whole image, which in my cases it is not.</p>
  
  <pre><code>copy = Image.fromarray(img)
  try:
      bg = Image.new(copy.mode, copy.size, copy.getpixel((0, 0)))
  except:
      return None
  diff = ImageChops.difference(copy, bg)
  diff = ImageChops.add(diff, diff, 2.0, -100)
  bbox = diff.getbbox()
  if bbox:
      return np.array(copy.crop(bbox))
  </code></pre>
  
  <p>There were a few other ideas that I tried, but none got me very far. Any help would be appreciated.</p>