Welcome to the first SimpleITK Notebook demo:

SimpleITK Image Basics

This document will give a brief orientation to the SimpleITK Image class.

First we import the SimpleITK Python module. By convention our module is imported into the shorter and more pythonic "sitk" local name.

In [1]:
import SimpleITK as sitk

Image Construction

There are a variety of ways to create an image. All images' initial value is well defined as zero.

In [2]:
image = sitk.Image(256, 128, 64, sitk.sitkInt16)
image_2D = sitk.Image(64, 64, sitk.sitkFloat32)
image_2D = sitk.Image([32,32], sitk.sitkUInt32)
image_RGB = sitk.Image([128,128], sitk.sitkVectorUInt8, 3)

Pixel Types

The pixel type is represented as an enumerated type. The following is a table of the enumerated list.

sitkUInt8 Unsigned 8 bit integer
sitkInt8 Signed 8 bit integer
sitkUInt16 Unsigned 16 bit integer
sitkInt16 Signed 16 bit integer
sitkUInt32 Unsigned 32 bit integer
sitkInt32 Signed 32 bit integer
sitkUInt64 Unsigned 64 bit integer
sitkInt64 Signed 64 bit integer
sitkFloat32 32 bit float
sitkFloat64 64 bit float
sitkComplexFloat32 complex number of 32 bit float
sitkComplexFloat64 complex number of 64 bit float
sitkVectorUInt8 Multi-component of unsigned 8 bit integer
sitkVectorInt8 Multi-component of signed 8 bit integer
sitkVectorUInt16 Multi-component of unsigned 16 bit integer
sitkVectorInt16 Multi-component of signed 16 bit integer
sitkVectorUInt32 Multi-component of unsigned 32 bit integer
sitkVectorInt32 Multi-component of signed 32 bit integer
sitkVectorUInt64 Multi-component of unsigned 64 bit integer
sitkVectorInt64 Multi-component of signed 64 bit integer
sitkVectorFloat32 Multi-component of 32 bit float
sitkVectorFloat64 Multi-component of 64 bit float
sitkLabelUInt8 RLE label of unsigned 8 bit integers
sitkLabelUInt16 RLE label of unsigned 16 bit integers
sitkLabelUInt32 RLE label of unsigned 32 bit integers
sitkLabelUInt64 RLE label of unsigned 64 bit integers

There is also sitkUnknown, which is used for undefined or erroneous pixel ID's. It has a value of -1.

The 64-bit integer types are not available on all distributions. When not available the value is sitkUnknown.

More Information about the Image class be obtained in the Docstring

SimpleITK classes and functions have the Docstrings derived from the C++ definitions and the Doxygen documentation.

In [3]:
help(image)
Help on Image in module SimpleITK.SimpleITK object:

class Image(__builtin__.object)
 |  The main Image class for SimpleITK.
 |  
 |  C++ includes: sitkImage.h
 |  
 |  Methods defined here:
 |  
 |  CopyInformation(self, *args, **kwargs)
 |      CopyInformation(Image self, Image srcImage)
 |      
 |      Copy common meta-data from an image to this one.
 |      
 |      
 |      Copies the Origin, Spacing, and Direction from the source image to
 |      this image.
 |      
 |      It is required for the source Image's dimension and size to match, this image's attributes, otherwise an
 |      exception will be generated.
 |  
 |  GetDepth(self)
 |      GetDepth(Image self) -> unsigned int
 |  
 |  GetDimension(self)
 |      GetDimension(Image self) -> unsigned int
 |  
 |  GetDirection(self)
 |      GetDirection(Image self) -> VectorDouble
 |  
 |  GetHeight(self)
 |      GetHeight(Image self) -> unsigned int
 |  
 |  GetITKBase(self, *args)
 |      GetITKBase(Image self) -> itk::DataObject
 |      GetITKBase(Image self) -> itk::DataObject const *
 |  
 |  GetMetaData(self, *args, **kwargs)
 |      GetMetaData(Image self, std::string const & key) -> std::string
 |      
 |      Get the value of a meta-data dictionary entry as a string.
 |      
 |      
 |      If the key is not in the dictionary then an exception is thrown.
 |      
 |      string types in the dictionary are returned as their native strings.
 |      Other types are printed to string before returning.
 |  
 |  GetMetaDataKeys(self)
 |      GetMetaDataKeys(Image self) -> VectorString
 |      
 |      get a vector of keys in from the meta-data dictionary
 |      
 |      
 |      Returns a vector of keys to the key/value entries in the image's meta-
 |      data dictionary. Iterate through with these keys to get the values.
 |  
 |  GetNumberOfComponentsPerPixel(self)
 |      GetNumberOfComponentsPerPixel(Image self) -> unsigned int
 |      
 |      Get the number of components for each pixel.
 |      
 |      
 |      For scalar images this methods returns 1. For vector images the number
 |      of components for each pixel is returned.
 |  
 |  GetOrigin(self)
 |      GetOrigin(Image self) -> VectorDouble
 |  
 |  GetPixel(self, *idx)
 |      Returns the value of a pixel.
 |      
 |      This method takes 2 parameters in 2D: the x and y index,
 |      and 3 parameters in 3D: the x, y and z index.
 |  
 |  GetPixelAsComplexFloat64(self, *args, **kwargs)
 |      GetPixelAsComplexFloat64(Image self, VectorUInt32 idx) -> std::complex< double >
 |  
 |  GetPixelID(self)
 |      GetPixelID(Image self) -> itk::simple::PixelIDValueEnum
 |  
 |  GetPixelIDTypeAsString(self)
 |      GetPixelIDTypeAsString(Image self) -> std::string
 |  
 |  GetPixelIDValue(self)
 |      GetPixelIDValue(Image self) -> itk::simple::PixelIDValueType
 |  
 |  GetSize(self)
 |      GetSize(Image self) -> VectorUInt32
 |  
 |  GetSpacing(self)
 |      GetSpacing(Image self) -> VectorDouble
 |  
 |  GetWidth(self)
 |      GetWidth(Image self) -> unsigned int
 |  
 |  HasMetaDataKey(self, *args, **kwargs)
 |      HasMetaDataKey(Image self, std::string const & key) -> bool
 |      
 |      Query the meta-data dictionary for the existence of a key.
 |  
 |  SetDirection(self, *args, **kwargs)
 |      SetDirection(Image self, VectorDouble direction)
 |  
 |  SetOrigin(self, *args, **kwargs)
 |      SetOrigin(Image self, VectorDouble origin)
 |  
 |  SetPixel(self, *args)
 |      Sets the value of a pixel.
 |      
 |      This method takes 3 parameters in 2D: the x and y index then the value,
 |      and 4 parameters in 3D: the x, y and z index then the value.
 |  
 |  SetPixelAsComplexFloat64(self, *args, **kwargs)
 |      SetPixelAsComplexFloat64(Image self, VectorUInt32 idx, std::complex< double > const v)
 |  
 |  SetSpacing(self, *args, **kwargs)
 |      SetSpacing(Image self, VectorDouble spacing)
 |  
 |  TransformContinuousIndexToPhysicalPoint(self, *args, **kwargs)
 |      TransformContinuousIndexToPhysicalPoint(Image self, VectorDouble index) -> VectorDouble
 |      
 |      Transform continuous index to physical point
 |  
 |  TransformIndexToPhysicalPoint(self, *args, **kwargs)
 |      TransformIndexToPhysicalPoint(Image self, VectorInt64 index) -> VectorDouble
 |      
 |      Transform index to physical point
 |  
 |  TransformPhysicalPointToContinuousIndex(self, *args, **kwargs)
 |      TransformPhysicalPointToContinuousIndex(Image self, VectorDouble point) -> VectorDouble
 |      
 |      Transform physical point to continuous index
 |  
 |  TransformPhysicalPointToIndex(self, *args, **kwargs)
 |      TransformPhysicalPointToIndex(Image self, VectorDouble point) -> VectorInt64
 |      
 |      Transform physical point to index
 |  
 |  __GetPixelAsComplexFloat32__(self, *args, **kwargs)
 |      __GetPixelAsComplexFloat32__(Image self, VectorUInt32 idx) -> std::complex< float >
 |  
 |  __GetPixelAsDouble__(self, *args, **kwargs)
 |      __GetPixelAsDouble__(Image self, VectorUInt32 idx) -> double
 |  
 |  __GetPixelAsFloat__(self, *args, **kwargs)
 |      __GetPixelAsFloat__(Image self, VectorUInt32 idx) -> float
 |  
 |  __GetPixelAsInt16__(self, *args, **kwargs)
 |      __GetPixelAsInt16__(Image self, VectorUInt32 idx) -> int16_t
 |  
 |  __GetPixelAsInt32__(self, *args, **kwargs)
 |      __GetPixelAsInt32__(Image self, VectorUInt32 idx) -> int32_t
 |  
 |  __GetPixelAsInt64__(self, *args, **kwargs)
 |      __GetPixelAsInt64__(Image self, VectorUInt32 idx) -> int64_t
 |  
 |  __GetPixelAsInt8__(self, *args, **kwargs)
 |      __GetPixelAsInt8__(Image self, VectorUInt32 idx) -> int8_t
 |  
 |  __GetPixelAsUInt16__(self, *args, **kwargs)
 |      __GetPixelAsUInt16__(Image self, VectorUInt32 idx) -> uint16_t
 |  
 |  __GetPixelAsUInt32__(self, *args, **kwargs)
 |      __GetPixelAsUInt32__(Image self, VectorUInt32 idx) -> uint32_t
 |  
 |  __GetPixelAsUInt64__(self, *args, **kwargs)
 |      __GetPixelAsUInt64__(Image self, VectorUInt32 idx) -> uint64_t
 |  
 |  __GetPixelAsUInt8__(self, *args, **kwargs)
 |      __GetPixelAsUInt8__(Image self, VectorUInt32 idx) -> uint8_t
 |  
 |  __GetPixelAsVectorFloat32__(self, *args, **kwargs)
 |      __GetPixelAsVectorFloat32__(Image self, VectorUInt32 idx) -> VectorFloat
 |  
 |  __GetPixelAsVectorFloat64__(self, *args, **kwargs)
 |      __GetPixelAsVectorFloat64__(Image self, VectorUInt32 idx) -> VectorDouble
 |  
 |  __GetPixelAsVectorInt16__(self, *args, **kwargs)
 |      __GetPixelAsVectorInt16__(Image self, VectorUInt32 idx) -> VectorInt16
 |  
 |  __GetPixelAsVectorInt32__(self, *args, **kwargs)
 |      __GetPixelAsVectorInt32__(Image self, VectorUInt32 idx) -> VectorInt32
 |  
 |  __GetPixelAsVectorInt64__(self, *args, **kwargs)
 |      __GetPixelAsVectorInt64__(Image self, VectorUInt32 idx) -> VectorInt64
 |  
 |  __GetPixelAsVectorInt8__(self, *args, **kwargs)
 |      __GetPixelAsVectorInt8__(Image self, VectorUInt32 idx) -> VectorInt8
 |  
 |  __GetPixelAsVectorUInt16__(self, *args, **kwargs)
 |      __GetPixelAsVectorUInt16__(Image self, VectorUInt32 idx) -> VectorUInt16
 |  
 |  __GetPixelAsVectorUInt32__(self, *args, **kwargs)
 |      __GetPixelAsVectorUInt32__(Image self, VectorUInt32 idx) -> VectorUInt32
 |  
 |  __GetPixelAsVectorUInt64__(self, *args, **kwargs)
 |      __GetPixelAsVectorUInt64__(Image self, VectorUInt32 idx) -> VectorUInt64
 |  
 |  __GetPixelAsVectorUInt8__(self, *args, **kwargs)
 |      __GetPixelAsVectorUInt8__(Image self, VectorUInt32 idx) -> VectorUInt8
 |  
 |  __SetPixelAsComplexFloat32__(self, *args, **kwargs)
 |      __SetPixelAsComplexFloat32__(Image self, VectorUInt32 idx, std::complex< float > const v)
 |  
 |  __SetPixelAsDouble__(self, *args, **kwargs)
 |      __SetPixelAsDouble__(Image self, VectorUInt32 idx, double v)
 |  
 |  __SetPixelAsFloat__(self, *args, **kwargs)
 |      __SetPixelAsFloat__(Image self, VectorUInt32 idx, float v)
 |  
 |  __SetPixelAsInt16__(self, *args, **kwargs)
 |      __SetPixelAsInt16__(Image self, VectorUInt32 idx, int16_t v)
 |  
 |  __SetPixelAsInt32__(self, *args, **kwargs)
 |      __SetPixelAsInt32__(Image self, VectorUInt32 idx, int32_t v)
 |  
 |  __SetPixelAsInt64__(self, *args, **kwargs)
 |      __SetPixelAsInt64__(Image self, VectorUInt32 idx, int64_t v)
 |  
 |  __SetPixelAsInt8__(self, *args, **kwargs)
 |      __SetPixelAsInt8__(Image self, VectorUInt32 idx, int8_t v)
 |  
 |  __SetPixelAsUInt16__(self, *args, **kwargs)
 |      __SetPixelAsUInt16__(Image self, VectorUInt32 idx, uint16_t v)
 |  
 |  __SetPixelAsUInt32__(self, *args, **kwargs)
 |      __SetPixelAsUInt32__(Image self, VectorUInt32 idx, uint32_t v)
 |  
 |  __SetPixelAsUInt64__(self, *args, **kwargs)
 |      __SetPixelAsUInt64__(Image self, VectorUInt32 idx, uint64_t v)
 |  
 |  __SetPixelAsUInt8__(self, *args, **kwargs)
 |      __SetPixelAsUInt8__(Image self, VectorUInt32 idx, uint8_t v)
 |  
 |  __SetPixelAsVectorFloat32__(self, *args, **kwargs)
 |      __SetPixelAsVectorFloat32__(Image self, VectorUInt32 idx, VectorFloat v)
 |  
 |  __SetPixelAsVectorFloat64__(self, *args, **kwargs)
 |      __SetPixelAsVectorFloat64__(Image self, VectorUInt32 idx, VectorDouble v)
 |  
 |  __SetPixelAsVectorInt16__(self, *args, **kwargs)
 |      __SetPixelAsVectorInt16__(Image self, VectorUInt32 idx, VectorInt16 v)
 |  
 |  __SetPixelAsVectorInt32__(self, *args, **kwargs)
 |      __SetPixelAsVectorInt32__(Image self, VectorUInt32 idx, VectorInt32 v)
 |  
 |  __SetPixelAsVectorInt64__(self, *args, **kwargs)
 |      __SetPixelAsVectorInt64__(Image self, VectorUInt32 idx, VectorInt64 v)
 |  
 |  __SetPixelAsVectorInt8__(self, *args, **kwargs)
 |      __SetPixelAsVectorInt8__(Image self, VectorUInt32 idx, VectorInt8 v)
 |  
 |  __SetPixelAsVectorUInt16__(self, *args, **kwargs)
 |      __SetPixelAsVectorUInt16__(Image self, VectorUInt32 idx, VectorUInt16 v)
 |  
 |  __SetPixelAsVectorUInt32__(self, *args, **kwargs)
 |      __SetPixelAsVectorUInt32__(Image self, VectorUInt32 idx, VectorUInt32 v)
 |  
 |  __SetPixelAsVectorUInt64__(self, *args, **kwargs)
 |      __SetPixelAsVectorUInt64__(Image self, VectorUInt32 idx, VectorUInt64 v)
 |  
 |  __SetPixelAsVectorUInt8__(self, *args, **kwargs)
 |      __SetPixelAsVectorUInt8__(Image self, VectorUInt32 idx, VectorUInt8 v)
 |  
 |  __abs__(self)
 |  
 |  __add__(self, other)
 |  
 |  __and__(self, other)
 |  
 |  __del__ lambda self
 |  
 |  __div__(self, other)
 |  
 |  __eq__(self, other)
 |  
 |  __floordiv__(self, other)
 |  
 |  __ge__(self, other)
 |  
 |  __getattr__ lambda self, name
 |  
 |  __getitem__(self, idx)
 |      Get an pixel value or a sliced image.
 |      
 |      This operator implements basic indexing where idx is
 |      arguments or a squence of integers the same dimension as
 |      the image. The result will be a pixel value from that
 |      index.
 |      
 |      Multi-dimension extended slice based indexing is also
 |      implemented. The return is a copy of a new image. The
 |      standard sliced based indices are supported including
 |      negative indices, to indicate location relative to the
 |      end, along with negative step sized to indicate reversing
 |      of direction.
 |      
 |      If the length of idx is less than the number of dimension
 |      of the image it will be padded with the defaults slice
 |      ":".
 |      
 |      A 2D image can be extracted from a 3D image by providing
 |      one argument being an integer instead of a slice.
 |  
 |  __gt__(self, other)
 |  
 |  __iadd__(self, other)
 |  
 |  __init__(self, *args)
 |      __init__(itk::simple::Image self) -> Image
 |      __init__(itk::simple::Image self, Image img) -> Image
 |      __init__(itk::simple::Image self, unsigned int width, unsigned int height, itk::simple::PixelIDValueEnum valueEnum) -> Image
 |      __init__(itk::simple::Image self, unsigned int width, unsigned int height, unsigned int depth, itk::simple::PixelIDValueEnum valueEnum) -> Image
 |      __init__(itk::simple::Image self, VectorUInt32 size, itk::simple::PixelIDValueEnum valueEnum, unsigned int numberOfComponents=0) -> Image
 |  
 |  __invert__(self)
 |  
 |  __iter__(self)
 |  
 |  __le__(self, other)
 |  
 |  __len__(self)
 |  
 |  __lt__(self, other)
 |  
 |  __mod__(self, other)
 |  
 |  __mul__(self, other)
 |  
 |  __ne__(self, other)
 |  
 |  __neg__(self)
 |  
 |  __or__(self, other)
 |  
 |  __pos__(self)
 |  
 |  __pow__(self, other)
 |  
 |  __radd__(self, other)
 |  
 |  __rand__(self, other)
 |  
 |  __rdiv__(self, other)
 |  
 |  __repr__ = _swig_repr(self)
 |  
 |  __rfloordiv__(self, other)
 |  
 |  __rmul__(self, other)
 |  
 |  __ror__(self, other)
 |  
 |  __rpow__(self, other)
 |  
 |  __rsub__(self, other)
 |  
 |  __rtruediv__(self, other)
 |  
 |  __rxor__(self, other)
 |  
 |  __setattr__ lambda self, name, value
 |  
 |  __setitem__(self, idx, value)
 |      Sets the pixel value at index idx to value.
 |      
 |      The dimension of idx should match that of the image.
 |  
 |  __str__(self)
 |      __str__(Image self) -> std::string
 |  
 |  __sub__(self, other)
 |  
 |  __truediv__(self, other)
 |  
 |  __xor__(self, other)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __swig_destroy__ = <built-in function delete_Image>
 |      delete_Image(Image self)
 |  
 |  __swig_getmethods__ = {}
 |  
 |  __swig_setmethods__ = {}


Accessing Attributes

If you are familliar with ITK, then these methods will follow your expectations:

In [4]:
print image.GetSize()
print image.GetOrigin()
print image.GetSpacing()
print image.GetDirection()
print image.GetNumberOfComponentsPerPixel()
(256, 128, 64)
(0.0, 0.0, 0.0)
(1.0, 1.0, 1.0)
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
1

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 index will be set to 0, and the origin adjusted accordingly.

The size of the image's dimensions have explicit accessors:

In [5]:
print image.GetWidth()
print image.GetHeight()
print image.GetDepth()
256
128
64

Since the dimension and pixel type of a SimpleITK image is determined at run-time accessors are needed.

In [6]:
print image.GetDimension()
print image.GetPixelIDValue()
print image.GetPixelIDTypeAsString()
3
2
16-bit signed integer

What is the depth of a 2D image?

In [7]:
print image_2D.GetSize()
print image_2D.GetDepth()
(32, 32)
0

What is the dimension and size of a Vector image?

In [8]:
print image_RGB.GetDimension()
print image_RGB.GetSize()
2
(128, 128)

In [9]:
print image_RGB.GetNumberOfComponentsPerPixel()
3

For certain file types such as DICOM, additional information about the image is contained in the meta-data dicitonary.

In [10]:
for key in image.GetMetaDataKeys():
        print "\"{0}\":\"{1}\"".format(key, image.GetMetaData(key))

Accessing Pixels

There are the member functions GetPixel and SetPixel which provides an ITK-like interface for pixel access.

In [11]:
help(image.GetPixel)
Help on method GetPixel in module SimpleITK.SimpleITK:

GetPixel(self, *idx) method of SimpleITK.SimpleITK.Image instance
    Returns the value of a pixel.
    
    This method takes 2 parameters in 2D: the x and y index,
    and 3 parameters in 3D: the x, y and z index.


In [12]:
print image.GetPixel(0, 0, 0)
image.SetPixel(0, 0, 0, 1)
print image.GetPixel(0, 0, 0)
0
1

In [13]:
print image[0,0,0]
image[0,0,0] = 10
print image[0,0,0]
1
10

Conversion between numpy and SimpleITK

In [14]:
nda = sitk.GetArrayFromImage(image)
print nda
[[[10  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]

 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]

 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]

 ..., 
 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]

 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]

 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]]]

In [15]:
help(sitk.GetArrayFromImage)
Help on function GetArrayFromImage in module SimpleITK.SimpleITK:

GetArrayFromImage(image)
    Get a numpy array from a SimpleITK Image.


In [16]:
nda = sitk.GetArrayFromImage(image_RGB)
img = sitk.GetImageFromArray(nda)
img.GetSize()
Out[16]:
(3, 128, 128)
In [17]:
help(sitk.GetImageFromArray)
Help on function GetImageFromArray in module SimpleITK.SimpleITK:

GetImageFromArray(arr, isVector=False)
    Get a SimpleITK Image from a numpy array. If isVector is True, then a 3D array will be treaded as a 2D vector image, otherwise it will be treaded as a 3D image


In [18]:
img = sitk.GetImageFromArray(nda, isVector=True)
print img
VectorImage (0x7f8b68d90880)
  RTTI typeinfo:   itk::VectorImage<unsigned char, 2u>
  Reference Count: 1
  Modified Time: 497
  Debug: Off
  Object Name: 
  Observers: 
    none
  Source: (none)
  Source output name: (none)
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 0
  UpdateMTime: 0
  RealTimeStamp: 0 seconds 
  LargestPossibleRegion: 
    Dimension: 2
    Index: [0, 0]
    Size: [128, 128]
  BufferedRegion: 
    Dimension: 2
    Index: [0, 0]
    Size: [128, 128]
  RequestedRegion: 
    Dimension: 2
    Index: [0, 0]
    Size: [128, 128]
  Spacing: [1, 1]
  Origin: [0, 0]
  Direction: 
1 0
0 1

  IndexToPointMatrix: 
1 0
0 1

  PointToIndexMatrix: 
1 0
0 1

  Inverse Direction: 
1 0
0 1

  VectorLength: 3
  PixelContainer: 
    ImportImageContainer (0x7f8b68d90a70)
      RTTI typeinfo:   itk::ImportImageContainer<unsigned long, unsigned char>
      Reference Count: 1
      Modified Time: 498
      Debug: Off
      Object Name: 
      Observers: 
        none
      Pointer: 0x7f8b69969c00
      Container manages memory: true
      Size: 49152
      Capacity: 49152


The order of index and dimensions need careful attention during conversion

ITK's Image class does not have a bracket operator. It has a GetPixel which takes an ITK Index object as an argument, which is an array ordered as (x,y,z). This is the convention that SimpleITK's Image class uses for the GetPixel method as well.

While in numpy, an array is indexed in the opposite order (z,y,x).

In [19]:
print img.GetSize()
print nda.shape
print nda.shape[::-1]
(128, 128)
(128, 128, 3)
(3, 128, 128)

Are we still dealing with Image, because I haven't seen one yet...

While SimpleITK does not do visualization, it does contain a built in Show method. This function writes the image out to disk and than launches a program for visualization. By default it is configured to use ImageJ, because it is readily supports all the image types which SimpleITK has and load very quickly. However, it's easily customizable by setting enviroment variables.

In [20]:
sitk.Show(image)
In [21]:
sitk.Show?

By converting into a numpy array, matplotlob can be used for visualization for integration into the scientifc python enviroment.

In [22]:
import pylab
z = 0
slice = sitk.GetArrayFromImage(image)[z,:,:]
imshow(slice)
Out[22]:
<matplotlib.image.AxesImage at 0x110d3c290>