Removing text baselines by morphological operations

Some weeks ago, I was developing a program to extract some specific informations from a photographed document, which is composed of many fields surrounded by layout lines. Most of the time those layout lines didn’t bothered me, but then I started finding a lot of sample images where the characters were touching the lines or were very very close to them, messing up with my text line segmentation strategy.

As I’m always trying to seek similarities between my problems and academical researches, I stumbled on an interesting paper that uses very simple procedures to remove handwritten text baselines. The technique described there uses mathematical morphology operations (basically, a sequence of dilations and erosions) on gray scale level, claiming that it avoids loosing too much information and generates smoother boundaries than that in binary images.

Let’s test it using small scripts in Octave:

img = imread('handwritten1.jpg' );
gray = rgb2gray(img);
h_gray = baseline_removal(gray, 30, 3);
subplot(1,2,1), imshow(gray)
subplot(1,2,2), imshow(h_gray)
[Baseline removal's result. The original image can be found at]
— Baseline removal's result. The original image can be found at

For me, the results are pretty impressive! Although some small parts of the lines were not totally removed, the text seems intact, and I didn’t even tunned the algorithm’s parameters.

My implementation of the technique, the function baseline_removal (that can be found in one of my github repositories), has 3 parameters: the grayscale image, the length of the shortest baseline feature to be eliminated and the height of the thickest part of the baselines. So, in the code above, the minimal length chosen was 30 and the height was 3, without any detailed analysis of the image, I just put some reasonable values.

This baseline_removal function has an optional parameter that was not used here. Its last and optional parameter is the angle from X-axis to X-Y projection of the line and it allows the removal of straight lines in any direction. As its default value is zero, we don’t need to worry about it for horizontal lines. If one wants to remove vertical lines, for example, the value 90 should be passed.


Ye, X., Cheriet, M., Suen, C., & Liu, K. (1999). Extraction of bankcheck items by mathematical morphology. International Journal on Document …, 2(2-3), 53–66. doi:10.1007/s100320050037

Do you wanna tell me something? Please, send me an email! =)

Published: July 22 2014