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 »

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 »

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 »

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: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: difference and mask

Post by dlemstra »

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 »

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