C# Imagemagick subtraction syntax

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
mfranquelo
Posts: 7
Joined: 2017-08-05T07:14:10-07:00
Authentication code: 1151

C# Imagemagick subtraction syntax

Post by mfranquelo » 2017-08-06T04:18:58-07:00

Dear all,

Could anyone point me how to do a subtraction between two images? I can't seem to find good documentation in C# (or similar) for this. Is it using the method FX or composite?

This is my code:

- Simple gaussian blur and difference between the original image and the gaussian blurred image.

Code: Select all

    class Program
    {
        static void Main(string[] args)
        {
            // Definir variables

            byte sigma;
            short radius;
            int originalX;
            int originalY;

            //Reading the file 

            using (MagickImageCollection images = new MagickImageCollection())
            {
                //First image
                MagickImage raw = new MagickImage(@"D:\test01.jpg");
                images.Add(raw);

                // Record original size
                originalX = raw.Width;
                originalY = raw.Height;

                // Gaussian Blur
                Console.WriteLine("Please enter sigma value for Gaussian blur:");
                radius = short.Parse(Console.ReadLine());
                Console.WriteLine("Please enter radius value for Gaussian blur:");
                sigma = byte.Parse(Console.ReadLine());

                    // Speed up
                    raw.Scale(new Percentage(10));
                    raw.GaussianBlur(radius,sigma);
                    raw.Resize(originalX,originalY);

                 // Write
                 raw.Write(@"D:\test01_gaussianblur.jpg");

                 // Second image
                 MagickImage blur = new MagickImage(@"D:\test01_gaussianblur.jpg");
                 images.Add(blur);

                 using (MagickImage flattened = images.Combine(); My doubt is here, how can i perform a subtraction?
             {
                 flattened.Write(@"D:\test01_flattened.jpg");
             }
         }
            Console.ReadLine();
         }
    }
}
Thank you,
Manuel.

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

Re: C# Imagemagick subtraction syntax

Post by dlemstra » 2017-08-06T05:02:52-07:00

What do you mean by subtraction? Have you done this one the command line and are you trying to convert those commands to c#?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

mfranquelo
Posts: 7
Joined: 2017-08-05T07:14:10-07:00
Authentication code: 1151

Re: C# Imagemagick subtraction syntax

Post by mfranquelo » 2017-08-06T05:13:24-07:00

I am trying to convert those commands to Magick.net C# syntax, yes.

For subtraction i mean that i want to output the mathematical difference between two inputs.
It works with image.Fx(u-0.5) for example. But i can't seem to figure out the structure for two images.

mfranquelo
Posts: 7
Joined: 2017-08-05T07:14:10-07:00
Authentication code: 1151

Re: C# Imagemagick subtraction syntax

Post by mfranquelo » 2017-08-06T05:42:48-07:00

The question is: How does magick know which one is "second" image?

The structure is like this first image (which is the one i am declaring) and [v] for second image, that i don't know where to declare it. :(

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

Re: C# Imagemagick subtraction syntax

Post by dlemstra » 2017-08-06T05:51:36-07:00

What is your ImageMagick command on the command line?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

mfranquelo
Posts: 7
Joined: 2017-08-05T07:14:10-07:00
Authentication code: 1151

Re: C# Imagemagick subtraction syntax

Post by mfranquelo » 2017-08-06T06:05:46-07:00

Im using Magick.net so i don't really know which is this particular command on the command line. I just want to output the difference between two images :? it could be related to this i guess: http://www.multipole.org/discourse-serv ... hp?t=16279

and this: https://www.imagemagick.org/Usage/compose/#difference

mfranquelo
Posts: 7
Joined: 2017-08-05T07:14:10-07:00
Authentication code: 1151

Re: C# Imagemagick subtraction syntax

Post by mfranquelo » 2017-08-06T08:11:30-07:00

according to the command line it seems i need to (compare them) raw.Compare(blur,ErrorMetric.Absolute, diffImage); and then compose them?

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

Re: C# Imagemagick subtraction syntax

Post by dlemstra » 2017-08-11T03:08:44-07:00

I think you just need to do this:

Code: Select all

raw.Composite(blur, CompositeOperator.ModulusSubtract);
This also means you don't need to MagickImageCollection.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Post Reply