bmp 文件比较?

发布于 2024-07-16 02:20:56 字数 150 浏览 5 评论 0原文

我想比较两个 bmp 文件。 我想到了两种方法:

  1. 比较两个文件的标头以及信息标头
  2. 将bmp文件转换为二进制文件,然后进行上述比较

但是,我不知道如何开始以及哪种方法更好。 如果有人能帮助我,我会很高兴!

I want to compare two bmp files. I thought of two approaches:

  1. to compare the header as well as the information header of the two files
  2. convert the bmp file to binary and then do the above comparison

But, I don't know how to start and which will be a better approach. I would be glad if someone could please help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剧终人散尽 2024-07-23 02:20:56

我不知道您想在哪个平台上实现此功能,但这里有一些可能有用的代码片段:

使用 C# 比较两个图像

这是比较 2 个图像的片段
看看它们是否相同。 这
方法首先将每个 Bitmap 转换为
字节数组,然后获取每个字节的哈希值
大批。 然后我们循环遍历每个
哈希值以查看它们是否匹配。

/// <summary>
/// method for comparing 2 images to see if they are the same. First
/// we convert both images to a byte array, we then get their hash (their
/// hash should match if the images are the same), we then loop through
/// each item in the hash comparing with the 2nd Bitmap
/// </summary>
/// <param name="bmp1"></param>
/// <param name="bmp2"></param>
/// <returns></returns>
public bool doImagesMatch(ref Bitmap bmp1, ref Bitmap bmp2)
{
  ...
}

I don't know on which platform you want to implement this, but here are some code snippets which could be useful:

Compare two images with C#

This is a snippet to compare 2 images
to see if they are the same. This
method first converts each Bitmap to a
byte array, then gets the hash of each
array. We then loop through each in
the hash to see if they match.

/// <summary>
/// method for comparing 2 images to see if they are the same. First
/// we convert both images to a byte array, we then get their hash (their
/// hash should match if the images are the same), we then loop through
/// each item in the hash comparing with the 2nd Bitmap
/// </summary>
/// <param name="bmp1"></param>
/// <param name="bmp2"></param>
/// <returns></returns>
public bool doImagesMatch(ref Bitmap bmp1, ref Bitmap bmp2)
{
  ...
}
始终不够爱げ你 2024-07-23 02:20:56

好吧,这里至少有两个选择:

  • 图像基本相同
    在这种情况下,我建议使用 splattne 的解决方案进行比较

  • 图像通常不同,只有有时相同
    在这种情况下,您可能可以通过快速比较信息标题来消除两个图像之间的任何相似性(认为“大小是否相同?”),并且仅在信息不明确时才进行完整比较(即尺寸相同)

Well, you have at least two options here:

  • The images are mostly the same
    In this case, I'd recommend a compare using splattne's solution

  • The images are usually different, and only sometimes the same
    In this case, it might be that you can dismiss any similarity between the two images with a quick comparison of the information header (think "is the size the same?), and only do the full comparison if the information is ambiguous (i.e. the size is the same)

烈酒灼喉 2024-07-23 02:20:56

您可能需要使用 .NET 3.0+ 中的扩展方法来公开所有位图上的比较方法:

    public static bool Compare(this Bitmap bmp1, Bitmap bmp2)
    {
        //put your comparison logic here
    }

You may want to use an extension method in .NET 3.0+ for this to expose the compare method on all the Bitmaps:

    public static bool Compare(this Bitmap bmp1, Bitmap bmp2)
    {
        //put your comparison logic here
    }
子栖 2024-07-23 02:20:56

你在比较什么? 您想看看两张图片是否完全相同? 或者你需要差异程度吗? 对于精确匹配,如何创建和比较两个文件的哈希值?

What are you comparing for? Are you looking to see if 2 images are exactly the same? Or do you need degrees of difference? For exact matches how about creating and comparing hashes of both files?

桃酥萝莉 2024-07-23 02:20:56

当我有两张仅颜色深度不同的图像(一张是 32bpp,另一张是 8bpp)时,上述解决方案对我不起作用。 我找到的解决方案使用 LockBits 将所有图像转换为 32bpp,使用 Marshal.Copy() 将数据放入数组中,然后比较数组。

/// <summary>
/// Compares two images for pixel equality
/// </summary>
/// <param name="fname1">first image file</param>
/// <param name="fname2">second image file</param>
/// <returns>true if images are identical</returns>
public static string PageCompare(string fname1, string fname2) {
    try {
        using (Bitmap bmp1 = new Bitmap(fname1))
        using (Bitmap bmp2 = new Bitmap(fname2)) {
            if (bmp1.Height != bmp2.Height || bmp1.Width != bmp2.Width)
                return false;

            // Convert image to int32 array with each int being one pixel
            int cnt = bmp1.Width * bmp1.Height * 4 / 4;
            BitmapData bmData1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            BitmapData bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            Int32[] rgbValues1 = new Int32[cnt];
            Int32[] rgbValues2 = new Int32[cnt];

            // Copy the ARGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(bmData1.Scan0, rgbValues1, 0, cnt);
            System.Runtime.InteropServices.Marshal.Copy(bmData2.Scan0, rgbValues2, 0, cnt);

            bmp1.UnlockBits(bmData1);
            bmp2.UnlockBits(bmData2);
            for (int i = 0; i < cnt; ++i) {
                if (rgbValues1[i] != rgbValues2[i])
                    return false;
            }
        }
    }
    catch (Exception ex) {
        return false;
    }
    // We made it this far so the images must match
    return true;
}

The above solutions didn't work for me when I had two images that only differed in color depth--one was 32bpp and the other was 8bpp. The solution I arrived at used LockBits to convert all images to 32bpp, Marshal.Copy() to get the data into an array and then just compare the arrays.

/// <summary>
/// Compares two images for pixel equality
/// </summary>
/// <param name="fname1">first image file</param>
/// <param name="fname2">second image file</param>
/// <returns>true if images are identical</returns>
public static string PageCompare(string fname1, string fname2) {
    try {
        using (Bitmap bmp1 = new Bitmap(fname1))
        using (Bitmap bmp2 = new Bitmap(fname2)) {
            if (bmp1.Height != bmp2.Height || bmp1.Width != bmp2.Width)
                return false;

            // Convert image to int32 array with each int being one pixel
            int cnt = bmp1.Width * bmp1.Height * 4 / 4;
            BitmapData bmData1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            BitmapData bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            Int32[] rgbValues1 = new Int32[cnt];
            Int32[] rgbValues2 = new Int32[cnt];

            // Copy the ARGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(bmData1.Scan0, rgbValues1, 0, cnt);
            System.Runtime.InteropServices.Marshal.Copy(bmData2.Scan0, rgbValues2, 0, cnt);

            bmp1.UnlockBits(bmData1);
            bmp2.UnlockBits(bmData2);
            for (int i = 0; i < cnt; ++i) {
                if (rgbValues1[i] != rgbValues2[i])
                    return false;
            }
        }
    }
    catch (Exception ex) {
        return false;
    }
    // We made it this far so the images must match
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文