# 图像哈希

图像哈希将图像转换为紧凑的二进制码，使感知上相似的图像得到相似的码，并可通过快速距离比较进行匹配。形式上，[感知哈希](https://www.edgepedia.org/zh/perceptual-hashing)函数 \( H \colon \mathbb{R}^{H \times W \times C} \to \{0,1\}^{k} \) 将图像映射为 \( k \) 位二进制哈希。<sup>[1](https://arxiv.org/pdf/2503.11195)</sup> 它用于近重复检测、图像检索、版权过滤和篡改检测，这些用途不适合使用 MD5 和 SHA-1 等密码学哈希：密码学哈希在任意单个输入位变化时会完全改变，而图像哈希的设计目标是相似输入产生相似输出。<sup>[2](https://github.com/johannesbuchner/imagehash)</sup>

| 关键事实 | 详情 |
|---|---|
| 输出 | 二进制码，经典哈希（aHash、dHash、pHash、wHash）通常为 64 位，已部署系统为 96 至 256 位<sup>[3](https://www.mdpi.com/2079-9292/15/7/1493)</sup><sup> • </sup><sup>[4](https://www.usenix.org/system/files/usenixsecurity25-zhang-yushu.pdf)</sup> |
| 匹配度量 | 归一化汉明距离：感知相同的图像接近 0，不同图像接近 0.5<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup> |
| 与密码学的对比 | 密码学哈希依赖雪崩效应；感知哈希在特征相似时“接近”<sup>[6](http://phash.org/)</sup> |
| 主要流程 | 预处理、特征提取、量化以及后处理/压缩<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup> |
| 已部署算法 | pHash（64 位 DCT）、PDQ（256 位 DCT）、PhotoDNA（144 位梯度网格）、NeuralHash（96 位 CNN）<sup>[4](https://www.usenix.org/system/files/usenixsecurity25-zhang-yushu.pdf)</sup> |
| 深度哈希 | 深度神经网络直接学习二进制码；深度哈希（DH）与监督深度哈希（SDH）由 Venice Erin Liong 及其同事于 2015 年提出<sup>[7](https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Liong_Deep_Hashing_for_2015_CVPR_paper.pdf)</sup> |
| 主要弱点 | 对旋转、裁剪和重度编辑的鲁棒性差；易受对抗性规避攻击<sup>[8](https://arxiv.org/html/2406.00918v1)</sup> |

## 工作原理

目标是内容保持：码必须概括视觉结构，使得微小的、保持内容的编辑只轻微改变哈希。感知图像哈希旨在对旋转、裁剪、伽马校正、加噪或加边框等微小变化平滑不变，这与密码学哈希函数形成对比——后者设计为任意单个位变化就完全改变。<sup>[9](https://www.mdpi.com/1999-4893/13/9/227)</sup>

比较通过码之间的距离进行。归一化[汉明距离](https://www.edgepedia.org/zh/hamming-distance)通过逐位比较两个哈希向量计算，并归一化到 [0, 1] 范围；若距离接近 0，则两幅图像被视为感知相同；对于感知不同的图像，距离预期接近 0.5。<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup> 在学习哈希中，复合函数 \( \mathbf{y} = \mathbf{h}(\mathbf{x}) \) 将输入项映射为紧凑码，使编码空间中的最近邻搜索近似于原始空间中的真实最近邻搜索。<sup>[10](https://ar5iv.labs.arxiv.org/html/1606.00185)</sup> 一种流行的线性哈希函数是 \( y = h(\mathbf{x}) = \operatorname{sgn}(\mathbf{w}^{\top}\mathbf{x} + b) \)，其中若 \( z \geqslant 0 \) 则 \( \operatorname{sgn}(z) = 1 \)，否则为 0（或 \( -1 \)），\( \mathbf{w} \) 是投影向量，\( b \) 是偏置。<sup>[10](https://ar5iv.labs.arxiv.org/html/1606.00185)</sup> 同样的符号阈值化出现在现代深度特征方案中，其中每个位 \( h_{i}(x) \) 通过 Heaviside 阶跃函数由特征 \( z_{i} \) 的符号设定。<sup>[1](https://arxiv.org/pdf/2503.11195)</sup>

## 实现方法

综述描述了三个主要过程的通用流程：预处理、特征提取和后处理；当涉及安全性时，密钥可注入这三个步骤中的任意一个。<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup> 一种四阶段表述区分了变换（例如用 DCT 或 DWT 变换到频域）、特征向量提取、量化与特征降维，以及压缩或加密为固定长度哈希。<sup>[11](https://arxiv.org/pdf/2212.08035)</sup>

**预处理**降低对微小失真的敏感性：常见操作包括图像下采样、低通滤波、缩放、顺序统计滤波、高斯模糊，<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup> 色彩空间降维和光照归一化。<sup>[12](https://www.sciencedirect.com/science/article/abs/pii/S0923596519301286)</sup>

**特征提取与量化**随后产生位。在广泛使用的基于 DCT 的 pHash 中，输入图像被缩放，进行二维[离散余弦变换](https://www.edgepedia.org/zh/discrete-cosine-transform)，保留左上角低频块，以该块的中位数为阈值，将每个系数与之比较以产生位。<sup>[13](https://github.com/jgraving/imagehash/blob/master/imagehash/__init__.py)</sup> 量化已使用均匀、Lloyd-Max 或密钥相关的随机量化器，压缩已使用纠错码的解码阶段来缩短哈希同时保持汉明距离。<sup>[14](https://terpconnect.umd.edu/~minwu/public_paper/Jnl/0606hash_IEEErev_TIFS.pdf)</sup> 后处理还可包括通过聚类进行压缩、基于密钥的置换，以及[格雷码](https://www.edgepedia.org/zh/gray-code)表示以避免自然二进制码的位错误。<sup>[5](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)</sup>

**匹配**在决策阶段使用汉明距离等距离度量比较参考哈希与测试哈希。<sup>[9](https://www.mdpi.com/1999-4893/13/9/227)</sup> 除汉明距离外的度量包括欧几里得距离、推土机距离和 L2 距离，通常归一化到零和一之间。<sup>[11](https://arxiv.org/pdf/2212.08035)</sup>

## 起源

感知哈希源于对基于视觉内容的哈希的需求，这些哈希能抵抗 JPEG 压缩、缩放和裁剪等常见互联网修改；早期鲁棒哈希方案将图像投影到密钥相关的随机平滑模式上，使用图像块的小波变换统计量，并在一种基于随机重叠图像区域上计算的矩阵不变量的方案中，将随机性作为安全性的关键加以利用。<sup>[15](http://congres.cran.univ-lorraine.fr/2004/ICIP%202004/defevent/papers/cr2945.pdf)</sup><sup> • </sup><sup>[14](https://terpconnect.umd.edu/~minwu/public_paper/Jnl/0606hash_IEEErev_TIFS.pdf)</sup> 学习哈希这条路线分为数据无关方法（如带随机投影的局部敏感哈希（LSH））和数据相关方法（用统计学习来学习哈希函数），后者包括谱哈希、二进制重构嵌入（BRE）、迭代量化（ITQ）、K 均值哈希（KMH）、最小损失哈希（MLH）和序列投影学习哈希（SPLH）。<sup>[7](https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Liong_Deep_Hashing_for_2015_CVPR_paper.pdf)</sup> [深度哈希](https://www.edgepedia.org/zh/deep-hashing)（DH）及其监督扩展 SDH 由 Venice Erin Liong 及其同事在《面向紧凑二进制码学习的深度哈希》（2015）中提出，发表于 CVPR，使用具有多个分层非线性变换的深度神经网络学习二进制码，训练受三个约束：最小化原始特征描述子与二进制向量之间的损失、均匀分布各位、使不同位尽可能独立。<sup>[7](https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Liong_Deep_Hashing_for_2015_CVPR_paper.pdf)</sup>

## 变体

**经典块与变换哈希。** imagehash 库实现了平均哈希、感知哈希、差分哈希和小波哈希，它们在亮度上分析图像结构而不含颜色信息；单独的颜色哈希分析颜色分布而不含位置信息。<sup>[2](https://github.com/johannesbuchner/imagehash)</sup> aHash 通过平均像素值判断块；dHash 使用相邻块平均像素值之间的差异；wHash 使用小波特征，保留空间域和频率域的信息，相比 aHash 和 dHash 提高了对模糊、滤波和小旋转的鲁棒性。<sup>[3](https://www.mdpi.com/2079-9292/15/7/1493)</sup> pHash 基于 DCT，如上所述。<sup>[13](https://github.com/jgraving/imagehash/blob/master/imagehash/__init__.py)</sup>

**基于学习的变体。** 核化 LSH 解决相似性由核函数给出时的快速图像搜索问题，在 \( n \) 个对象的数据库中寻求 \( \operatorname{argmax}_{i} \kappa(q, x_{i}) \)。<sup>[16](https://www.cs.utexas.edu/~grauman/papers/iccv2009_klsh.pdf)</sup> 深度哈希方法为大型图像数据集中的最近邻搜索学习哈希函数，按网络架构、训练策略、损失函数、相似性度量和量化进行分类。<sup>[17](https://link.springer.com/article/10.1007/s10115-022-01734-0)</sup>

**已部署系统。** pHash 和 PDQ 通过二维 DCT 提取全局尺度特征，分别使用 64 位和 256 位码；PhotoDNA 通过 6 × 6 梯度网格提取中尺度特征，使用 144 位码；NeuralHash 通过卷积神经网络提取像素尺度特征，使用 96 位码。<sup>[4](https://www.usenix.org/system/files/usenixsecurity25-zhang-yushu.pdf)</sup> NeuralHash 用于识别儿童性虐待材料（CSAM），将图像通过 DNN 产生特征嵌入并转换为 96 位哈希，以对比方式训练，使感知相似图像的余弦相似度高。<sup>[8](https://arxiv.org/html/2406.00918v1)</sup>

## 应用

近重复匹配系统为查询图像计算哈希，并使用相似性阈值与已注册的参考哈希比较，应用于内容审核、媒体溯源、数字取证和大规模图像搜索。<sup>[18](https://arxiv.org/pdf/2608.03101)</sup> 感知哈希主要用于检测相同文件的重复，而标准密码学哈希通常无法做到，因为密码学哈希在格式变化和轻微处理下会失效。<sup>[19](https://www.phash.org/docs/howto.html)</sup> 感知哈希也被纳入客户端和端到端加密（E2EE）系统，其中被识别为非法内容的文件会报告给提供商，而其余内容则机密发送。<sup>[20](https://www.usenix.org/system/files/usenixsecurity23-prokos.pdf)</sup> 即时通讯中广泛部署的感知哈希算法包括 pHash、PDQ、PhotoDNA 和 NeuralHash。<sup>[4](https://www.usenix.org/system/files/usenixsecurity25-zhang-yushu.pdf)</sup>

## 局限与替代方案

**失效模式。** 经典哈希方法计算高效，对精确匹配有效，但在近重复和几何变换下表现不佳，而 CNN 嵌入模型在所有重复类型上显著更鲁棒，但计算成本高。<sup>[3](https://www.mdpi.com/2079-9292/15/7/1493)</sup> 方块方案在裁剪下失效，因为裁剪会偏移块的对齐，严重改变哈希。<sup>[1](https://arxiv.org/pdf/2503.11195)</sup> 在一项实证研究中，所有三种被测感知哈希算法对压缩和缩放鲁棒，但对滤波和旋转不鲁棒，且发现 PhotoDNA 对滤波、旋转、缩放、镜像、加边框或裁剪均不鲁棒。<sup>[8](https://arxiv.org/html/2406.00918v1)</sup> 这与 pHash 库自身声称其 DCT 哈希对模糊、旋转和不同压缩格式等微小失真鲁棒形成对比；<sup>[6](http://phash.org/)</sup> 这一差异似乎取决于所测试旋转的严重程度。

**对抗脆弱性。** NeuralHash 等深度感知哈希不鲁棒：对手可通过基于梯度的扰动或标准图像变换强制或阻止哈希碰撞。<sup>[21](https://www.aiml.informatik.tu-darmstadt.de/papers/struppek2022facct_%20hash.pdf)</sup> 针对 ImageHash 算法和 PDQ 的黑盒攻击在超过一百万张图像上将修改图像距离膨胀到所有情况下假阳性率都变得不可接受的程度。<sup>[11](https://arxiv.org/pdf/2212.08035)</sup> 规避攻击做出不可察觉的改变，使修改图像的哈希越过匹配边界，不再匹配存储的参考，例如重新上传已删除的有害内容或规避版权监控。<sup>[18](https://arxiv.org/pdf/2608.03101)</sup>

**评估。** 学习哈希评估使用精确率、召回率、精确率-召回率曲线和平均精度均值，mAP 计算为各查询的精确率-召回率曲线下面积的均值，\( \sum_{t=1}^{N} P(t) \Delta(t) \)。<sup>[10](https://ar5iv.labs.arxiv.org/html/1606.00185)</sup> 基准研究使用了 UKBench 和 [Amazon Berkeley Objects](https://www.edgepedia.org/zh/amazon-berkeley-objects) 数据集，包含精确、光度与几何重复子集，用精确率-召回率、ROC、F1、MAP、NDCG、Jaccard 相似度和运行时间对归一化汉明相似度评分。<sup>[3](https://www.mdpi.com/2079-9292/15/7/1493)</sup> 深度感知哈希在经验鲁棒性和[可认证鲁棒性](https://www.edgepedia.org/zh/certified-robustness)下评估；由于只评估有限的一组修改，经验鲁棒性并不保证抵抗力。<sup>[18](https://arxiv.org/pdf/2608.03101)</sup>

## References

1. [Perceptual hashing for AI-generated content provenance (MP-FHE scheme paper)](https://arxiv.org/pdf/2503.11195)
2. [JohannesBuchner/imagehash](https://github.com/johannesbuchner/imagehash)
3. [Comparative Evaluation of Perceptual Hashing and Deep Embedding Methods for Robust and Efficient Image Deduplication (Electronics, 2025)](https://www.mdpi.com/2079-9292/15/7/1493)
4. [Atkscopes: Multiresolution Adversarial Perturbation as a Unified Attack on Perceptual Hashing and Beyond (USENIX Security 2025)](https://www.usenix.org/system/files/usenixsecurity25-zhang-yushu.pdf)
5. [A Survey on Image Hashing for Image Authentication (IEICE Trans. Inf. & Syst., E93.D, 2010)](https://www.jstage.jst.go.jp/article/transinf/E93.D/5/E93.D_5_1020/_pdf)
6. [pHash.org: Home of pHash, the open source perceptual hash library](http://phash.org/)
7. [Deep Hashing for Compact Binary Codes Learning (CVPR 2015)](https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Liong_Deep_Hashing_for_2015_CVPR_paper.pdf)
8. [Assessing the Adversarial Security of Practical Perceptual Hashing Algorithms (arXiv 2406.00918)](https://arxiv.org/html/2406.00918v1)
9. [An Image Hashing Algorithm for Authentication with Multi-Attack Reference Generation and Adaptive Thresholding](https://www.mdpi.com/1999-4893/13/9/227)
10. [A Survey on Learning to Hash](https://ar5iv.labs.arxiv.org/html/1606.00185)
11. [Perceptual hashing and content-based file matching (arXiv 2212.08035)](https://arxiv.org/pdf/2212.08035)
12. [Perceptual hashing for image authentication: A survey (Signal Processing: Image Communication, Vol. 81, 2020)](https://www.sciencedirect.com/science/article/abs/pii/S0923596519301286)
13. [imagehash/__init__.py (DCT pHash implementation)](https://github.com/jgraving/imagehash/blob/master/imagehash/__init__.py)
14. [Robust and Secure Image Hashing (IEEE TIFS review)](https://terpconnect.umd.edu/~minwu/public_paper/Jnl/0606hash_IEEErev_TIFS.pdf)
15. [Robust Perceptual Image Hashing via Matrix Invariants (ICIP 2004)](http://congres.cran.univ-lorraine.fr/2004/ICIP%202004/defevent/papers/cr2945.pdf)
16. [Kernelized Locality-Sensitive Hashing for Scalable Image Search (ICCV 2009)](https://www.cs.utexas.edu/~grauman/papers/iccv2009_klsh.pdf)
17. [Learning to hash: a comprehensive survey of deep learning-based hashing methods](https://link.springer.com/article/10.1007/s10115-022-01734-0)
18. [Double Down on Defense: Strengthening Deep Perceptual Hashes against Evasion Attacks without Retraining](https://arxiv.org/pdf/2608.03101)
19. [pHash.org: How-to / documentation](https://www.phash.org/docs/howto.html)
20. [Squint Hard Enough: Attacking Perceptual Hashing with Adversarial Machine Learning (USENIX Security 2023)](https://www.usenix.org/system/files/usenixsecurity23-prokos.pdf)
21. [Learning to Break Deep Perceptual Hashing: The Use Case NeuralHash](https://www.aiml.informatik.tu-darmstadt.de/papers/struppek2022facct_%20hash.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Computer vision › Vision methods and geometry › Recognition and matching methods

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/image-hashing

English original: https://www.edgepedia.org/image-hashing
