Remove all horizontal and vertical lines from an image
Asked Answered
F

2

3

I want to remove all horizontal and vertical lines but some small vertical lines are not getting removed. Adding the input and output images and the code below.

            string ImageUrl = @"C:\Users\Jayant\Desktop\test images\rtaImage.tiff";
            Image<Bgr, Byte> image = new Image<Bgr, byte>(ImageUrl);
            Image<Bgr, byte> res = image.Copy();

            LineSegment2D[] lines =
                image
                    .Convert<Gray, byte>()
                    .Canny(16, 16)
                    .HoughLinesBinary(1, Math.PI / 16, 10, 50, 1)[0];

            foreach (LineSegment2D line in lines)
            {
                res.Draw(line, new Bgr(System.Drawing.Color.White), 2);
            }

            res.Save(ImageUrl);

I want to remove all horizontal and vertical lines but some small vertical lines are not getting removed. Adding the input and output of above code.

input image :

enter image description here

ouptut image :

enter image description here

If you notice some vertical lines did not get removed. I an using emgu.cv library in Visual Studio and the code is C# . Any solution without using emgu will also be appreciated

Frawley answered 28/12, 2018 at 7:54 Comment(1)
Don’t use Hough for this. Use morphological closings with a horizontal and with a vertical line structuring element.Agrippina
F
6

In Imagemagick, you can use morphology close, but the result must be combined back with the original to remove the lines. The morphology close makes short horizontal or vertical segments white and leaves the long black lines. So the result must be negated and added to the original. It is important to make the morphology lines smaller than the shortest line segment, but longer than any parts of the text. So below, I process the image for the vertical lines and negate. Then repeat for the horizontal lines white and negate. Then I combine the two sets of lines and add them together. Finally I add the combined lines to the original image.

Input: enter image description here

Imagemagick 6, Unix Syntax:

convert \( image.png -alpha off \) \
\( -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.png


Imagemagick 6 Windows Syntax:

convert ( image.png -alpha off ) ^
( -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.png


tmp1 (vertical lines): enter image description here

tmp2 (horizontal lines): enter image description here

tmp3 (combined and negated lines): enter image description here

Result: enter image description here

For Imagemagick 7, change convert to magick.

You should be able to do this in Windows Imagemagick with Magick.NET. See https://github.com/dlemstra/Magick.NET. But I suspect your emgu.cv tool has the same morphology tools.

Fixity answered 28/12, 2018 at 18:6 Comment(4)
Thank you for the answer. Helped me understand it really well just one change we should write magick in place of convert as when we installl it on windows the file is magick.exe at least for windows and it looks like magick.net is exactly what i need but not sure how to replicate the same command in C#. would be great if you can help with that too or refer to any resources.Frawley
anyway to find the tables in an image?Frawley
@Jayant Rajwani Sorry, I do not understand what you want. What part do you consider the "table"?Fixity
See #54041826Fixity
U
1

You can do this with ImageMagick library easily, just use some predefined filter

I am not sure this works with tiff format , you may need convert your tiff file to bmp/png/jpb first

This removes horizontal lines

 convert rtaImage.png -morphology close:1 "1x5: 0,1,1,1,0"

this removes both

 convert rtaImage.png -convolve "0,$v,0,0,$v,0,0,$v,0" -threshold 99% rtaImage2.png
Uncaredfor answered 28/12, 2018 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.