Practical 6: Histogram Matching & Specification

Open In Colab Run this practical in Google Colab

1. Aim

Objective

To understand and implement histogram matching (specification) — a generalization of histogram equalization that transforms an image so its histogram approximates a specified target histogram rather than a uniform one.

2. Description / Theory
Theory: Histogram equalization maps every image to a uniform distribution. Histogram matching goes further: given a source image and a target (reference) image, we transform the source so its intensity distribution matches the target's. The algorithm uses CDF inversion:
  • Compute \(\text{CDF}_s(r)\) of the source image.
  • Compute \(\text{CDF}_t(z)\) of the target image.
  • For each source intensity \(r\), find \(z\) such that \(\text{CDF}_t(z) \approx \text{CDF}_s(r)\).
  • Build a lookup table: \(\text{mapping}[r] = z\).
$$z = \text{CDF}_t^{-1}\!\bigl(\text{CDF}_s(r)\bigr)$$

Source Image

Select the source image whose histogram you want to transform.

3. Code

Part 1: Source Histogram

Visualize the source image and its pixel intensity distribution.

Part 2: Equalization Baseline

Equalize the source as a baseline. Equalization is histogram matching where the target is a uniform distribution — the simplest special case.

Part 3: Histogram Matching

Select a target (reference) image. The source image's histogram will be transformed to match the target's distribution.

Part 4: Multi-Target Comparison

Match the same source to multiple targets simultaneously: the equalized version, a high-contrast image, and a bright image. This demonstrates how different target distributions reshape the same source.

Part 5: Transfer Function Analysis

Examine the CDF-based mapping used in histogram matching: source vs. target CDFs, the derived transfer function, and verification that the matched CDF approximates the target CDF.

4. Output 5. Analysis / Conclusion

Analysis Questions

  1. How does histogram matching differ from histogram equalization? When would you prefer matching over equalization?
  2. After matching, the result histogram approximates the target but isn't identical. Why? What limitations does the discrete nature of pixel intensities impose?
  3. Compare the matched result when the target is a bright image vs. a dark image. How does the transfer function change?
  4. If you match image A to B, then match the result to A, do you get back the original? Why or why not?