{ "metadata": { "name": "", "signature": "sha256:b560c8fc7f938b6be6e74cb1859be2cd7f1eee1b1bbca25d89019754a481662b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "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", "collapsed": false, "input": [ "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" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "img = plt.imread('data/pic1.jpg')[...,0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. We create a function that displays the image along with its **histogram**." ] }, { "cell_type": "code", "collapsed": false, "input": [ "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()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Let's display the image along with its histogram." ] }, { "cell_type": "code", "collapsed": false, "input": [ "show(img)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "show(skie.rescale_intensity(img,\n", " in_range=(100, 240), out_range=(0, 255)))" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "show(skie.equalize_adapthist(img))" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": {} } ] }