0

同じ高さと幅の 2 つの画像があるとします。pic1.jpg & pic2.jpg。2 つの画像はほとんど同じに見えますが、違いはほとんどありません。以下のルーチンの助けを借りて、2 つの画像の違いを得ることができます。この下のルーチンは私のルーチンではありません。

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);
        }
    }
}
}

違いを取得した後、最初の画像の違いをどのようにマージできますか。

この下の方法でマージできます

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

ただし、新しい画像が最初の画像に描画される場所から x & y を知る必要があります。上記のPi​​xelDiff()というルーチンから x と y の位置を取得する方法を教えてください。事前に感謝します。

4

1 に答える 1

0

このルーチンは、両方の入力画像に座標 0,0 を使用し、差分画像にも同じ座標を使用するため、同じものを使用して差分画像を描画します。

于 2012-12-11T19:11:08.670 に答える