Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jayantrajwani opened this issue Dec 31, 2018 · 9 comments

Comments

Projects
None yet
2 participants
@jayantrajwani
Copy link

commented Dec 31, 2018

Prerequisites

  • I have written a descriptive issue title
  • 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 jayantrajwani changed the title How do I do this in Magick.net Need help with Morphology, How do I do this in Magick.net Dec 31, 2018

@jayantrajwani jayantrajwani changed the title Need help with Morphology, How do I do this in Magick.net Need help with Morphology, How do I execute this command in Magick.net ( command in description ) Dec 31, 2018

@dlemstra

This comment has been minimized.

Copy link
Owner

commented Dec 31, 2018

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.

@jayantrajwani

This comment has been minimized.

Copy link
Author

commented Dec 31, 2018

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

This comment has been minimized.

Copy link
Owner

commented Dec 31, 2018

Can you share your input image?

@jayantrajwani

This comment has been minimized.

Copy link
Author

commented Jan 2, 2019

@dlemstra here they are
Input Image :
image

This is what i expect as output :
result

@dlemstra dlemstra added the question label Jan 3, 2019

@dlemstra

This comment has been minimized.

Copy link
Owner

commented Jan 3, 2019

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");
        }
    }
}
@jayantrajwani

This comment has been minimized.

Copy link
Author

commented Jan 4, 2019

It works. Thanks @dlemstra 💯

@jayantrajwani

This comment has been minimized.

Copy link
Author

commented Jan 7, 2019

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

This comment has been minimized.

Copy link
Author

commented Jan 10, 2019

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

@dlemstra

This comment has been minimized.

Copy link
Owner

commented Jan 10, 2019

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

@dlemstra dlemstra reopened this Jan 10, 2019

@dlemstra dlemstra closed this Mar 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.