""" ====================== Histogram Equalization ====================== This examples enhances an image with low contrast, using a method called *histogram equalization*, which "spreads out the most frequent intensity values" in an image [1]_. The equalized image has a roughly linear cumulative distribution function. While histogram equalization has the advantage that it requires no parameters, it sometimes yields unnatural looking images. An alternative method is *contrast stretching*, where the image is rescaled to include all intensities that fall within the 2nd and 98th percentiles [2]_. .. [1] http://en.wikipedia.org/wiki/Histogram_equalization .. [2] http://homepages.inf.ed.ac.uk/rbf/HIPR2/stretch.htm """ from skimage import data from skimage.util.dtype import dtype_range from skimage import exposure import matplotlib.pyplot as plt import numpy as np 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 = data.moon() # Contrast stretching p5 = np.percentile(img, 2) p95 = np.percentile(img, 98) img_rescale = exposure.rescale_intensity(img, in_range=(p5, p95)) # Equalization img_eq = exposure.equalize(img) # Display results f, axes = plt.subplots(2, 3, figsize=(11, 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('Contrast stretching') ax_img, _, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2]) ax_img.set_title('Histogram equalization') ax_cdf.set_ylabel('Fraction of total intensity') # prevent overlap of y-axis labels plt.subplots_adjust(wspace=0.4) plt.show()