{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "## Image processing" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": "import numpy as np\nimport skimage\nfrom skimage import img_as_float\nimport skimage.filters as skif\nfrom skimage.color import rgb2gray\nimport skimage.data as skid\nimport skimage.exposure as skie\nfrom IPython.html.widgets import interact\nimport matplotlib.pyplot as plt\nimport seaborn\n%matplotlib inline" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": "chelsea = skid.chelsea()" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": "((300, 451, 3), dtype('uint8'))" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": "chelsea.shape, chelsea.dtype" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": "plt.imshow(chelsea)\nplt.axis('off')" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": "img = rgb2gray(chelsea)" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": "((300, 451), dtype('float64'))" }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": "img.shape, img.dtype" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": "array([[ 0.4852, 0.4852, ..., 0.1169, 0.1169],\n [ 0.4969, 0.4930, ..., 0.1225, 0.1272 ],\n ...,\n [ 0.4248, 0.3688, ..., 0.5544, 0.5583]])" }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": "img" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": "p2, p98 = np.percentile(img, (2, 98))" }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": "img_rescale = skie.rescale_intensity(img, in_range=(p2, p98))" }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": "img_eq = skie.equalize_hist(img)" }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": "img_adapteq = img_as_float(skie.equalize_adapthist(img, clip_limit=0.03))" }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": "hist_types = dict([('Contrast stretching', img_rescale),\n ('Histogram equalization', img_eq),\n ('Adaptive equalization', img_adapteq)])" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": "@interact(hist_type=list(hist_types.keys()))\ndef display_result(hist_type):\n result = hist_types[hist_type]\n\n # We display the processed grayscale image on the left.\n plt.subplot(121)\n plt.imshow(result, cmap='gray')\n plt.axis('off')\n\n # We display the histogram on the right.\n plt.subplot(122)\n plt.hist(result.ravel(), bins=np.linspace(0., 1., 256),\n histtype='step', color='black')\n\n plt.show()" } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }