title: Remove text boxes for OCR with OpenCV
body: <p>I am trying to run OCR (using Google's Tesseract) on a document with the following format:</p>
  
  <p><a href="https://i.stack.imgur.com/YroD9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YroD9.png" alt="enter image description here"></a></p>
  
  <p>However, Tesseract assumes the short bars in between to be letters/numbers (l or i or 1).</p>
  
  <p>As a pre-processing measure I tried to remove vertical and horizontal lines using the following code:</p>
  
  <pre><code>import cv2
  
  from pdf2image import convert_from_path
  
  pages = convert_from_path('..\\app\\1.pdf', 500)
  for page in pages:
      page.save('..\\app\\out.jpg', 'JPEG')
  
  image = cv2.imread('out.jpg')
  result = image.copy()
  gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  
  # Remove horizontal lines
  horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
  remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
  cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      cv2.drawContours(result, [c], -1, (255,255,255), 5)
  
  # Remove vertical lines
  vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
  remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
  cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      cv2.drawContours(result, [c], -1, (255,255,255), 5)
  
  cv2.imshow('thresh', thresh)
  cv2.imshow('result', result)
  cv2.imwrite('result.png', result)
  cv2.waitKey()
  </code></pre>
  
  <p>I run into an issue where the output of this document removes most of the vertical and horizontal lines in the document even the start and the finish line on the left and right side of the image below but not the small bars in between.</p>
  
  <p>I'm wondering if I am going about this wrong by trying to pre-process and remove lines. Is there a better way to pre-process or another way to solve this problem?</p>