{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 11.1. Manipulating the exposure of an image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You need scikit-image for this recipe. You will find the installation instructions [here](http://scikit-image.org/download.html).\n", "\n", "You also need to download the *Beach* dataset. (http://ipython-books.github.io)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import the packages." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import skimage.exposure as skie\n", "%matplotlib inline\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We open an image with matplotlib. We only take a single RGB component to have a grayscale image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = plt.imread('data/pic1.jpg')[...,0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. We create a function that displays the image along with its **histogram**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def show(img):\n", " # Display the image.\n", " plt.figure(figsize=(8,2));\n", " plt.subplot(121);\n", " plt.imshow(img, cmap=plt.cm.gray);\n", " plt.axis('off');\n", " # Display the histogram.\n", " plt.subplot(122);\n", " plt.hist(img.ravel(), lw=0, bins=256);\n", " plt.xlim(0, img.max());\n", " plt.yticks([]);\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Let's display the image along with its histogram." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The histogram is unbalanced and the image appears slightly over-exposed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Now, we rescale the intensity of the image using scikit-image's `rescale_intensity` function. The `in_range` and `out_range` define a linear mapping from the original image to the modified image. The pixels that are outside `in_range` are clipped to the extremal values of `out_range`. Here, the darkest pixels (intensity less than 100) become completely black (0), whereas the brightest pixels (>240) become completely white (255)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(skie.rescale_intensity(img,\n", " in_range=(100, 240), out_range=(0, 255)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many intensity values seem to be missing in the histogram, which reflects the poor quality of this exposure correction technique." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. We now use a more advanced exposure correction technique called **Contrast Limited Adaptive Histogram Equalization**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(skie.equalize_adapthist(img))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The histogram seems more balanced, and the image now appears more contrasted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }