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

Created on 31 Dec 2018  路  9Comments  路  Source: dlemstra/Magick.NET

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

question

Most helpful comment

Your command would translate to something like this:

```C#
// 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");
    }
}

}
```

All 9 comments

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.

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");

Can you share your input image?

@dlemstra here they are
Input Image :
image

This is what i expect as output :
result

Your command would translate to something like this:

```C#
// 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");
    }
}

}
```

It works. Thanks @dlemstra 馃挴

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

@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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Insire picture Insire  路  3Comments

trong8820 picture trong8820  路  8Comments

zhangnan0324 picture zhangnan0324  路  8Comments

shadowndacorner picture shadowndacorner  路  7Comments

cssriraman picture cssriraman  路  7Comments