title: Detecting and Removing Vertical and Horizontal Lines in OpenCV
body: <p>I'm trying to remove the square boxes(vertical and horizontal lines) from a filled out form using opencv (Python). I am trying to detect the vertical and horizontal lines through morphological operations of OpenCV. <a href="https://i.stack.imgur.com/SkY8h.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SkY8h.jpg" alt="The original image"></a></p>
  
  <p>After detecting the Vertical and Horizontal lines. <a href="https://i.stack.imgur.com/yFlF4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yFlF4.jpg" alt="Horizontal lines"></a></p>
  
  <p>Vertical Lines <a href="https://i.stack.imgur.com/JynnN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JynnN.jpg" alt="result of vertical lines "></a></p>
  
  <p>After the horizontal and vertical lines are detected , i am simply adding them and subtracting it from processed image.
  <code>res = verticle_lines_img + horizontal_lines_img</code> 
  <code>exp = img_bin - res</code></p>
  
  <p>The final results is not so smoothed as expected. 
  <a href="https://i.stack.imgur.com/wAteF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wAteF.jpg" alt="Final image after removing H and V lines "></a></p>
  
  <p>The full code for this is </p>
  
  <pre><code># Read the image
  img_for_box_extraction_path='aligned_filled.jpg'
  img = cv2.imread(img_for_box_extraction_path, 0)
  # Thresholding the image
  (thresh, img_bin) = cv2.threshold(img, 128, 255,cv2.THRESH_BINARY|     
  cv2.THRESH_OTSU)
  # Invert the image
  img_bin = ~img_bin
  cv2.imwrite("Image_bin.jpg",img_bin)
  bw = cv2.adaptiveThreshold(img_bin, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
                              cv2.THRESH_BINARY, 15, -2)
  horizontal = np.copy(bw)
  vertical = np.copy(bw)
  # Defining a kernel length for horizontal and vertical 
  cols = horizontal.shape[1]
  
  
  horizontal_size = int(cols)
  horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, 
  (horizontal_size, 1))
  # Apply morphology operations
  horizontal = cv2.erode(horizontal, horizontalStructure)
  horizontal = cv2.dilate(horizontal, horizontalStructure)
  rows = vertical.shape[0]
  
  verticalsize = int(rows)
  # Create structure element for extracting vertical lines through morphology 
  operations
  verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 
  verticalsize))
  # Apply morphology operations
  vertical = cv2.erode(vertical, verticalStructure)
  vertical = cv2.dilate(vertical, verticalStructure)
  #kernel_length = np.array(img).shape[1]//80
  #kernel_length = 7
  # A verticle kernel of (1 X kernel_length =6), which will detect all the 
  verticle lines from the image.
  verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 6))
  # A horizontal kernel of (kernel_length=7 X 1), which will help to detect 
  all the horizontal line from the image.
  hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 1))
  # A kernel of (3 X 3) ones.
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  
  
  # Morphological operation to detect vertical lines from an image
  img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)
  verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=2)
  cv2.imwrite("verticle_lines.jpg",verticle_lines_img)
  # Morphological operation to detect horizontal lines from an image
  
  
  img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)
  horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=2)
  cv2.imwrite("horizontal_lines.jpg",verticle_lines_img)
  
  
  res = verticle_lines_img + horizontal_lines_img
  #fin = cv2.bitwise_and(img_bin, img_bin, mask = cv2.bitwise_not(res))
  exp = img_bin - res
  exp = ~exp
  cv2.imwrite("final.jpg",exp)
  </code></pre>
  
  <p>What could be a novel way to detect and remove the square boxes?</p>