如何比较两个图像?

时间:2014-02-08 15:26:25

标签: c# .net

public void winCheck()
{
    if (button1.Image == img1)
    {
        w1 = "P2";
        button1.Image = new Bitmap(@"win_cross.png");
        button2.Image = new Bitmap(@"win_cross.png");
        button3.Image = new Bitmap(@"win_cross.png");
    }
}

此处button1.Imageimg1都有相同的图像但仍然没有进入if块。 他们的任何属性都是这样,两者的价值都是相同的。

4 个答案:

答案 0 :(得分:7)

如果您可以拥有unsafe代码,并且您希望图像在字节级别上完全相同,则可以执行此操作:

public unsafe static bool AreEqual(Bitmap b1, Bitmap b2)
{
    if (b1.Size != b2.Size)
    {
        return false;
    }

    if (b1.PixelFormat != b2.PixelFormat)
    {
        return false;
    }

    if (b1.PixelFormat != PixelFormat.Format32bppArgb)
    {
        return false;
    }

    Rectangle rect = new Rectangle(0, 0, b1.Width, b1.Height);
    BitmapData data1
        = b1.LockBits(rect, ImageLockMode.ReadOnly, b1.PixelFormat);
    BitmapData data2
        = b2.LockBits(rect, ImageLockMode.ReadOnly, b1.PixelFormat);

    int* p1 = (int*)data1.Scan0;
    int* p2 = (int*)data2.Scan0;
    int byteCount = b1.Height * data1.Stride / 4; //only Format32bppArgb 

    bool result = true;
    for (int i = 0; i < byteCount; ++i)
    {
        if (*p1++ != *p2++)
        {
            result = false;
            break;
        }
    }

    b1.UnlockBits(data1);
    b2.UnlockBits(data2);

    return result;
}

这比较“按字面意思”的图像 - 所有字节都需要完全相同。其他可能性是比较像素的颜色 - 然后PixelFormat s不必相同(取自here):

public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
{
    CompareResult cr = CompareResult.ciCompareOk;

    //Test to see if we have the same size of image
    if (bmp1.Size != bmp2.Size)
    {
        cr = CompareResult.ciSizeMismatch;
    }
    else
    {
        //Sizes are the same so start comparing pixels
        for (int x = 0; x < bmp1.Width 
             && cr == CompareResult.ciCompareOk; x++)
        {
            for (int y = 0; y < bmp1.Height 
                         && cr == CompareResult.ciCompareOk; y++)
            {
                if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                    cr = CompareResult.ciPixelMismatch;
            }
        }
    }
    return cr;
}

但这可能非常慢。相同的链接包含比较哈希值的有趣想法:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography;

namespace Imagio
{
    public class ComparingImages
    {
        public enum CompareResult
        {
            ciCompareOk,
            ciPixelMismatch,
            ciSizeMismatch
        };

        public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
        {
            CompareResult cr = CompareResult.ciCompareOk;

            //Test to see if we have the same size of image
            if (bmp1.Size != bmp2.Size)
            {
                cr = CompareResult.ciSizeMismatch;
            }
            else
            {
                //Convert each image to a byte array
                System.Drawing.ImageConverter ic = 
                       new System.Drawing.ImageConverter();
                byte[] btImage1 = new byte[1];
                btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());
                byte[] btImage2 = new byte[1];
                btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());

                //Compute a hash for each image
                SHA256Managed shaM = new SHA256Managed();
                byte[] hash1 = shaM.ComputeHash(btImage1);
                byte[] hash2 = shaM.ComputeHash(btImage2);

                //Compare the hash values
                for (int i = 0; i < hash1.Length && i < hash2.Length 
                                  && cr == CompareResult.ciCompareOk; i++)
                {
                    if (hash1[i] != hash2[i])
                        cr = CompareResult.ciPixelMismatch;
                }
            }
            return cr;
        }
    }
}

答案 1 :(得分:0)

您的代码不起作用,因为您正在比较指向图像的变量(即在内存地址中)而不是图像数据(像素)。

请参阅ImageComparer.Compare方法。有重载来指定容差。 它自VS2012开始提供。

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.imagecomparer.compare.aspx

答案 2 :(得分:0)

一种合适的方法是进行像素比较,看看它们是否相同。您可以在此用例中使用GroupDocs.Comparison for .NET
如何比较?
如果像素相同,它将继续移动到下一个像素,依此类推,直到找到另一个像素为止。 您可以将比较结果保存在一个对象中,如果它为null,则表示两个图像相同。

//set detail level 
DetailLevel zonesDetailLevel = DetailLevel.Hight; //heigh, low, middle 
ComparisonSettings settings = new ComparisonSettings(); 
settings.DetailLevel = zonesDetailLevel; 
settings.UseFramesForDelInsElements = true; 
settings.DeletedItemsStyle.FontColor = Color.Green; 
settings.StyleChangedItemsStyle.FontColor = Color.Red; 
ICompareResult compareResult = null; 
Comparer comparer = new Comparer(); compareResult = 
comparer.Compare(source path\file, target file\path, settings); 
string resultPath = @"D:\Output Path"; 
if (!Directory.Exists(resultPath)) Directory.CreateDirectory(resultPath);
compareResult.SaveImages(resultPath);

披露:我是GroupDocs的一名开发人员。

答案 3 :(得分:-5)

不要使用双等号(==)。 使用equals方法。 (如果button1.Image.equals(img1)) //你编码