Remove all horizontal and vertical lines from an image

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
lucky_hieu09
Posts: 3
Joined: 2020-01-02T02:28:09-07:00
Authentication code: 1152

Remove all horizontal and vertical lines from an image

Post by lucky_hieu09 »

i using C# code remove line of table with code :

Code: Select all

private static void RemoveLines(IMagickImage original, IMagickImage image, string geometryValue)
        {
            // -scale x1! and -scale 1x!
            image.Scale(new MagickGeometry(geometryValue));

            // -scale 321x522! (resize to original width/height)
            MagickGeometry geometry = new MagickGeometry(original.Width, original.Height);
            geometry.IgnoreAspectRatio = true;
            image.Scale(geometry);

            image.AutoLevel(); // -auto-level
            image.Threshold( new Percentage(80)); // -threshold 50%
            image.Morphology(MorphologyMethod.Erode, Kernel.Diamond, 2); // -morphology erode diamond:2

            // ( -clone 1 )
            using (IMagickImage clone = image.Clone())
            {
                image.Negate();
                image.Composite(original, Gravity.Center);
             
            }
        }
but when run, it will clear all text and return the white image.
Can you guys help me fix the above error?

User avatar
dlemstra
Posts: 1610
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Remove all horizontal and vertical lines from an image

Post by dlemstra »

Could you add a link to the image?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

lucky_hieu09
Posts: 3
Joined: 2020-01-02T02:28:09-07:00
Authentication code: 1152

Re: Remove all horizontal and vertical lines from an image

Post by lucky_hieu09 »

ok sr you.
image before :https://prnt.sc/qipjvx
image after :https://prnt.sc/qipl8r

thanks bro.

User avatar
fmw42
Posts: 26281
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove all horizontal and vertical lines from an image

Post by fmw42 »

You can do this with morphology. Here are some command line commands. Sorry I do not know Magick.Net


Input:
Image

# extract vertical lines of length 50 (needs to be longer than any vertical text)

Code: Select all

convert Unknown.jpeg -morphology close rectangle:1x50 vertical.png
Image

# extract horizontal lines of length 345 (could use 50 also, needs to be longer than any horizontal text)

Code: Select all

convert Unknown.jpeg -morphology close rectangle:345x1 horizontal.png
Image

# combine vertical and horizontal lines, negate and threshold. Then use as mask to fill with white to cover the lines in your input.

Code: Select all

convert Unknown.jpeg \( +clone -fill white -colorize 100 \) \( vertical.png horizontal.png -negate -threshold 0 -compose plus -composite +write lines.png \) -compose over -composite Unknown_nolines.png
Lines:
Image

Result:
Image

lucky_hieu09
Posts: 3
Joined: 2020-01-02T02:28:09-07:00
Authentication code: 1152

Re: Remove all horizontal and vertical lines from an image

Post by lucky_hieu09 »

thanks bro !
i use ImageMagick.Net
- # extract vertical lines of length 50 (needs to be longer than any vertical text)
i convert to C# :image.Morphology(MorphologyMethod.Close, "rectangle:1x50");
- # extract horizontal lines of length 345 (could use 50 also, needs to be longer than any horizontal text)
image.Morphology(MorphologyMethod.Close, "rectangle:345x1");
- # combine vertical and horizontal lines, negate and threshold. Then use as mask to fill with white to cover the lines in your input.

I convert to C # and I run wrong. Can you convert it to help me?

Code: Select all

         MagickImage image = new MagickImage(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture.JPG"); // image001.tif
            image.Morphology(MorphologyMethod.Close, "rectangle:345x1");
            //image.Threshold(new Percentage(0)); // -threshold 50%
            //image.Negate();
            image.Write(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture1.JPG");
            image = new MagickImage(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture.JPG");
            image.Morphology(MorphologyMethod.Close, "rectangle:1x50");
            //image.Threshold(new Percentage(0)); // -threshold 50%
             //image.Negate();
            image.Write(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture2.JPG");
        using (IMagickImage clone = image.Clone())
            {
            // +clone -fill white -colorize 100
                clone.Colorize(MagickColors.White, new Percentage(100));
                image.Write(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture3.JPG");

               // vertical.png horizontal.png -negate -threshold 0
                MagickImage horizontal = new MagickImage(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture1.JPG");
                MagickImage vertical = new MagickImage(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture2.JPG");
                vertical.Threshold(new Percentage(0)); // -threshold 50%
                vertical.Negate();

              // -compose plus -composite +write lines.png
                var image_over = new MagickImage(@"D:\Source\src\RPA.Nals\bin\Debug\temp\Out_Img_RowsCells\Capture.JPG");
                image_over.Composite(image, 1, 0, CompositeOperator.Plus);

               //[b][/b]-compose over -composite Unknown_nolines.png ==>I convert to C # and I run wrong
                
            }

User avatar
fmw42
Posts: 26281
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove all horizontal and vertical lines from an image

Post by fmw42 »

Sorry, as I said, I do not know Magick.net. Perhaps the developer can help you when he has a chance.

SLS8
Posts: 6
Joined: 2020-03-03T11:26:22-07:00
Authentication code: 1152

Re: Remove all horizontal and vertical lines from an image

Post by SLS8 »

With morphology is a very imagemagicky way of doing it but with direct access to pixels I would just read the pixels from left to right in the middle of the height and see when it starts to be black(er) than white twice, start and end of the first vertical line. Then from the top to bottom to find the horizontal frame lines. Using that you can CopyPixels for all 9 rectangles (they're all identical in size) and put them in new white image.

If the image isn't aligned to the axes like in the sample you provided then find 2 corners and straighten it.

Post Reply