{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline" ], "metadata": {} }, { "source": "
\n

# Local Histogram Equalization

\n

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

\n

The equalized image  has a roughly linear cumulative distribution function\nfor each pixel neighborhood.

\n

The local version  of the histogram equalization emphasized every local\ngraylevel variations.

\n
\n

# References

\n\n\n\n\n\n\n\n\n\n\n\n\n
\n
\n", "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": "import numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage.util.dtype import dtype_range\nfrom skimage.util import img_as_ubyte\nfrom skimage import exposure\nfrom skimage.morphology import disk\nfrom skimage.filter import rank\n\n\nmatplotlib.rcParams['font.size'] = 9\n\n\ndef plot_img_and_hist(img, axes, bins=256):\n \"\"\"Plot an image along with its histogram and cumulative histogram.\n\n \"\"\"\n ax_img, ax_hist = axes\n ax_cdf = ax_hist.twinx()\n\n # Display image\n ax_img.imshow(img, cmap=plt.cm.gray)\n ax_img.set_axis_off()\n\n # Display histogram\n ax_hist.hist(img.ravel(), bins=bins)\n ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))\n ax_hist.set_xlabel('Pixel intensity')\n\n xmin, xmax = dtype_range[img.dtype.type]\n ax_hist.set_xlim(xmin, xmax)\n\n # Display cumulative distribution\n img_cdf, bins = exposure.cumulative_distribution(img, bins)\n ax_cdf.plot(bins, img_cdf, 'r')\n\n return ax_img, ax_hist, ax_cdf\n\n\n# Load an example image\nimg = img_as_ubyte(data.moon())\n\n# Global equalize\nimg_rescale = exposure.equalize_hist(img)\n\n# Equalization\nselem = disk(30)\nimg_eq = rank.equalize(img, selem=selem)\n\n\n# Display results\nfig, axes = plt.subplots(2, 3, figsize=(8, 5))\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])\nax_img.set_title('Low contrast image')\nax_hist.set_ylabel('Number of pixels')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])\nax_img.set_title('Global equalise')\n\nax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])\nax_img.set_title('Local equalize')\nax_cdf.set_ylabel('Fraction of total intensity')\n\n\n# prevent overlap of y-axis labels\nfig.subplots_adjust(wspace=0.4)\nplt.show()", "metadata": {} } ], "metadata": {} } ], "metadata": { "name": "" } }