In [5]:
%matplotlib inline

Local Histogram Equalization

This examples enhances an image with low contrast, using a method called local histogram equalization, which spreads out the most frequent intensity values in an image.

The equalized image [1] has a roughly linear cumulative distribution function for each pixel neighborhood.

The local version [2] of the histogram equalization emphasized every local graylevel variations.


In [6]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

fr

matplotlib.rcParams['font.size'] = 9


def plot_img_and_hist(img, axes, bins=256)om skimage import data
from skimage.util.dtype import dtype_range
from skimage.util import img_as_ubyte
from skimage import exposure
from skimage.morphology import disk
from skimage.filter import rank
:
    """Plot an image along with its histogram and cumulative histogram.

    """
    ax_img, ax_hist = axes
    ax_cdf = ax_hist.twinx()

    # Display image
    ax_img.imshow(img, cmap=plt.cm.gray)
    ax_img.set_axis_off()

    # Display histogram
    ax_hist.hist(img.ravel(), bins=bins)
    ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
    ax_hist.set_xlabel('Pixel intensity')

    xmin, xmax = dtype_range[img.dtype.type]
    ax_hist.set_xlim(xmin, xmax)

    # Display cumulative distribution
    img_cdf, bins = exposure.cumulative_distribution(img, bins)
    ax_cdf.plot(bins, img_cdf, 'r')

    return ax_img, ax_hist, ax_cdf


# Load an example image
img = img_as_ubyte(data.moon())

# Global equalize
img_rescale = exposure.equalize_hist(img)

# Equalization
selem = disk(30)
img_eq = rank.equalize(img, selem=selem)


# Display results
fig, axes = plt.subplots(2, 3, figsize=(8, 5))

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
ax_img.set_title('Low contrast image')
ax_hist.set_ylabel('Number of pixels')

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])
ax_img.set_title('Global equalise')

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])
ax_img.set_title('Local equalize')
ax_cdf.set_ylabel('Fraction of total intensity')


# prevent overlap of y-axis labels
fig.subplots_adjust(wspace=0.4)
plt.show()



In [38]:
from skimage import data
from skimage.util import img_as_ubyte
from skimage.util import random_noise

# Load an example image
img = skimage.util.img_as_ubyte(data.lena())
img_noise_1 = skimage.util.random_noise(img, mode='gaussian', seed=None, clip=True)
img_noise_2 = skimage.util.random_noise(img, mode='gaussian', seed=None, clip=True)

fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].set_axis_off()
axes[1].set_axis_off()
axes[2].set_axis_off()
axes[0].imshow(img)
axes[1].imshow(img_noise_1)
axes[2].imshow(img_noise_2)


Out[38]:
<matplotlib.image.AxesImage at 0x2bcc0ac8>