1

我需要计算图像的感知散列,并且应该在不使用任何外部库的情况下进行。

我尝试使用 pHash (http://phash.org/) 但我无法为 iOS (5) 编译它,而且我还没有找到关于如何做到这一点的真正教程。

4

1 回答 1

0

一种(依赖于库的)解决方案是使用在 6.8.8.3 版本中添加到 ImageMagick 的 pHashing 功能,该版本具有可用的iOS 二进制文件此处记录了使用示例。

这里还有一个简单的参考函数(在 C# 中),用于生成您自己的可比较图像平均散列,可在此博客上找到。

public static ulong AverageHash(System.Drawing.Image theImage)
// Calculate a hash of an image based on visual characteristics.
// Described at http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
{
    // Squeeze the image down to an 8x8 image.
    // Chant the ancient incantations to create the correct data structures.
    Bitmap squeezedImage = new Bitmap(8, 8, PixelFormat.Format32bppRgb);
    Graphics drawingArea = Graphics.FromImage(squeezedImage);
        drawingArea.CompositingQuality = CompositingQuality.HighQuality;
        drawingArea.InterpolationMode = InterpolationMode.HighQualityBilinear;
        drawingArea.SmoothingMode = SmoothingMode.HighQuality;
        drawingArea.DrawImage(theImage, 0, 0, 8, 8);

    byte[] grayScaleImage = new byte[64];

    uint averageValue = 0;
    ulong finalHash = 0;

    // Reduce to 8-bit grayscale and calculate the average pixel value.
    for(int y = 0; y < 8; y++)
    {
        for(int x = 0; x < 8; x++)
        {
            Color pixelColour = squeezedImage.GetPixel(x,y);
            uint grayTone = ((uint)((pixelColour.R * 0.3) + (pixelColour.G * 0.59) + (pixelColour.B * 0.11)));

            grayScaleImage[x + y*8] = (byte)grayTone;
            averageValue += grayTone;
        }
    }
    averageValue /= 64;

    // Return 1-bits when the tone is equal to or above the average,
    // and 0-bits when it's below the average.
    for(int k = 0; k < 64; k++)
    {
        if(grayScaleImage[k] >= averageValue)
        {
            finalHash |= (1UL << (63-k));
        }
    }

    return finalHash;
}
于 2014-08-01T22:47:57.250 回答