'How to remove all lines from an image? (horizontal, vertical, diagonal)

I need to remove the lines in an image, which is a table eventually. I have found a way to remove the horizontal and vertical lines:

convert 1.jpg -type Grayscale -negate -define morphology:compose=darken -morphology Thinning 'Rectangle:1x80+0+0<' -negate out.jpg

The following image:

enter image description here

Was converted to the following one:

enter image description here

As one can see the diagonal line is still there. I tried to rotate the image for 45 degrees and then try to remove it, but was also unsuccessful. How it can be done? Any suggestions are appreciated. I opted for imagemagick, but any other options are welcome



Solution 1:[1]

Here is another approach. I use Imagemagick, since I am not proficient with OpenCV. Basically, I binarize the image. Then do connected components processing to isolate the largest contiguous black region, which will be the black lines you want to exclude. Then use that as a mask to fill in white over the lines. This is Unix syntax with Imagemagick.

Note that some text characters will be lost, if they touch the black lines.

Input:

enter image description here

Get the id number of the largest black region:

id=`convert Arkey.jpg -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 null: |\
grep "gray(0)" | head -n 1 | sed -n 's/^ *\(.*\):.*$/\1/p'`


Isolate the black lines and dilate them

convert Arkey.jpg -threshold 50% -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:keep=$id \
-connected-components 4 \
-alpha extract \
-morphology dilate octagon:2 \
mask.png


enter image description here

Fill white over the lines in the image using the mask for control:

convert Arkey.jpg \( -clone 0 -fill white -colorize 100 \) mask.png -compose over -composite result.png


enter image description here

See -connected-components at https://imagemagick.org/script/connected-components.php for details how it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fmw42