{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#Welcome to your first SimpleITK iPython Notebook:\n",
    "##This will teach you basically how to load an image and render it using SimpleITK's Image class.You can follow along this tutorial by hitting the \"play\" button in the navigation panel above this writing.\n",
    "\n",
    "First we import the SimpleITK Python module into this Virtual Python Environment. We will import it using a shorter name identifier \"sitk\" , for easy referencing and quick code writing.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import SimpleITK as sitk\n",
    "print(sitk.Version_ExtendedVersionString())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Making an Image and Storing it to a Variable (Remember Variables need not be initialized)\n",
    "\n",
    "Here are some ways to create an image using SimpleITK.  Note that all images are initialized by default to have a value of zero at each pixel (2D) / voxel (3D)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = sitk.Image(256, 128, 64, sitk.sitkInt16)\n",
    "image_2D = sitk.Image(64, 64, sitk.sitkFloat32)\n",
    "image_2D = sitk.Image([32,32], sitk.sitkUInt32)\n",
    "image_RGB = sitk.Image([128,128], sitk.sitkVectorUInt8, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#Accessing Image Attributes and Printing them \n",
    "\n",
    "Properties of a basic image include it size, origin, spacing between voxels (or voxel dimension), number of components per pixel (each voxel could store a vector (eg: R, G, B channels) or complex number value!), and if it is more than a scalar stored at each voxel the voxel will have its own direction, as defined by a direction matrix in terms of the orthonormal basis of Cartesian space: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image.GetSize() )\n",
    "print( image.GetWidth() )\n",
    "print( image.GetHeight() )\n",
    "print( image.GetDepth() )\n",
    "\n",
    "print( image.GetOrigin() )\n",
    "print( image.GetSpacing() )\n",
    "print( image.GetDirection() )\n",
    "print( image.GetNumberOfComponentsPerPixel() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: The starting index of a SimpleITK Image is always 0. If the output of an ITK filter has non-zero starting index, then the origin will be adjusted. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If an image is loaded from a file, the follow parameters are determined at run-time, and can be assessed as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image.GetDimension() )\n",
    "print( image.GetPixelIDValue() )\n",
    "print( image.GetPixelIDTypeAsString() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Now test out the results of these Print statements and learn about types of SimpleITK images..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is the depth of a 2D image?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image_2D.GetSize() )\n",
    "print( image_2D.GetDepth() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is the dimension and size of a Vector image?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image_RGB.GetDimension() )\n",
    "print( image_RGB.GetSize() )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image_RGB.GetNumberOfComponentsPerPixel() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Accessing Pixels\n",
    "\n",
    "Member functions ``GetPixel`` and ``SetPixel`` provide pixel access based on a pixel index. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(image.GetPixel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image.GetPixel(0, 0, 0) )\n",
    "image.SetPixel(0, 0, 0, 1)\n",
    "print( image.GetPixel(0, 0, 0) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( image[10,10,10] )\n",
    "image[10,10,10] = 100\n",
    "print( image[10,10,10] )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#Viewing a Created or Loaded Image\n",
    "\n",
    "SimpleITK does not do visualization per se (we have VTK for this!), SimpleITK does contain a built in ``Show`` method to show an image. This function writes the image out to disk and then launches a program for visualization. By default it is configured to use ImageJ, because it is readily supports all the image types which SimpleITK supports. The default visualization program is customizable by setting enviroment variables.\n",
    "\n",
    "  Note:  If the below command does not work, then edit the command to instead be:\n",
    "\n",
    "  sitk.Show(image,\"imageTitle\",True)\n",
    "\n",
    "  Then email the resulting output to your TA or instructor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sitk.Show(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try the following to learn about any function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sitk.Show?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##By converting into a numpy array, matplotlib can be used for visualization of slices of a 3D volume "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import pylab\n",
    "import numpy\n",
    "z = 10\n",
    "slice = sitk.GetArrayFromImage(image)[z,:,:]\n",
    "\n",
    "# Plot the image using default color map and no rescaling\n",
    "# You should see a blue image with a single white pixel (blue means zero in this color map)\n",
    "imgplot = pylab.imshow(slice)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the image using a grayscale color map, rescale the intensities to go from black to white, and surpress the axes\n",
    "imgplot = pylab.imshow(slice, cmap=pylab.cm.Greys_r, vmin=slice.min(), vmax=slice.max())\n",
    "pylab.axis('off')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
