Scikit Image - Histogram Matching



A Histogram is a graphical representation that shows the distribution of pixel intensities of an image. For a digital image histogram plots a graph between pixel intensity versus the number of pixels. Where the x-axis represents the intensity values, and the y-axis represents the frequency or number of pixels in that particular intensity.

Histogram helps to get a basic idea about image information like contrast, brightness, intensity distribution, etc., by simply looking at the histogram of an image.

Histogram Matching using Scikit Image

Histogram matching, also known as histogram specification or histogram equalization matching, is a technique used to transform the intensity distribution of an image to match a specified target histogram. The goal of histogram matching is to take an input image and generate an output image that has a histogram shape similar to a specific or reference histogram.

Histogram matching is commonly used in various image processing applications, such as color correction, image enhancement, and feature-matching tasks.

In the scikit-image library, the exposure module provides the match_histograms() function to perform histogram matching.

The exposure.match_histograms() function

This function is used to adjust an input image so that its cumulative histogram matches that of a reference image. The adjustment is applied separately for each channel in color images.

Syntax

Following is the syntax of this function −

skimage.exposure.match_histograms(image, reference, *, channel_axis=None)

Parameters

  • image: The input image to be adjusted, which can be grayscale or color.
  • reference: The reference image whose histogram the input image will be matched to. It must have the same number of channels as the input image.
  • channel_axis: An integer or None indicates which axis of the image array corresponds to channels. If None, the image is assumed to be a grayscale (single channel) image.

Return Value

It returns a ndarray representing the transformed input image with its histogram adjusted to match that of the reference image.

Example

The following example demonstrates how to apply histogram matching on a grayscale image using exposure.match_histograms() function.

import matplotlib.pyplot as plt
from skimage import io, exposure
from skimage.util import img_as_ubyte

# Load the input image and reference image
input_image = img_as_ubyte(io.imread('Images/blue.jpg', as_gray=True))
reference_image = img_as_ubyte(io.imread('Images/tree.jpg', as_gray=True))

# Perform histogram matching
matched_image = exposure.match_histograms(input_image, reference_image)

# Display the original input image, reference image, and the histogram-matched output image
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(input_image, cmap='gray')
axes[0].set_title('Input Image')
axes[0].axis('off')
axes[1].imshow(reference_image, cmap='gray')
axes[1].set_title('Reference Image')
axes[1].axis('off')
axes[2].imshow(matched_image, cmap='gray')
axes[2].set_title('Histogram-Matched Image')
axes[2].axis('off')

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

It's important to note that, for color images: the number of channels in the input image and the reference image must be the same; otherwise, a ValueError will be raised.

Example

The following example demonstrates how to apply histogram matching on a color image using exposure.match_histograms() function.

import matplotlib.pyplot as plt
from skimage import io, exposure

# Load the input image and reference image
input_image = io.imread('Images/Tajmahal.jpg')
reference_image = io.imread('Images/rose.jpg')

# Perform histogram matching
matched_image = exposure.match_histograms(input_image, reference_image)

# Display the original input image, reference image, and the histogram-matched output image
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(input_image)
axes[0].set_title('Input Image')
axes[0].axis('off')
axes[1].imshow(reference_image)
axes[1].set_title('Reference Image')
axes[1].axis('off')
axes[2].imshow(matched_image)
axes[2].set_title('Histogram-Matched Image')
axes[2].axis('off')

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements