Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

Need help with Morphology, How do I execute this command in Magick.net ( command in description )

Open jayantrajwani opened this issue 7 years ago • 10 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am using the latest version of Magick.NET

System Configuration

  • Magick.NET version: 7.9.2
  • Environment (Operating system, version and so on): Windows 10

Question

I want to remove all horizontal and vertical lines from an image Below is the command for what i want to do using C#

magick ( 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

jayantrajwani avatar Dec 31 '18 06:12 jayantrajwani

What have you tried so far? Most options are a method of the MagickImage class and when you work with multiple images you probably want to use the MagickImageCollection also.

dlemstra avatar Dec 31 '18 11:12 dlemstra

I have been using AForge to detect quadilateral shapes and replace them with white boxes till now.

But i also want to remove other vertical and horizontal lines as well. I dont have much idea of magick.net but i was able to do it via console using image magick using the following command:

magick ( 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

I want a equivalent C# code using magick.net something like this

IMagickImage image = new MagickImage(@"C:\Users\Jayant\Desktop\test images\Image.tiff");
image = image.Clone();
var settings = new MorphologySettings();
settings.Channels = Channels.Alpha;
settings.Method = MorphologyMethod.Distance;
settings.Kernel = Kernel.Euclidean;
settings.KernelArguments = "1,10!";

image.Alpha(AlphaOption.Set);
image.VirtualPixelMethod = VirtualPixelMethod.Transparent;
image.Morphology(settings);
image.Write(@"C:\Users\Jayant\Desktop\test images\result.png");

jayantrajwani avatar Dec 31 '18 12:12 jayantrajwani

Can you share your input image?

dlemstra avatar Dec 31 '18 14:12 dlemstra

@dlemstra here they are Input Image : image

This is what i expect as output : result

jayantrajwani avatar Jan 02 '19 06:01 jayantrajwani

Your command would translate to something like this:

// magick ( image.png 
using (var image = new MagickImage(@"I:\issues\magick\367\image.png"))
{
    // -alpha off ) ^
    image.Alpha(AlphaOption.Off);

    using (var images = new MagickImageCollection())
    {
        // ( -clone 0
        var tmp1 = image.Clone();

        // -morphology close rectangle:1x50
        tmp1.Morphology(MorphologyMethod.Close, "rectangle:1x50");

        // -negate
        tmp1.Negate();

        // +write tmp1.png ) ^
        tmp1.Write(@"I:\issues\magick\367\tmp1.png");

        images.Add(tmp1);

        // ( -clone 0
        var tmp2 = image.Clone();

        // -morphology close rectangle:50x1
        tmp2.Morphology(MorphologyMethod.Close, "rectangle:50x1");

        // -negate
        tmp2.Negate();

        // +write tmp2.png ) ^
        tmp2.Write(@"I:\issues\magick\367\tmp2.png");

        images.Add(tmp2);

        // -evaluate-sequence add
        using (var tmp3 = images.Evaluate(EvaluateOperator.Add))
        {
            // +write tmp3.png ) ^
            tmp3.Write(@"I:\issues\magick\367\tmp3.png");

            // -compose plus -composite ^
            image.Composite(tmp3, CompositeOperator.Plus);

            // result.png
            image.Write(@"I:\issues\magick\367\result.png");
        }
    }
}

dlemstra avatar Jan 03 '19 21:01 dlemstra

It works. Thanks @dlemstra 💯

jayantrajwani avatar Jan 04 '19 06:01 jayantrajwani

Hi @dlemstra

I am trying to extract tables from an image and am looking for a generic solution to be able to extract multiple tables from an image without repetition and ignore large borders covering entire image. Below is a command to do something similar. How do i convert the following command to get a list of Images as output. Hope you can help.

IFS=" "
OLDIFS=$IFS
IFS=$'\n'
bboxArr=(`convert image.png -alpha off -type bilevel \
-define connected-components:verbose=true \
-define connected-components:area-threshold=500 \
-connected-components 4 \
null: | grep "gray(0)" | awk '{print $2}'`)
num=${#bboxArr[*]}
IFS=$OLDIFS
for ((i=0; i<num; i++)); do
convert image.png -crop ${bboxArr[$i]} +repage image$i.png

jayantrajwani avatar Jan 07 '19 06:01 jayantrajwani

@dlemstra I have closed this issue as the actual problem statement got solved but would be great if you could provide a solution to the above comment.

jayantrajwani avatar Jan 10 '19 06:01 jayantrajwani

And what have you tried yourself? The MagickImage class contains a ConnectedComponents method that should give you all the information that you need.

dlemstra avatar Jan 10 '19 08:01 dlemstra

Please don't hijack an old topic for a new question. Please start a new discussion with your question.

dlemstra avatar Aug 20 '24 18:08 dlemstra