Scikit Image - Histogram Equalization



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. The x-axis represents the intensity values, while 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 equalization is a technique used to improve the contrast of an image by stretching out the pixel intensities. It can improve the visibility of details and enhance the overall appearance of the image. However, it's important to note that histogram equalization can sometimes yield unnatural-looking images.

In the scikit-image library, the exposure module provides functions for histogram equalization, which are equalize_hist() and equalize_adapthist().

Using the exposure.equalize_hist() function

The exposure.equalize_hist() function is used to perform histogram equalization on an input image. And returns an image after performing the histogram equalization.

Syntax

Following is the syntax of this function −

skimage.exposure.equalize_hist(image, nbins=256, mask=None)

Parameters

  • image: The input image array on which histogram equalization will be performed.
  • nbins (optional): The number of bins to use for the image histogram. This is ignored for integer images, where each integer is treated as its own bin.
  • mask (optional): An array of the same shape as the image, specifying a mask. Only the points where the mask is True will be used for equalization. The equalization is applied to the entire image.

Return Value

It returns an image array after performing the histogram equalization. The array is of type float.

Example

The following example demonstrates how to use the exposure.equalize_hist() function on an image to get the histogram equalization.

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

# Load the input image
image = io.imread('Images/Dog.jpg')

# Perform histogram equalization
equalized_image = exposure.equalize_hist(image)

# Display the original and equalized images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(equalized_image)
axes[1].set_title('Equalized Image')
axes[1].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 the exposure.equalize_hist() function performs histogram equalization by mapping the cumulative distribution function (CDF) of pixel values onto a linear CDF. This ensures that all parts of the value range are equally represented in the image.

And it leads to enhanced details in large regions with poor contrast. However, to address exposure gradients across the image, a more refined approach can be employed using the equalize_adapthist() function.

Using the exposure.equalize_adapthist() function

This function implements Contrast Limited Adaptive Histogram Equalization (CLAHE). It is an algorithm for local contrast enhancement that operates on different tile regions of an image's histogram. This allows for the enhancement of local details even in regions that are darker or lighter than the majority of the image.

Syntax

Following is the syntax of this function −

skimage.exposure.equalize_adapthist(image, kernel_size=None, clip_limit=0.01, nbins=256)

Parameters

  • image: The input image array (ndarray).
  • kernel_size (optional): Defines the shape of contextual regions used in the algorithm. It can be an integer or an array-like object with the same number of elements as the image's dimensions. By default, the kernel size is set to 1/8 of the image's height by 1/8 of its width.
  • clip_limit (optional): The clipping limit for contrast normalization, normalized between 0 and 1. Higher values result in more contrast enhancement.
  • nbins (optional): The number of gray bins for the histogram.

Return Value

It returns an equalized image with a float64 data type. Note that, the equalize_adapthist() function performs the following steps for color image inputs:

  • The image is converted to the HSV color space.
  • The CLAHE algorithm is applied to the V (Value) channel.
  • The image is converted back to the RGB color space and returned.
  • For RGBA images, the original alpha channel is removed.

Example

Here's an example of using the exposure.equalize_adapthist() function on an image.

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

# Load the input image
image = io.imread('Images/Dog.jpg')

# Perform the Adaptive Histogram Equalization
equalized_image = exposure.equalize_adapthist(image)

# Display the original and equalized images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(equalized_image)
axes[1].set_title('Equalized Image')
axes[1].axis('off')

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

Output

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

Example

The following example shows, demonstrates the difference between equalize_adapthist() and equalize_hist() methods.

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

# Load the input image
image = io.imread('Images/Blue.jpg', as_gray=True)

# Perform histogram equalization
equalized_hist = exposure.equalize_hist(image)

# Perform CLAHE (Contrast Limited Adaptive Histogram Equalization)
equalized_adapthist = exposure.equalize_adapthist(image)

# Display the original, histogram equalized, and CLAHE equalized images
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(equalized_hist, cmap='gray')
axes[1].set_title('Histogram Equalization')
axes[1].axis('off')
axes[2].imshow(equalized_adapthist, cmap='gray')
axes[2].set_title('CLAHE (Adaptive Histogram Equalization)')
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