感知(或平均)图像散列

时间:2012-06-19 13:55:46

标签: iphone objective-c ios xcode

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

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

1 个答案:

答案 0 :(得分:0)

一个(依赖于库)解决方案是使用版本6.8.8.3中添加到ImageMagick的pHashing功能,该功能具有iOS binaries available。用法示例记录在案here

这里也是一个简单的参考函数(在C#中),用于生成您自己的可比图像平均哈希值,可在this blog找到。

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