【问题标题】:Apply diff image on the original image c#在原始图像上应用差异图像c#
【发布时间】:2012-12-11 19:07:59
【问题描述】:

假设我有两个具有相同高度和宽度的图像。 pic1.jpg 和 pic2.jpg。两个图像看起来非常相似,差异最小。在下面的例程的帮助下,我们可以得到两个图像之间的区别。下面的例程不是我的例程。

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。谁能告诉我如何从上述名为 PixelDiff() 的例程中获取 x 和 y 位置,谢谢。

【问题讨论】:

    标签: c# image-comparison


    【解决方案1】:

    例程对两个输入图像使用坐标 0,0,对差异图像使用相同的坐标,因此您将使用相同的坐标来绘制差异图像。

    【讨论】:

    • 我不知道你是否理解我的问题。基本上我想在第一个图像上绘制差异图像。DrawImage() 需要 x 和 y 坐标。如果我可以提供正确的 x 和 y 坐标,那么我可以在正确的位置绘制第一张图像的差异,从而生成一个看起来与第二张图像相同的图像。我希望我很清楚。
    • 假设您有两张图片或桌面的屏幕截图,差异最小。您可以以编程方式提取两个图像之间的差异,然后您可以合并第一个图像上的差异,从而生成另一个图像,该图像看起来像您桌面的第二个屏幕截图。我只需要知道当我提取两个图像之间的差异时如何获得正确的 x 和 y。如果我知道正确的 x 和 y,那么我可以将差异图像转储到具有正确 x 和 y 的第一张图像上,生成第三张图像,看起来与第二张图像相同。
    • @Thomas:是的。该例程从坐标 0,0 (Point.Empty) 开始读取两个图像,并将结果写入 0,0 处的差异图像。因此,如果您在第一张图像的 0,0 处绘制 diff 图像的 0,0 坐标,它们将匹配。
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2022-12-15
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2012-11-28
    相关资源
    最近更新 更多