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.

References

[1]http://en.wikipedia.org/wiki/Histogram_equalization
[2]http://en.wikipedia.org/wiki/Adaptive_histogram_equalization
../_images/plot_local_equalize_1.png

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from 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


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


def plot_img_and_hist(img, axes, bins=256):
    """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()

STDOUT


        

STDERR


        

Python source code: download (generated using skimage 0.11dev)

IPython Notebook: download (generated using skimage 0.11dev)

aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCBtYXRwbG90bGliCmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHQKCmZyb20gc2tpbWFnZSBpbXBvcnQgZGF0YQpmcm9tIHNraW1hZ2UudXRpbC5kdHlwZSBpbXBvcnQgZHR5cGVfcmFuZ2UKZnJvbSBza2ltYWdlLnV0aWwgaW1wb3J0IGltZ19hc191Ynl0ZQpmcm9tIHNraW1hZ2UgaW1wb3J0IGV4cG9zdXJlCmZyb20gc2tpbWFnZS5tb3JwaG9sb2d5IGltcG9ydCBkaXNrCmZyb20gc2tpbWFnZS5maWx0ZXIgaW1wb3J0IHJhbmsKCgptYXRwbG90bGliLnJjUGFyYW1zWydmb250LnNpemUnXSA9IDkKCgpkZWYgcGxvdF9pbWdfYW5kX2hpc3QoaW1nLCBheGVzLCBiaW5zPTI1Nik6CiAgICAiIiJQbG90IGFuIGltYWdlIGFsb25nIHdpdGggaXRzIGhpc3RvZ3JhbSBhbmQgY3VtdWxhdGl2ZSBoaXN0b2dyYW0uCgogICAgIiIiCiAgICBheF9pbWcsIGF4X2hpc3QgPSBheGVzCiAgICBheF9jZGYgPSBheF9oaXN0LnR3aW54KCkKCiAgICAjIERpc3BsYXkgaW1hZ2UKICAgIGF4X2ltZy5pbXNob3coaW1nLCBjbWFwPXBsdC5jbS5ncmF5KQogICAgYXhfaW1nLnNldF9heGlzX29mZigpCgogICAgIyBEaXNwbGF5IGhpc3RvZ3JhbQogICAgYXhfaGlzdC5oaXN0KGltZy5yYXZlbCgpLCBiaW5zPWJpbnMpCiAgICBheF9oaXN0LnRpY2tsYWJlbF9mb3JtYXQoYXhpcz0neScsIHN0eWxlPSdzY2llbnRpZmljJywgc2NpbGltaXRzPSgwLCAwKSkKICAgIGF4X2hpc3Quc2V0X3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