Scikit Image - Local Histogram Equalization



Local Histogram Equalization (LHE), also known as local contrast enhancement, is an image processing technique that focuses on enhancing the most frequent intensity values within different regions of an image.

In Local Histogram Equalization, the goal is to create an equalized image where the cumulative distribution function of intensity values is approximately linear for each pixel neighborhood or region. Unlike global histogram equalization, which works on the entire image, Local Histogram Equalization operates on smaller, localized regions.

To perform Local Histogram Equalization in scikit-image, you can use the rank.equalize() function from the filters module.

Using the skimage.filters.rank.equalize() functon

The skimage.filters.rank.equalize function in scikit-image is used to equalize an image using local histograms.

Syntax

Following is the syntax of this function −

skimage.filters.rank.equalize(image, footprint, out=None, mask=None, shift_x=False, shift_y=False, shift_z=False)

Parameters

  • image: The input image on which you want to perform local histogram equalization. It should be a NumPy array of shape (M, N). and the data type of the image can be uint8 or uint16.
  • footprint: The footprint parameter is a ndarray that represents the neighborhood or region over which the local histogram equalization is applied. It should be an ndarray of 1's and 0's.
  • out (optional, None): If out is not provided, a new array will be allocated for the result.
  • mask (optional, None): The mask parameter is also a ndarray that defines the area of the image to include in the local neighborhood. If not provided (default), the entire input image is used. The mask is used to exclude parts of the image from the local equalization process.
  • shift_x, shift_y, shift_z (optional): These parameters specify an offset added to the center point of the footprint. The center point must be inside the given footprint. These shifts allow you to control where the local region is centered within the footprint.

Return Value

It returns the equalized image as a NumPy array of the same data type as the input image.

Example

The following example demonstrates how to apply the local histogram equalization within a circular window using the skimage.filters.rank.equalize() function.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.util import img_as_ubyte
from skimage.morphology import disk
from skimage.filters import rank

# load original image
img = img_as_ubyte(io.imread('Images/albert_einstein_15.jpg', as_gray=True))

# Local Equalization
footprint = disk(40)
img_local = rank.equalize(img, footprint=footprint)
fig, (ax_img, ax_local) = plt.subplots(1, 2)
ax_img.imshow(img, cmap=plt.cm.gray)
ax_img.set_title('Original image')
ax_img.set_axis_off()

ax_local.imshow(img_local, cmap=plt.cm.gray)
ax_local.set_title('Local equalization')
ax_local.set_axis_off()

plt.show()

Output

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

Example

The following example demonstrates how to apply the local histogram equalization within a square window of fixed size (100x100 pixels) rather than a circular window using the skimage.filters.rank.equalize() function.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.util import img_as_ubyte
from skimage.morphology import disk
from skimage.filters import rank

# load original image
img = img_as_ubyte(io.imread('Images/albert_einstein_15.jpg', as_gray=True))

# Local Equalization within a square mask
footprint = np.ones((100, 100))
img_local = rank.equalize(img, footprint=footprint)
fig, (ax_img, ax_local) = plt.subplots(1, 2)
ax_img.imshow(img, cmap=plt.cm.gray)
ax_img.set_title('Original image')
ax_img.set_axis_off()

ax_local.imshow(img_local, cmap=plt.cm.gray)
ax_local.set_title('Local equalization')
ax_local.set_axis_off()

plt.show()

Output

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

Advertisements