{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import ndimage as ndi\n",
    "\n",
    "from skimage import feature\n",
    "from scipy import misc\n",
    "fimg = misc.imread(\"contourV1-cropped.png\")\n",
    "\n",
    "from skimage import measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from skimage import color\n",
    "gimg = color.colorconv.rgb2grey(fimg)\n",
    "edges1 = feature.canny(gimg)\n",
    "edges2 = feature.canny(gimg, sigma=5.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))\n",
    "\n",
    "ax1.imshow(gimg, cmap=plt.cm.jet)\n",
    "ax1.axis('off')\n",
    "ax1.set_title('noisy image', fontsize=20)\n",
    "\n",
    "ax2.imshow(edges1, cmap=plt.cm.gray)\n",
    "ax2.axis('off')\n",
    "ax2.set_title('Canny filter, $\\sigma=1$', fontsize=20)\n",
    "\n",
    "ax3.imshow(edges2, cmap=plt.cm.gray)\n",
    "ax3.axis('off')\n",
    "ax3.set_title('Canny filter, $\\sigma=5$', fontsize=20)\n",
    "\n",
    "fig.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9,\n",
    "                    bottom=0.02, left=0.02, right=0.98)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "contours = measure.find_contours(gimg, 0.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig2, ax2 = plt.subplots()\n",
    "ax2.imshow(gimg, interpolation='nearest', cmap=plt.cm.gray)\n",
    "\n",
    "for n, contour in enumerate(contours):\n",
    "    ax2.plot(contour[:, 1], contour[:, 0], linewidth=2)\n",
    "\n",
    "ax2.axis('image')\n",
    "ax2.set_xticks([])\n",
    "ax2.set_yticks([])\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
