0

suppose i have two image with same height and width. pic1.jpg & pic2.jpg. two image look pretty same with minimum difference. with the help of below routine we can get the difference between two images.this below routine is not my routine.

public class ImageDifferences
{
    private static ILog mLog = LogManager.GetLogger("ImageDifferences");

    public static unsafe Bitmap PixelDiff(Image a, Image b)
    {
        if (!a.Size.Equals(b.Size)) return null;
        if (!(a is Bitmap) || !(b is Bitmap)) return null;

        return PixelDiff(a as Bitmap, b as Bitmap);
    }

    public static unsafe Bitmap PixelDiff(Bitmap a, Bitmap b)
    {
        Bitmap output = new Bitmap(
            Math.Max(a.Width, b.Width),
            Math.Max(a.Height, b.Height),
            PixelFormat.Format32bppArgb);

        Rectangle recta = new Rectangle(Point.Empty, a.Size);
        Rectangle rectb = new Rectangle(Point.Empty, b.Size);
        Rectangle rectOutput = new Rectangle(Point.Empty, output.Size);

        BitmapData aData = a.LockBits(recta, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
        BitmapData bData = b.LockBits(rectb, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
        BitmapData outputData = output.LockBits(rectOutput, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        try
        {
            byte* aPtr = (byte*)aData.Scan0;
            byte* bPtr = (byte*)bData.Scan0;
            byte* outputPtr = (byte*)outputData.Scan0;
            int len = aData.Stride * aData.Height;

            for (int i = 0; i < len; i++)
            {
                // For alpha use the average of both images (otherwise pixels with the same alpha won't be visible)
                if ((i + 1) % 4 == 0)
                    *outputPtr = (byte)((*aPtr + *bPtr) / 2);
                else
                    *outputPtr = (byte)~(*aPtr ^ *bPtr);

                outputPtr++;
                aPtr++;
                bPtr++;
            }

            return output;
        }
        catch (Exception ex)
        {

            return null;
        }
        finally
        {
            a.UnlockBits(aData);
            b.UnlockBits(bData);
            output.UnlockBits(outputData);
        }
    }
}
}

after getting the difference how could i merge the difference on first image.

this below way we can merge

using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

but we need to know the x & y from where the new image will be drawn on first image. can any one tell me how can i get the x & y position from the above routine called PixelDiff() thanks in advance.

Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

0

The routine uses the coordinates 0,0 for both input images, and the same for the difference image, so you would use the same to draw the difference image.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • i do not know whether you understand my question or not. basically i want to draw difference image on the first image.DrawImage() need x & y coordinate. if i can provide right x & y coordinate then i can draw the difference on first image at right location which causes to generate a image which would look same as 2nd image. i hope i am clear. – Thomas Dec 11 '12 at 19:33
  • suppose you have tow images or screen shot of your desktop with minimum difference. u can programmatically extract the difference between two images and later u can merge the difference on first image which generate a another image which would look like second screen shot of your desktop. i just need to know how could i get the right x & y when i extract the difference between two images. if i know right x & y then i can dump the diff image on the first image with right x & y that generate 3rd image which would look like same as 2nd image. – Thomas Dec 11 '12 at 19:36
  • @Thomas: Yes. The routine is reading from both images starting at coordinates 0,0 (`Point.Empty`), and writing the result into the diff image at 0,0. Therefore if you draw the 0,0 coordinate of the diff image at 0,0 on the first image, they will match. – Guffa Dec 11 '12 at 20:42