我如何比较,如果2图像都是一样的使用Hash字节? [英] How do I compare if 2 images are the same using Hash bytes?

查看:152
本文介绍了我如何比较,如果2图像都是一样的使用Hash字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人无效的button1_Click(对象发件人,EventArgs五)
{
位图IM1 =新位图(@C:\Users\user\Downloads \CaptchaCollection\1.png);
位图IM2 =新位图(@C:\Users\user\Downloads\CaptchaCollection\2.png);

如果(HashImage(IM1)== HashImage(IM2))
{
MessageBox.Show(相同的图像);
}

,否则
{
MessageBox.Show(不同图像);
}
}

如果单击该按钮,它会比较这两个图像



下面是用于散列图像的代码。

 字节公开[] HashImage(位图图像​​)
{
VAR SHA256 = SHA256.Create();

变种RECT =新的Rectangle(0,0,image.Width,image.Height);
VAR数据= image.LockBits(RECT,ImageLockMode.ReadOnly,image.PixelFormat);

VAR dataPtr = data.Scan0;

VAR totalBytes =(int)的Math.Abs​​(data.Stride)* data.Height;
变种RAWDATA =新的字节[totalBytes]
System.Runtime.InteropServices.Marshal.Copy(dataPtr,RAWDATA,0,totalBytes);

image.UnlockBits(数据);

返回sha256.ComputeHash(RAWDATA);
}



那么,如何使用 HashImage()方法,如果它们是相同的视觉或比不上这两个图像?



我试过比较2的图像,显​​然是相同的,但他们AREN 'T工作正确进行比较。相反,我得到,就好像它是一个不同的形象。



我甚至试过,但它不工作要么。

 如果(HashImage(IM1).Equals(HashImage(IM2)))

更新:我试过,但它不工作要么

 如果(的ReferenceEquals(HashImage( IM1),HashImage(IM2)))


解决方案

我知道3种方式来比较字节数组:




  • 字节[] SequenceEqual(字节[])

  • System.Text.Encoding.UTF8.GetString(字节[])==

  • Convert.ToBase64String(字节[])==



有关你的代码就可以轻松尝试这个办法:

  Console.WriteLine(序列EQUAL :+(HashImage(IM1).SequenceEqual(HashImage(IM2))?真:FALSE)+(最简单的方法)); 
Console.WriteLine(UTF8字符串:?+(System.Text.Encoding.UTF8.GetString(HashImage(IM1))== System.Text.Encoding.UTF8.GetString(HashImage(IM2))TRUE :FALSE)+(转换为UTF字符串 - 不利于显示器或哈希,不仅有利于从UTF8范围的数据));
Console.WriteLine(HASH字符串:+(Convert.ToBase64String(HashImage(IM1))== Convert.ToBase64String(HashImage(IM2))TRUE:FALSE)+(最佳显示));

Console.WriteLine(1:+ Convert.ToBase64String(HashImage(IM1)));
Console.WriteLine(2:+ Convert.ToBase64String(HashImage(IM2)));



位图IM2初始化之后给你的代码添加,并期待在输出窗口的结果。您可以使用任何本作比较,如果数组是相同的评价



注意 System.Text。 Encoding.UTF8.GetString 是不适合用在这种情况下,利用(从图象哈希数据)。见从下面@CodesInChaos评论。


private void button1_Click(object sender, EventArgs e)
{
    Bitmap im1 = new Bitmap(@"C:\Users\user\Downloads\CaptchaCollection\1.png");
    Bitmap im2 = new Bitmap(@"C:\Users\user\Downloads\CaptchaCollection\2.png");

    if (HashImage(im1) == HashImage(im2))
    {
        MessageBox.Show("Same Image");
    }

    else
    {
        MessageBox.Show("Different Image");
    }
}

If the button is clicked it will compare these 2 images.

Here is the code that is used to hash an image.

public byte[] HashImage(Bitmap image)
{
    var sha256 = SHA256.Create();

    var rect = new Rectangle(0, 0, image.Width, image.Height);
    var data = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

    var dataPtr = data.Scan0;

    var totalBytes = (int)Math.Abs(data.Stride) * data.Height;
    var rawData = new byte[totalBytes];
    System.Runtime.InteropServices.Marshal.Copy(dataPtr, rawData, 0, totalBytes);

    image.UnlockBits(data);

    return sha256.ComputeHash(rawData);
}

So how do I use the HashImage() method to compare both those images if they're the same visually or not?

I tried comparing 2 images that are clearly the same but they aren't working to compare correctly. Instead I'm getting as if it's a different image.

I even tried this but it's not working either.

if (HashImage(im1).Equals(HashImage(im2)))

UPDATE: I've tried this but it isn't working either.

if (ReferenceEquals(HashImage(im1),HashImage(im2)))

解决方案

I know 3 ways to compare byte array:

  • byte[].SequenceEqual(byte[])
  • System.Text.Encoding.UTF8.GetString(byte[]) ==
  • Convert.ToBase64String(byte[]) ==

For your code you can easy try this:

   Console.WriteLine("SEQUENCE EQUAL: " + (HashImage(im1).SequenceEqual(HashImage(im2)) ? "TRUE" : "FALSE") + " (easiest way)");
   Console.WriteLine("UTF8 STRING:    " + (System.Text.Encoding.UTF8.GetString(HashImage(im1)) == System.Text.Encoding.UTF8.GetString(HashImage(im2)) ? "TRUE" : "FALSE") + " (conversion to utf string - not good for display or hash, good only for data from UTF8 range)");
   Console.WriteLine("HASH STRING:    " + (Convert.ToBase64String(HashImage(im1)) == Convert.ToBase64String(HashImage(im2)) ? "TRUE" : "FALSE") + " (best to display)");

   Console.WriteLine("1: " + Convert.ToBase64String(HashImage(im1)));
   Console.WriteLine("2: " + Convert.ToBase64String(HashImage(im2)));

Add this to your code right after initialization of Bitmap im2, and look at results in output window. You can use any of this for compare and evaluate if array is the same.

Note: System.Text.Encoding.UTF8.GetString is not suitable for use in this case (hash data from picture). See comment from @CodesInChaos below.

这篇关于我如何比较,如果2图像都是一样的使用Hash字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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