# 直方图匹配

直方图匹配，又称直方图规定化，是一种图像处理技术，它重新映射图像的像素强度，使其直方图逼近某个选定的目标直方图，用于对比度调整、色调传递，以及在不同条件下拍摄的图像之间的归一化。<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup><sup> • </sup><sup>[2](https://dip.dmj.one/practical/6)</sup> 它是[直方图均衡化](https://www.edgepedia.org/zh/histogram-equalization)的推广：不再强制输出分布为均匀分布，而是由使用者指定输出分布的形状，通常取参考图像的直方图。<sup>[3](https://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm)</sup><sup> • </sup><sup>[4](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心操作 | 将每个源强度 r 重映射为目标强度 z，满足 \( z = \mathrm{CDF}_{t}^{-1}(\mathrm{CDF}_{s}(r)) \)<sup>[2](https://dip.dmj.one/practical/6)</sup> |
| 与均衡化的关系 | 均衡化是目标分布为均匀分布的特例<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup><sup> • </sup><sup>[3](https://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm)</sup> |
| 颜色处理 | 当通道数相等时，scikit-image 对每个通道独立匹配<sup>[4](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html)</sup> |
| 典型用途 | 跨来源、跨光照条件进行特征匹配的轻量级归一化<sup>[4](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html)</sup> |
| 主要失效模式 | 直方图间隙、云等异常导致的 CDF 偏斜、噪声增强、逐通道着色<sup>[5](https://paulbourke.net/miscellaneous/equalisation/)</sup><sup> • </sup><sup>[6](https://developers.google.com/earth-engine/tutorials/community/histogram-matching)</sup><sup> • </sup><sup>[7](https://perso.telecom-paristech.fr/gousseau/artefacts.pdf)</sup> |
| 标准实现 | `skimage.exposure.match_histograms`、MATLAB `imhistmatch`、Earth Engine 逐波段 LUT 教程<sup>[8](https://scikit-image.org/docs/stable/api/skimage.exposure.html)</sup><sup> • </sup><sup>[9](https://la.mathworks.com/help/images/ref/imhistmatch.html)</sup> |

## 工作原理

该方法建立在两个累积分布函数的复合之上。设 \( H \) 为源灰度级的 CDF，\( G \) 为期望输出密度的 CDF；变换为 \( y = G^{-1}(H(x)) \)，它将源分布映射到目标分布上。<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup> 等价地，匹配可以写成均衡化变换与目标的反均衡化变换的复合 \( \tau_{xz} = P_{z}^{-1}(P_{x}) \)。<sup>[10](https://doi.org/10.15353/vsnl.v3i1.170)</sup> 一种常见定义使用广义逆 \( F^{-1}(t) = \min\{\lambda \in \mathbb{R};\, F(\lambda) \geq t\} \)。<sup>[11](https://perso.telecom-paristech.fr/gousseau/rabin_icip10.pdf)</sup>

对于连续函数，对大多数密度而言逆 CDF 难以解析求值，但对于离散图像可以毫无困难地近似，这正是数字算法实用的原因。<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup> 经典的连续域原理对实值图像是良定义的，此时两个 CDF 变换是一一对应的；而对于量化数字图像——也就是所有数字成像系统的情况——精确解变得不适定，因为经验 CDF 是阶跃函数，需要严格单调性且并非精确可逆，所以普通的离散实现只能近似目标分布；这一差距催生了下文所述的精确变体和变分变体。<sup>[12](https://mnikolova.perso.math.cnrs.fr/histR18.pdf)</sup>

## 具体做法

在 8 位灰度图像上，使用者执行以下步骤。<sup>[2](https://dip.dmj.one/practical/6)</sup>

1. 计算源图像和目标图像的 256 箱直方图。
2. 对两个直方图的累积和进行归一化，得到源 CDF 和目标 CDF。
3. 对每个源强度 \( r \)，找到使 \( |\mathrm{CDF}_{t}(z) - \mathrm{CDF}_{s}(r)| \) 最小的目标强度 \( z \)，并将其存入查找表：\( \mathrm{mapping}[r] = z \)。代码中为 `lut[r] = np.argmin(np.abs(tgt_cdf - src_cdf[r]))`。
4. 通过查找表重映射每个像素。
5. 用匹配后 CDF 与目标 CDF 之间的平均绝对 CDF 误差验证结果。

scikit-image 的实现以分位数形式遵循同样的逻辑：它将归一化分位数计算为累积计数除以数组大小，然后用 `np.interp` 在源分位数处插值模板值以重映射源图像。<sup>[13](https://github.com/scikit-image/scikit-image/blob/98e157969bafabdff8b262ef2328f02c797ac8d3/skimage/exposure/histogram_matching.py)</sup> 匹配之后，输出的累积直方图对每个通道都逼近参考图像的累积直方图；普通的分位数与查找表实现通常并不保证完全相等。<sup>[4](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html)</sup>

## 起源

直方图匹配起源于早期数字图像处理文献中的直接直方图规定化，它是一种交互式增强算子，允许用户指定期望的输出密度 \( p_{y} \)，并据此变换图像的概率密度函数。<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup> 那项早期工作在一张照明不良的四分之一美元硬币图像上展示了它相对于均衡化的优势，并指出均衡化是作为特例 \( G^{-1}(H(x)) = H(x) \) 得到的。<sup>[1](https://doi.org/10.1016/0094-114x(77)90062-3)</sup> 后来的文献统一了术语：生成具有指定目标直方图的输出图像称为直方图规定化或直方图匹配，均衡化作为特例包含在内。<sup>[14](https://mdpi-res.com/d_attachment/jimaging/jimaging-08-00247/article_deploy/jimaging-08-00247-v2.pdf?version=1663244389)</sup>

## 变体

**精确直方图规定化**消除了 CDF 方法的近似误差。像素被置于严格排序之下，有序序列被分成大小与目标箱计数相匹配的组，每组被赋予一个灰度级，因此只要目标直方图有效（其各箱之和等于图像大小），输出直方图就与指定直方图完全相等。<sup>[15](http://dev.ipol.im/~morel/LivreGMR/A%20CITER/exact_histogram_specification.pdf)</sup> 最简单的精确方法需要 \( O(M \log M) \) 次排序计算，其中 \( M \) 是像素数；一种 SSIM 优化的变体保持相同的每次迭代复杂度，但无法直接处理彩色图像，因为 SSIM 是为灰度图像定义的。<sup>[16](https://arxiv.org/pdf/0901.0065)</sup> 基于局部对比度的像素排序是一种较新的精确变体。<sup>[14](https://mdpi-res.com/d_attachment/jimaging/jimaging-08-00247/article_deploy/jimaging-08-00247-v2.pdf?version=1663244389)</sup> 直方图修正也曾通过图像演化方程以及保持输入图像平均亮度的方法来处理，精确规定化被归入变分和 PDE 类变体之中。<sup>[17](https://raymondhonfu.github.io/paper/histogram_proc.pdf)</sup>

**CLAHE**（对比度受限自适应直方图均衡化）通过局部计算直方图来改变结果。在 scikit-image 中，`equalize_adapthist` 处理瓦片状的上下文区域，其默认 `kernel_size` 为图像高度的 1/8 乘宽度的 1/8，`clip_limit` 归一化在 0 到 1 之间；对于彩色图像，它转换为 HSV，在 V 通道上运行 CLAHE，再转换回来。<sup>[8](https://scikit-image.org/docs/stable/api/skimage.exposure.html)</sup>

**颜色处理。** 逐通道 RGB 匹配简单，但当某个通道分布狭窄时可能产生着色效应。<sup>[5](https://paulbourke.net/miscellaneous/equalisation/)</sup> 精确规定化路线将 RGB 转换为 HSI，在强度分量上对像素排序，对 I 的直方图进行规定化，再转换回来，因此不会发生颜色偏移。<sup>[15](http://dev.ipol.im/~morel/LivreGMR/A%20CITER/exact_histogram_specification.pdf)</sup> 通道独立匹配比同时多通道（三维直方图）方法快得多，但某些颜色可能被变换到原始介质色域之外，需要校正方法。<sup>[18](https://www.imaging.org/common/uploaded%20files/pdfs/Papers/2001/PICS-0-251/4654.pdf)</sup>

## 应用

**MRI 强度标准化。** 当目标概率质量函数固定且与输入图像无关时，如在 MRI 标准化中，直方图匹配相当于先对源图像做均衡化，再通过固定目标的逆 CDF 进行映射；只有目标为均匀分布时它才退化为均衡化本身。<sup>[10](https://doi.org/10.15353/vsnl.v3i1.170)</sup> 流行的方法是直方图匹配的分段线性近似，具体是非均匀梯形黎曼近似；其近似误差会在匹配后的直方图中引入伪影，随着控制点数量 N 的增加而改善。<sup>[10](https://doi.org/10.15353/vsnl.v3i1.170)</sup>

**遥感。** 一个 Google Earth Engine 社区教程对 3 波段 RGB 图像执行直方图匹配，逐波段强制源图像的 CDF 匹配目标图像，使用 `reduceRegion` 和逐波段查找表。<sup>[6](https://developers.google.com/earth-engine/tutorials/community/histogram-matching)</sup>

**深度学习流水线。** 一篇 2025 年的分类论文用连续的、基于排序的值替换取代了不可微的 argmin/LUT 步骤，使直方图匹配可以端到端训练；目标分布是每个颜色通道一组可训练参数 \( p_{c} \in \mathbb{R}^{s} \)，通过排序像素、按秩替换值并裁剪到 [0, 1] 来应用。<sup>[19](https://arxiv.org/abs/2506.01346)</sup> 在去噪中，一篇 2025 年的论文将未知的真实噪声直方图匹配到类高斯噪声，从而单一固定水平的高斯去噪器就足够了，对信号相关噪声使用分块匹配，对空间相关噪声使用频域匹配。<sup>[20](https://arxiv.org/html/2510.06757)</sup> 直方图约束的图像生成将直方图匹配视为最优传输来引导扩散采样，Hist2Style 在 Y'CbCr 空间中将直方图条件化的风格化暴露为交互式滑块。<sup>[21](https://arxiv.org/html/2606.31683)</sup><sup> • </sup><sup>[22](https://github.laiyagushi.com/MAPS-research/hig)</sup><sup> • </sup><sup>[23](https://openaccess.thecvf.com/content/CVPR2026/papers/Galor_Hist2Style_Histogram-Guided_Stylization_with_Bilateral_Grids_CVPR_2026_paper.pdf)</sup> 直方图匹配还在 SD1.5 和 SDXL 上引导扩散的评估中作为标准颜色传递基线。<sup>[24](https://arxiv.org/abs/2503.19034)</sup>

**软件。** scikit-image 提供 `skimage.exposure.match_histograms(image, reference, *, channel_axis=None)`，它调整图像使其累积直方图匹配另一图像的累积直方图，逐通道分别应用调整，并要求通道数相等。<sup>[8](https://scikit-image.org/docs/stable/api/skimage.exposure.html)</sup><sup> • </sup><sup>[13](https://github.com/scikit-image/scikit-image/blob/98e157969bafabdff8b262ef2328f02c797ac8d3/skimage/exposure/histogram_matching.py)</sup> MATLAB 的 `imhistmatch` 调整二维灰度或真彩色图像的直方图，使其近似匹配参考图像的直方图，并提供一种多项式方法，在参考图像比输入图像更暗时有用。<sup>[9](https://la.mathworks.com/help/images/ref/imhistmatch.html)</sup>

## 局限与替代方法

**失效模式。** 匹配会扭曲直方图并在输出直方图中引入间隙，对图像这类离散数据集尤其如此。<sup>[5](https://paulbourke.net/miscellaneous/equalisation/)</sup> 一幅图像中存在而参考图像中不存在的异常（如云）会使 CDF 偏斜并降低结果质量，源与目标之间的配准误差同样如此。<sup>[6](https://developers.google.com/earth-engine/tutorials/community/histogram-matching)</sup> 对比度和颜色传递操作会导致四种主要视觉伪影：噪声增强、压缩伪影（颜色相近的像素被映射为不同颜色，例如 JPEG 之后）、细节丢失和颜色比例问题。<sup>[7](https://perso.telecom-paristech.fr/gousseau/artefacts.pdf)</sup> 从机制上讲，当映射增大相邻颜色或灰度级之间的距离时会出现噪声增强和压缩伪影，当映射减小这些距离时则出现细节丢失。<sup>[11](https://perso.telecom-paristech.fr/gousseau/rabin_icip10.pdf)</sup>

**替代方法。** 直方图均衡化只产生均匀的输出分布，因此无法突出选定的强度范围；规定化将给定分布映射为期望分布，以均衡化图像作为中间阶段。<sup>[3](https://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm)</sup> Reinhard 风格的方法应用仿射变换匹配颜色分布的均值和方差（在 \( l\alpha\beta \) 空间中的通道均值和标准差）；对于分布相似且简单的图像它可以满足要求，但在一般情况下通常失败。<sup>[7](https://perso.telecom-paristech.fr/gousseau/artefacts.pdf)</sup><sup> • </sup><sup>[25](https://www.dannyadam.com/blog/wp-content/uploads/2022/03/colortrans.pdf)</sup> 线性直方图匹配则通过 \( x \cdot \beta + \alpha \) 匹配目标的均值和协方差，避免剧烈改变图像外观。<sup>[25](https://www.dannyadam.com/blog/wp-content/uploads/2022/03/colortrans.pdf)</sup> 对于完整的颜色传递，三维直方图匹配可以用单纯形算法估计，但计算代价高昂；随机轴上的迭代一维最优映射速度快，但在 Monge-Kantorovich 意义上并非最优。<sup>[11](https://perso.telecom-paristech.fr/gousseau/rabin_icip10.pdf)</sup><sup> • </sup><sup>[7](https://perso.telecom-paristech.fr/gousseau/artefacts.pdf)</sup>

**数据说明了什么。** 一项 2023 年对全局直方图均衡化、CLAHE、直方图规定化和 BPDHE 的数学分析发现，它们作为非线性方法都会造成数据损失，在脑肿瘤 MRI 和结直肠组织病理学数据集上，皮尔逊相关系数和 SSIM 都落在 0.6 到 0.95 的范围内；线性 Reinhard 方法在医学图像增强方面优于所有这些 HE 家族方法。<sup>[26](https://link.springer.com/article/10.1007/s11042-023-15799-8)</sup> 相比之下，一种高斯直方图规定化技术（HGHS）报告平均保留输入图像熵的 98.15%，高于所比较的对比度增强技术。<sup>[27](https://www.iieta.org/journals/ria/paper/10.18280/ria.330603)</sup> 这些发现测量的是不同数据集上的不同量（SSIM/PCC 保真度与熵保留）。近期工作还表明视觉质量与任务准确率会出现分歧：可微分类方法学到的目标分布是非均匀的，匹配后的图像往往看起来有噪声或充满伪影，却能提高分类准确率。<sup>[19](https://arxiv.org/abs/2506.01346)</sup>

## References

1. [Gray-level transformations for interactive image enhancement](https://doi.org/10.1016/0094-114x(77)90062-3)
2. [Practical 6: Histogram Matching | CSU2543](https://dip.dmj.one/practical/6)
3. [Point Operations - Histogram Equalization](https://homepages.inf.ed.ac.uk/rbf/HIPR2/histeq.htm)
4. [Histogram matching, skimage 0.26.0 documentation](https://scikit-image.org/docs/stable/auto_examples/color_exposure/plot_histogram_matching.html)
5. [Histogram Matching](https://paulbourke.net/miscellaneous/equalisation/)
6. [Histogram Matching | Google Earth Engine](https://developers.google.com/earth-engine/tutorials/community/histogram-matching)
7. [Removing Artefacts From Color and Contrast Changes](https://perso.telecom-paristech.fr/gousseau/artefacts.pdf)
8. [skimage.exposure, skimage 0.26.0 documentation](https://scikit-image.org/docs/stable/api/skimage.exposure.html)
9. [imhistmatch, MATLAB Image Processing Toolbox documentation](https://la.mathworks.com/help/images/ref/imhistmatch.html)
10. [Equivalence of histogram equalization, histogram matching and the Nyul algorithm for intensity standardization in MRI](https://doi.org/10.15353/vsnl.v3i1.170)
11. [Regularization of Transportation Maps for Color and Contrast Transfer](https://perso.telecom-paristech.fr/gousseau/rabin_icip10.pdf)
12. [Author's personal copy (Nikolova, histogram specification)](https://mnikolova.perso.math.cnrs.fr/histR18.pdf)
13. [skimage/exposure/histogram_matching.py](https://github.com/scikit-image/scikit-image/blob/98e157969bafabdff8b262ef2328f02c797ac8d3/skimage/exposure/histogram_matching.py)
14. [Local Contrast-Based Pixel Ordering for Exact Histogram Specification](https://mdpi-res.com/d_attachment/jimaging/jimaging-08-00247/article_deploy/jimaging-08-00247-v2.pdf?version=1663244389)
15. [Exact Histogram Specification](http://dev.ipol.im/~morel/LivreGMR/A%20CITER/exact_histogram_specification.pdf)
16. [Exact Histogram Specification Optimized for Structural Similarity](https://arxiv.org/pdf/0901.0065)
17. [A Variational Approach for Exact Histogram Specification](https://raymondhonfu.github.io/paper/histogram_proc.pdf)
18. [The Influence of Image Histograms on Cross-Media Colour Image Reproduction](https://www.imaging.org/common/uploaded%20files/pdfs/Papers/2001/PICS-0-251/4654.pdf)
19. [Rethinking Image Histogram Matching for Image Classification](https://arxiv.org/abs/2506.01346)
20. [Transforming Noise Distributions with Histogram Matching: Towards a Single Denoiser for All](https://arxiv.org/html/2510.06757)
21. [Histogram-constrained Image Generation](https://arxiv.org/html/2606.31683)
22. [MAPS-research/hig GitHub repository](https://github.laiyagushi.com/MAPS-research/hig)
23. [Hist2Style: Histogram-Guided Stylization with Bilateral Grids (CVPR 2026)](https://openaccess.thecvf.com/content/CVPR2026/papers/Galor_Hist2Style_Histogram-Guided_Stylization_with_Bilateral_Grids_CVPR_2026_paper.pdf)
24. [Color Conditional Generation with Sliced Wasserstein Guidance](https://arxiv.org/abs/2503.19034)
25. [colortrans Color Transfer Algorithms](https://www.dannyadam.com/blog/wp-content/uploads/2022/03/colortrans.pdf)
26. [Mathematical analysis of histogram equalization techniques for medical image enhancement: a tutorial from the perspective of data loss](https://link.springer.com/article/10.1007/s11042-023-15799-8)
27. [Histogram Shape Based Gaussian Histogram Specification for Contrast Enhancement](https://www.iieta.org/journals/ria/paper/10.18280/ria.330603)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Sep 30, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/histogram-matching

English original: https://www.edgepedia.org/histogram-matching
