如何检查一个图像是否是另一个图像的缩放版本

Chr*_*ert 5 .net c# image

给定两张图像,我如何轻松检查一张图像是否是另一张图像的缩放版本?

性能不是问题;它只需要合理准确,并且在 .NET 平台上工作即可。

我考虑缩小较大的图像以匹配较小的图像,然后比较图像校验和或在重新缩放后迭代比较各个像素;但其中任何一个都只能捕获精确匹配,这似乎不太可能是通过将原始较小图像放大然后缩小而产生的。

也许有一种方法可以结合使用色调分布、亮度等“统计数据”?

小智 5

我认为这将是您最好的解决方案。首先检查纵横比。如果图像大小不同,则将图像缩放为两个图像中较小的一个。最后,对两张图像进行哈希比较。这比进行像素比较要快得多。我在其他人的帖子中找到了哈希比较方法,并只是调整了此处的答案以适应。我试图为一个需要比较 5200 多张图像的项目想出最好的方法。在我阅读了这里的一些帖子后,我意识到我已经拥有了所需的一切,并认为我应该分享。

public class CompareImages2
    {
        public enum CompareResult
        {
            ciCompareOk,
            ciPixelMismatch,
            ciAspectMismatch
        };

        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.Height / bmp1.Size.Width == bmp2.Size.Height / bmp2.Size.Width)
            {
                if (bmp1.Size != bmp2.Size)
                {
                    if (bmp1.Size.Height > bmp2.Size.Height)
                    {
                        bmp1 = (new Bitmap(bmp1, bmp2.Size));
                    }
                    else if (bmp1.Size.Height < bmp2.Size.Height)
                    {
                        bmp2 = (new Bitmap(bmp2, bmp1.Size));
                    }
                }

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

                }
            }
            else cr = CompareResult.ciAspectMismatch;
            return cr;
        }
    }
Run Code Online (Sandbox Code Playgroud)


tda*_*ers 0

您必须在某个时刻循环像素。易于实现但功能强大的方法是计算每个像素的各个颜色分量 (RGB) 之间的差异,找到平均值,并查看它是否超过某个阈值。这当然不是最好的方法,但为了快速检查它应该可以。