{ "metadata": { "name": "", "signature": "sha256:2be466caff3b15919aa3ed6f1f9c0780d540e2df062a8a8ada6b57bc30a1a838" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Medical Image Computing - Tutorial 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial gives a brief introduction to medical image data analysis, visualization, and processing. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "# load the required modules\n", "import numpy as np\n", "import SimpleITK as sitk" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Documentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SimpleITK classes and functions have the Docstrings derived from the C++ definitions and the Doxygen documentation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(sitk.Image.GetPixel)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on method GetPixel in module SimpleITK.SimpleITK:\n", "\n", "GetPixel(self, *idx) unbound SimpleITK.SimpleITK.Image method\n", " Returns the value of a pixel.\n", " \n", " This method takes 2 parameters in 2D: the x and y index,\n", " and 3 parameters in 3D: the x, y and z index.\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "image = sitk.ReadImage('../sample_data/image.nii.gz')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Image dimensions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "imageSize = image.GetSize()\n", "n_rows = imageSize[0]\n", "n_cols = imageSize[1]\n", "n_slices = imageSize[2]\n", "\n", "print \"(Image Size) number of rows:%d cols:%d slices:%d \" % (imageSize[0],imageSize[1],imageSize[2])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(Image Size) number of rows:200 cols:225 slices:200 \n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Image orientation and origin in world coordinate space" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print image.GetOrigin()\n", "print image.GetDirection()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(-99.5, -112.0, -99.5)\n", "(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Voxel size" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print image.GetSpacing()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1.0, 1.0, 1.0)\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Reading and assigning pixel values" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The pixel value at x=50, y=50, z=50 is %d\" % int(image.GetPixel(50, 50, 50))\n", "image.SetPixel(50, 50, 50, 10)\n", "print \"The new value is %d\" % int(image.GetPixel(50, 50, 50))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The pixel value at x=50, y=50, z=50 is 70\n", "The new value is 10\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "print image[50,50,50]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Image statistics" ] }, { "cell_type": "code", "collapsed": false, "input": [ "stats = sitk.StatisticsImageFilter()\n", "stats.Execute( image )\n", "max = stats.GetMaximum()\n", "min = stats.GetMinimum()\n", "print 'Min intensity = {0} \\nMax intensity = {1}'.format(min, max)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Min intensity = 0.0 \n", "Max intensity = 255.0\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Intensity rescaling" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Linearly rescale the intensity values\n", "\n", "outputMinimum = 0\n", "outputMaximum = 1024 \n", "rescaled_image = sitk.RescaleIntensity(image,outputMinimum,outputMaximum)\n", "\n", "stats.Execute( rescaled_image )\n", "max = stats.GetMaximum()\n", "min = stats.GetMinimum()\n", "print 'Min intensity = {0} \\nMax intensity = {1}'.format(min, max)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Min intensity = 0.0 \n", "Max intensity = 1024.0\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }