找出图像之间的差异 [英] Find differences between images

查看:141
本文介绍了找出图像之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose i had two nearly identical images and i wanted to locate and highlight the differences between them and produce the diff image. the routine works but this routine ask to supply color which i do not want. here is my code.

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()方法时,我们不必传递颜色。

  1. some one just guide me how to change this routine as a result we do not have to pass color when i will call GetDifferenceImage() method.

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

this way image comparison is best technique if not then guide me how to develop a routine which can be more faster to get the diff image.

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

after getting the diff image how can i merge the diff image with image1. help me to develop a faster merge routine.


推荐答案

差异图像是如果两个图像相同则为黑色,并且对于具有较大差异的像素具有增加的亮度。您可以更改算法,以便不是为交换颜色指定像素,而是指定两种颜色之间的差异。

The diff image is black if two images are identical and has an increasing brightness for pixels with larger differences. You can just change the algorithm so that instead of assigning the pixel the swapcolor it assigns it the difference between the two colors.

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

你如何展示/合并差异将取决于什么你打算用它。

How you show/merge the diff will depend on what you are going to do with it.

这篇关于找出图像之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆