{ "metadata": { "name": "", "signature": "sha256:1d1cd05219c0506492561566dc88b67fc9c57e7b39025492312434b7dc203816" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Morphological operations\n", "\n", "Morphology is the study of shapes. In image processing, some simple operations can get you a long way. The first things to learn are *erosion* and *dilation*. In erosion, we look at a pixel\u2019s local neighborhood and replace the value of that pixel with the minimum value of that neighborhood. In dilation, we instead choose the maximum." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from matplotlib import pyplot as plt, cm\n", "%matplotlib inline\n", "import skdemo\n", "plt.rcParams['image.cmap'] = 'cubehelix'\n", "plt.rcParams['image.interpolation'] = 'none'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "image = np.array([[0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)\n", "plt.imshow(image)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The documentation for scikit-image's morphology module is\n", "[here](http://scikit-image.org/docs/0.10.x/api/skimage.morphology.html).\n", "\n", "Importantly, we must use a *structuring element*, which defines the local\n", "neighborhood of each pixel. To get every neighbor (up, down, left, right, and\n", "diagonals), use `morphology.square`; to avoid diagonals, use\n", "`morphology.diamond`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from skimage import morphology\n", "sq = morphology.square(width=3)\n", "dia = morphology.diamond(radius=1)\n", "print(sq)\n", "print(dia)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The central value of the structuring element represents the pixel being considered, and the surrounding values are the neighbors: a 1 value means that pixel counts as a neighbor, while a 0 value does not. So:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "skdemo.imshow_all(image, morphology.erosion(image, sq), shape=(1, 2))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "code", "collapsed": false, "input": [ "skdemo.imshow_all(image, morphology.dilation(image, sq))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "code", "collapsed": false, "input": [ "skdemo.imshow_all(image, morphology.dilation(image, dia))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Erosion and dilation can be combined into two slightly more sophisticated operations, *opening* and *closing*. Here's an example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)\n", "plt.imshow(image)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens when run an erosion followed by a dilation of this image?\n", "\n", "What about the reverse?\n", "\n", "Try to imagine the operations in your head before trying them out below." ] }, { "cell_type": "code", "collapsed": false, "input": [ "skdemo.imshow_all(image, morphology.opening(image, sq)) # erosion -> dilation" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "skdemo.imshow_all(image, morphology.closing(image, sq)) # dilation -> erosion" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**: use morphological operations to remove noise from a binary image." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from skimage import data, color\n", "hub = color.rgb2gray(data.hubble_deep_field()[350:450, 90:190])\n", "plt.imshow(hub)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remove the smaller objects to retrieve the large galaxy." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }