Page 1 of 1

difference and mask

Posted: 2018-01-09T03:33:48-07:00
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 !!

Re: difference and mask

Posted: 2018-01-09T07:02:50-07:00
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

Re: difference and mask

Posted: 2018-01-10T02:34:47-07:00
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 !

Re: difference and mask

Posted: 2018-01-14T03:29:33-07:00
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 );

Re: difference and mask

Posted: 2018-01-15T07:53:18-07:00
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);
}