{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Introduction to the Insight Toolkit (ITK)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Learning Objectives\n",
    "\n",
    "* Learn how to **run** cells in **a Jupyter Notebook**\n",
    "* Run a segmentation example that demonstrates **ITK**'s ability to provide **insight into images**\n",
    "* Understand the **purpose and capabilities** of the toolkit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Jupyter Notebooks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "These are [Jupyter Notebooks](https://jupyter.org/), an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.\n",
    "\n",
    "To run cells in the notebook, press *shift + enter*.\n",
    "\n",
    "For more information, see the [Notebook Help](https://nbviewer.jupyter.org/github/ipython/ipython/blob/3.x/examples/Notebook/Index.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Insight Into Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "import itk\n",
    "\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4cdfc916fe954f998b62ecd4b18f637e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(gradient_opacity=0.8, rendered_image=<itkImagePython.itkImageF3; proxy of <Swig Object of type 'itkImag…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "file_name = 'data/brainweb165a10f17.mha'\n",
    "image = itk.imread(file_name, itk.ctype('float'))\n",
    "view(image, slicing_planes=True, gradient_opacity=0.8, ui_collapsed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e83c0d5f34a5421f8aef475ea7e928ae",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(gradient_opacity=0.8, rendered_image=<itkImagePython.itkImageF3; proxy of <Swig Object of type 'itkImag…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Smooth the image\n",
    "smoothed = itk.curvature_flow_image_filter(image,\n",
    "                                          number_of_iterations=6,\n",
    "                                          time_step=0.005)\n",
    "view(smoothed, slicing_planes=True, gradient_opacity=0.8, ui_collapsed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# Segment the white matter with a 3D region-growing algorithm\n",
    "confidence_connected = itk.ConfidenceConnectedImageFilter.New(smoothed)\n",
    "confidence_connected.SetMultiplier(2.5)\n",
    "confidence_connected.SetNumberOfIterations(5)\n",
    "confidence_connected.SetInitialNeighborhoodRadius(2)\n",
    "confidence_connected.SetReplaceValue(255)\n",
    "\n",
    "confidence_connected.AddSeed([118, 133, 92])\n",
    "confidence_connected.AddSeed([63, 135, 94])\n",
    "confidence_connected.AddSeed([63, 157, 90])\n",
    "confidence_connected.AddSeed([111, 150, 90])\n",
    "confidence_connected.AddSeed([111, 50, 88])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5d0fab5c630e4ed28e79fd4253b94f53",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(annotations=False, cmap='BuPu', gradient_opacity=0.22, rendered_image=<itkImagePython.itkImageSS3; prox…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "confidence_connected.Update()\n",
    "view(confidence_connected, ui_collapsed=True, cmap='BuPu', shadow=False, annotations=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## History of ITK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Insight into Images\n",
    "\n",
    "![Visible Human Head Rendering](data/VisibleHumanHeadRender.png)\n",
    "\n",
    "http://itk.org"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### History\n",
    "\n",
    "In 1999, the US National Institute of Health’s (NIH) National Library of Medicine (NLM) started a project to support the Visible Human Project.\n",
    "\n",
    "![Visible Human Logo](data/VisibleHumanLogo.png)\n",
    "\n",
    "![Visible Human Slice](data/VisibleHumanSlice.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Goals\n",
    "\n",
    "* Collect best-of-the-best image analysis algorithms for reproducible science.\n",
    "* Provide a software resource for teaching medical image analysis algorithms.\n",
    "* Establish a foundation for future research.\n",
    "* Support commercial applications.\n",
    "* Create conventions for future work.\n",
    "* Grow a self-sustaining community of software users and developers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Continued Development\n",
    "\n",
    "* Development has progressed since 1999\n",
    "* Contributions from over 267 developers\n",
    "* Over 1.6 million lines of code\n",
    "* Downloaded over 500,000 times\n",
    "\n",
    "![ITK contributors](data/itk-contributors.png)\n",
    "\n",
    "ITK contributors locations for the 4.8 and 4.9 releases."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### N-Dimensional Image Filtering\n",
    "\n",
    "![Filtering](data/Filtering.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Filtering Algorithms Classes\n",
    "\n",
    "* Fast marching methods\n",
    "* Convolution\n",
    "* Image gradient\n",
    "* Denoising\n",
    "* Thresholding\n",
    "* Mathematical morphology\n",
    "* Smoothing\n",
    "* Image features\n",
    "* Image statistics\n",
    "* Bias correction\n",
    "* Image grid operations\n",
    "* ...."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "\"Image segmentation is the process of partitioning a digital image into multiple segments, i.e. sets of pixels. The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze.\"\n",
    "\n",
    "Source: [Wikipedia](https://en.wikipedia.org/wiki/Image_segmentation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/SegmentationLevelSet.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/Segmentation1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/Segmentation2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/Segmentation3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/Segmentation4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Segmentation\n",
    "\n",
    "![Segmentation](data/Segmentation5.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Registration\n",
    "\n",
    "\"Image registration is the process of transforming different sets of data into one coordinate system. [...] Registration is necessary in order to be able to compare or integrate the data obtained from these different measurements.\"\n",
    "\n",
    "Source: [Wikipedia](https://en.wikipedia.org/wiki/Image_registration)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Registration\n",
    "\n",
    "![Registration](data/Registration.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Other Data Structures\n",
    "\n",
    "* Mesh's\n",
    "* Transforms's\n",
    "* SpatialObject's\n",
    "* Path's\n",
    "* LabelMap's\n",
    "\n",
    "![Mesh](data/Mesh.png)\n",
    "\n",
    "![Transform](data/Transformation.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### ITK Resources\n",
    "\n",
    "* ITK Software Guide: https://www.itk.org/ItkSoftwareGuide.pdf\n",
    "\n",
    "* Discourse Discussion: https://discourse.itk.org\n",
    "\n",
    "* Sphinx Examples: https://www.itk.org/ITKExamples\n",
    "\n",
    "* `Examples/` directory in the ITK repository\n",
    "\n",
    "* Wiki Examples: http://www.itk.org/Wiki/ITK/Examples\n",
    "\n",
    "* Doxygen: http://www.itk.org/Doxygen/html/index.html\n",
    "\n",
    "* Kitware: https://www.kitware.com/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Kitware, Inc\n",
    "\n",
    "\n",
    "![Kitware ITK](data/KitwareITK.jpg)\n",
    "\n",
    "![Kitware overview](data/KitwareOverview.jpg)\n",
    "\n",
    "- Collaborative, Open Source, Scientific Software Development\n",
    "- Carrboro, NC, near University of North Carolina-Chapel Hill, in the Research Triangle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Enjoy ITK!"
   ]
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}