This project has moved. For the latest updates, please go here.

Compare with Mask

Jan 3, 2016 at 1:12 PM
Hi, I am using v7.0.0.0022 in C# (.Net 4.6.1) to compare multiple parts of two images and I am looking for more efficiency. I think an improvement would be to use Mask with Compare, but it does not return a difference image.

This is my current approach:
MagickGeometry mg;
Rectangle[] rcts = { new System.Drawing.Rectangle(200, 25, 200, 25), new System.Drawing.Rectangle(200, 25, 640, 480) }; //For example

using (MagickImage miAF = new MagickImage("A.jpg"))
{
    using (MagickImage miBF = new MagickImage("B.jpg"))
    {
        foreach (Rectangle rct in rcts)
        {
            mg = new MagickGeometry(rct);
            using (MagickImage miA = miAF.Clone())
            {
                using (MagickImage miB = miBF.Clone())
                {
                    miA.Crop(mg);
                    miA.ColorFuzz = new Percentage(30);
                    miB.Crop(mg);

                    using (MagickImage miC = new MagickImage())
                        miA.Compare(miB, ErrorMetric.Absolute, miC);
                }
            }
        }
    }
}
It returns the usual difference image in miC -- a "shaded/dimmed" image with red pixels indicating the differences.

This is what I think would be more efficient:
using (MagickImage miA = new MagickImage("A.jpg"))
{
    using (MagickImage miB = new MagickImage("B.jpg"))
    {
        foreach(Rectangle rct in rcts)
        {
            using (MagickImage miM = new MagickImage(MagickColor.Transparent, miA.Width, miA.Height))
            {
                miM.Draw(new DrawableRectangle(rct));

                miA.ColorFuzz = new Percentage(30);
                miA.Mask = miM;

                using (MagickImage miC = new MagickImage())
                    miA.Compare(miB, ErrorMetric.Absolute, miC);
            }
        }
    }
}
Compare returns a valid double result but it puts a full size copy of miA into miC, instead of returning a difference image. Am I doing something wrong?

Thank you!
Coordinator
Jan 3, 2016 at 2:21 PM
Your mask should always be black and white and it looks like your miM image is transparent and white (the default background color)
Jan 3, 2016 at 2:40 PM
Thank you for replying so quickly! I just tried four variants but alas, no change:
using (MagickImage miM = new MagickImage(Color.Black, miA.Width, miA.Height))
{
    miM.Draw(new DrawableFillColor(Color.White), new DrawableRectangle(rct));
    ...
The other three variants are reversing white and black, and both combinations of white and black with the addition of miB.Mask = miM;.
Coordinator
Jan 4, 2016 at 8:42 PM
It looks like it is not working correctly. I am in the middle of a huge rewrite of Magick.NET and I will take a look at this when that has been finished.
Jan 4, 2016 at 8:46 PM
Thanks again, no rush. I'd still be running each image through a bunch of command line shells if it wasn't for your awesome library!