代码之家  ›  专栏  ›  技术社区  ›  JY2k

感知散列距离的计算

  •  1
  • JY2k  · 技术社区  · 8 年前

    我使用Imagemagick来获取图像的感知哈希。我使用以下命令:

    identify -verbose -define identify:moments x.png
    

    除其他参数外,输出还返回pereceptual哈希:

    I1: 0.0017694 (0.451197) I2: 3.22345e-07 (0.0209605) I3: 2.88038e-10 (0.00477606) I4: 3.93968e-12 (6.53253e-05) I5: 1.2326e-22 (3.38892e-08) I6: -1.94034e-15 (-8.20426e-06) I7: -4.91938e-23 (-1.35254e-08) I8: 5.56374e-16 (2.35249e-06) Channel perceptual hash: Red, Hue: PH1: 0.407586, 0.690687 PH2: 1.88394, 2.91999 PH3: 2.36028, 3.96979 PH4: 5.36184, 5.3591 PH5: 9.25849, 11 PH6: 6.30422, 6.93025 PH7: 9.6332, 10.0241 Green, Chroma: PH1: 0.293148, -0.0406998 PH2: 1.49146, 2.52843 PH3: 2.21568, 0.992456 PH4: 3.52683, 2.3777 PH5: 6.48291, 4.06334 PH6: 4.38149, 4.23342 PH7: 6.64322, 5.35487 Blue, Luma: PH1: 0.329865, 0.33357 PH2: 1.6461, 1.63528 PH3: 2.39206, 2.26483 PH4: 3.72747, 4.09284 PH5: 6.789, 7.36151 PH6: 4.56493, 5.0171 PH7: 7.83416, 7.50669 
    

    我想保存哈希,然后计算两幅图像之间的距离。如何将上述输出转换为哈希并计算两个哈希之间的距离?

    1 回复  |  直到 8 年前
        1
  •  3
  •   fmw42    8 年前

    看见 http://www.fmwconcepts.com/misc_tests/perceptual_hash_test_results_510/index.html 有关此感知哈希的详细信息和测试。

    基本上,它创建了42个浮点值,需要使用平方和度量与另一个图像中的另一组42个浮点值进行比较。

    这不是一个简单的二进制哈希,可以轻松地存储为1和0x的字符串,并使用汉明距离进行比较。

    但您可以使用ImageMagick中的感知哈希比较两幅图像

    compare -metric phash image1 image2 null:
    

    您可以将phash值输出到。json文件。

    另外,我还有两个bash unix ImageMagick shell脚本(phashconvert和phashcompare)。将42个浮点数转换为可以保存在注释部分文件中的数字字符串。第二个将读取两个文件的注释部分以提取字符串,将其转换回浮点,然后使用平方和度量对其求值。但请注意,由于从浮点到数字的来回转换,这个过程只是一个近似值。

    如果您只想提取42个浮点,那么应该这样做(从我的脚本phashconvert)

    identify -quiet -verbose -moments -alpha off "x.png" | grep "PH[1-7]" | sed -n 's/.*: \(.*\)$/\1/p' | sed 's/ *//g' | tr "," "\n"