找出图像之间的差异

时间:2013-04-26 09:15:44

标签: c# image-processing

假设我有两个几乎相同的图像,我想找到并突出它们之间的差异并产生差异图像。例行工作,但这个例程要求提供我不想要的颜色。这是我的代码。

public class ImageTool
{
    public static unsafe Bitmap GetDifferenceImage(Bitmap image1, Bitmap image2, Color matchColor)
    {
        if (image1 == null | image2 == null)
            return null;

        if (image1.Height != image2.Height || image1.Width != image2.Width)
            return null;

        Bitmap diffImage = image2.Clone() as Bitmap;

        int height = image1.Height;
        int width = image1.Width;

        BitmapData data1 = image1.LockBits(new Rectangle(0, 0, width, height), 
                                           ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        BitmapData data2 = image2.LockBits(new Rectangle(0, 0, width, height), 
                                           ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        BitmapData diffData = diffImage.LockBits(new Rectangle(0, 0, width, height), 
                                               ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

        byte* data1Ptr = (byte*)data1.Scan0;
        byte* data2Ptr = (byte*)data2.Scan0;
        byte* diffPtr = (byte*)diffData.Scan0;

        byte[] swapColor = new byte[3];
        swapColor[0] = matchColor.B;
        swapColor[1] = matchColor.G;
        swapColor[2] = matchColor.R;

        int rowPadding = data1.Stride - (image1.Width * 3);

        // iterate over height (rows)
        for (int i = 0; i < height; i++)
        {
            // iterate over width (columns)
            for (int j = 0; j < width; j++)
            {
                int same = 0;

                byte[] tmp = new byte[3];

                // compare pixels and copy new values into temporary array
                for (int x = 0; x < 3; x++)
                {
                    tmp[x] = data2Ptr[0];
                    if (data1Ptr[0] == data2Ptr[0])
                    {
                        same++;
                    }
                    data1Ptr++; // advance image1 ptr
                    data2Ptr++; // advance image2 ptr
                }

                // swap color or add new values
                for (int x = 0; x < 3; x++)
                {
                    diffPtr[0] = (same == 3) ? swapColor[x] : tmp[x];
                    diffPtr++; // advance diff image ptr
                }
            }

            // at the end of each column, skip extra padding
            if (rowPadding > 0)
            {
                data1Ptr += rowPadding;
                data2Ptr += rowPadding;
                diffPtr += rowPadding;
            }
        }

        image1.UnlockBits(data1);
        image2.UnlockBits(data2);
        diffImage.UnlockBits(diffData);

        return diffImage;
    }
}

这样打电话:

Bitmap diff = ImageTool.GetDifferenceImage(image1, image2, Color.Pink);
diff.MakeTransparent(Color.Pink);
diff.Save("C:\\test-diff.png",ImageFormat.Png);
  1. 某人只是指导我如何更改此例程,因此当我调用GetDifferenceImage()方法时我们不必传递颜色。

  2. 这种方式图像比较是最好的技术,如果没有,那么指导我如何开发一个例程,可以更快地获得差异图像。

  3. 获取差异图像后,
  4. 如何将差异图像与image1合并。帮助我开发一个更快的合并例程。

1 个答案:

答案 0 :(得分:5)

如果两个图像相同,则差异图像为黑色,对于差异较大的像素,亮度图像的亮度增加。你可以改变算法,这样就不会给像素分配交换颜色,而是分配两种颜色之间的区别。

    // iterate over height (rows)
    for (int i = 0; i < height; i++)
    {
        // iterate over width (columns)
        for (int j = 0; j < width; j++)
        {
            // for each channel
            for (int x=0; x<3; x++)
            {
                diffPtr[0] = Abs(data1Ptr[0]-data2Ptr[0]);
                data1Ptr++; // advance image1 ptr
                data2Ptr++; // advance image2 ptr
                diffPtr++; // advance diff image ptr
            }
        }

        // at the end of each column, skip extra padding
        if (rowPadding > 0)
        {
            data1Ptr += rowPadding;
            data2Ptr += rowPadding;
            diffPtr += rowPadding;
        }
    }

你如何展示/合并差异将取决于你将如何处理它。