difference and mask

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
Ln79
Posts: 4
Joined: 2018-01-09T02:47:12-07:00
Authentication code: 1152

difference and mask

Post by Ln79 » 2018-01-09T03:33:48-07:00

Hello,

I have two images, I need to get the difference : How can I do that with Image Magik ?

I have tried this command line found on this topic : viewtopic.php?f=1&t=15584#p55309

Code: Select all

convert cyclops_question.png cyclops.png \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
cyclops_sep.png
It's not exactly what I want, but it's a beginning. Anyway, how transform this command line in Magick.Net ?

I have found this code, too :

Code: Select all

img1.Compare(img2, new ErrorMetric(), imgDiff);
imgDiff.Write(monImageResult);
It's nearly what I want, it borders whith red the differences, and what I need it's the red borders (or not) and just what is inside the red borders, not the entire image. (I want the image cut around the differences).

How can I do that ?

Thanks !!

Ln79
Posts: 4
Joined: 2018-01-09T02:47:12-07:00
Authentication code: 1152

Re: difference and mask

Post by Ln79 » 2018-01-09T07:02:50-07:00

In fact, this code

Code: Select all

img1.Compare(img2, new ErrorMetric(), imgDiff);
imgDiff.Write(monImageResult);
give me some geometrical shapes red bordered,

With these geometrical shapes I want to cut the img2, as a jigsaw :
http://www.imagemagick.org/Usage/advanced/#jigsaw

Ln79
Posts: 4
Joined: 2018-01-09T02:47:12-07:00
Authentication code: 1152

Re: difference and mask

Post by Ln79 » 2018-01-10T02:34:47-07:00

How to cut 20px around a shape ? With Crop ?

Code: Select all

using (MagickImage cloneImg1 = (MagickImage)img1)
{
     using (MagickImage cloneImg2 = (MagickImage)img2)
     {
           cloneImg1.Composite(cloneImg2, CompositeOperator.Difference);
           cloneImg1.Threshold(new Percentage(0));
           // into cloneImg1 I have the shapes I want to cut

           cloneImg2.Crop(new MagickGeometry()); // <= I don't know how to use Crop to cut 20px around the shapes

           // save the result
           cloneImg2.Write(monImageResult);
     }
}
Thanks !

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

Re: difference and mask

Post by dlemstra » 2018-01-14T03:29:33-07:00

You will need to use the size of the input image:

Code: Select all

var size = 20;
var geometry = new MagickGeometry(size, size, cloneImg2.Width - size,cloneImg2.Height - size);
cloneImg2.Crop(geometry );
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Ln79
Posts: 4
Joined: 2018-01-09T02:47:12-07:00
Authentication code: 1152

Re: difference and mask

Post by Ln79 » 2018-01-15T07:53:18-07:00

Thanks, that's not exactly what I wanted.

I managed to make what I wanted :

Code: Select all

cloneImg1.Composite(cloneImg2, CompositeOperator.Difference);
cloneImg1.Threshold(new Percentage(0));
// into cloneImg1 I have the shapes I want to cut

Point pointTopLeft = new Point();
Point pointBottomRight = new Point();

if (GetRectangle(cloneImg1, out pointTopLeft, out pointBottomRight)) // explore the image pixel by pixel to find the positions of the changes, return false if the changes are too numerous
{
	cloneImg2.Crop(new MagickGeometry(new Rectangle((int)pointTopLeft.X, (int)pointTopLeft.Y, (int)(Math.Abs(pointBottomRight.X - pointTopLeft.X)), (int)Math.Abs(pointBottomRight.Y - pointTopLeft.Y))));
        // save the result
	cloneImg2.Write(pathResult);
}

Post Reply