I am trying to pre-process my Images to remove Horizontal and Vertical Lines from them (required for some OCR). I got some Image Magick code to do the same which works fairly well. Below is the snippet:
magick convert ( img_name.jpg )
( -clone 0 -morphology close rectangle:1x50 -negate +write tmp1.png )
( -clone 0 -morphology close rectangle:50x1 -negate +write tmp2.png )
( -clone 1 -clone 2 -evaluate-sequence add +write tmp3.png )
-delete 1,2
-compose plus -composite
result.jpg
This was obtained from the below link (full credit to them):
Remove all horizontal and vertical lines from an image
The thing is even though Image Magick works well it takes a very long time on high resolution Images (which is what I mostly have). So I am trying to convert the above snippet to Open CV Python code. I have already written partial Code for steps 1 and 2.
Step 1 and 2:
img = cv2.imread('img_name.jpg', cv2.IMREAD_COLOR)
kernel_vertical = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
kernel_horizontal = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp_1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_vertical)
#cv2.imwrite('temp_1.png',temp_1)
temp_2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_horizontal)
#cv2.imwrite('temp_2.png',temp_2)
However I am unable to perform Step 3 and 4 because I am unsure what exactly the evaluate-sequence operator does. I have looked it up online but didn't understand well enough. If anybody is able to help me translate the remaining steps I would be very grateful.
Here is the equivalent commands in Python/OpenCV.
Input:
import cv2
import numpy as np
img = cv2.imread('document.png')
# do morphology to locate vertical lines and invert
kernel_vertical = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
temp1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_vertical)
temp1 = (255-temp1)
# do morphology to locate horizontal lines and invert
kernel_horizontal = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_horizontal)
temp2 = (255-temp2)
# add the two temp images together
temp = cv2.add(temp1, temp2)
# add temp to img to create result
result = cv2.add(temp, img)
# write result to disk
cv2.imwrite("document_lines_removed.png", result)
# display it
cv2.imshow("temp1", temp1)
cv2.imshow("temp2", temp2)
cv2.imshow("temp", temp)
cv2.imshow("result", result)
cv2.waitKey(0)