{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "from skimage import data, io, morphology\n", "from skimage.color import rgb2hsv, hsv2rgb, rgb2gray\n", "from pylab import *\n", "from skimage.io import Image\n", "from skimage.morphology import square\n", "import skimage as si\n", "import numpy as np\n", "from numpy import array\n", "import matplotlib.pyplot as pl\n", "\n", "def show(*args):\n", " \"Show multiple images in a row\"\n", " pl.figure(figsize=(20,12))\n", " for i,img in enumerate(args):\n", " pl.subplot(1, len(args), i+1)\n", " io.imshow(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure(figsize=(15,4))\n", "io.imshow(io.imread('img/morphology.png'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "arr = zeros([7,7],dtype='uint8')\n", "arr[1:6,1:6]=255\n", "arr" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "eroded = morphology.erosion(arr, square(3))\n", "eroded" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "morphology.dilation(eroded, square(3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Threshold\n", "coins = data.coins()\n", "binary = coins > 150\n", "show(coins, binary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Erosion\n", "eroded = Image(morphology.erosion(binary, square(3)))\n", "show(binary, eroded)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Dilatation\n", "dilated = Image(morphology.dilation(binary, square(3)))\n", "show(binary, dilated)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Closing (small dark spots were removed)\n", "dilated = Image(morphology.dilation(binary, square(5)))\n", "eroded = Image(morphology.erosion(dilated, square(5)))\n", "show(binary, dilated, eroded)\n", "result = eroded" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "opened = morphology.opening(result, square(2))\n", "show(result, opened)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "horse = rgb2gray(255 - data.horse()) > 0.5\n", "\n", "count = 3\n", "eroded = horse\n", "for i in range(count):\n", " eroded = morphology.erosion(eroded, square(5))\n", "\n", "dilated = eroded\n", "for i in range(count):\n", " dilated = morphology.dilation(dilated, square(5))\n", "\n", "show(horse, eroded, dilated)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "horse = rgb2gray(255 - data.horse()) > 0.5\n", "\n", "count = 3\n", "dilated = horse\n", "for i in range(count):\n", " dilated = morphology.dilation(dilated, square(5))\n", "\n", "eroded = dilated\n", "for i in range(count):\n", " eroded = morphology.erosion(eroded, square(5))\n", "\n", "show(horse, dilated, eroded)" ] } ], "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.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }