I got the required segmentation by making changes to the above code in line:
kernel = np.ones((5,5), np.uint8)
I changed this to:
kernel = np.ones((5,100), np.uint8)
Now I get the output as follows
. It also works with handwritten text images with lines that are not perfectly horizontal:
EDIT: To get individual characters from a word, follow these steps:
Resize the outline containing the word using the code as follows.
im = cv2.resize(image,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
Apply the same contour detection process as with line segmentation, but with a core size of (5.5), i.e.
kernel = np.ones((5,5), np.uint8) img_dilation = cv2.dilate(im_th, kernel, iterations=1)
source share