Reading and Writing for Images and Transforms

Images

There are numerous file formats support by SimpleITK’s image readers and writers. Support for a particular format is handled by a specific ITK ImageIO class. By default, the ImageIO is automatically determined for a particular file based on the file name suffix and/or the contents of the file’s header. Advanced SimpleITK installations can configure or extend which file formats are supported by SimpleITK. A list of registered ImageIO’s can be found using the GetRegisteredImageIOs() method, but is posted here:

A read and write example using SimpleITK’s ImageFileReader and ImageFileWriter classes:

  ImageFileReader reader = new ImageFileReader();
  reader.SetImageIO("PNGImageIO");
  reader.SetFileName(inputImageFileName);
  Image image = reader.Execute();

  ImageFileWriter writer = new ImageFileWriter();
  writer.SetFileName(outputImageFileName);
  writer.Execute(image);

The above example specifies using the PNGImageIO to read the file. If that line is omitted, SimpleITK would determine which IO to use automatically, based on the file name’s suffix and/or the file’s header.

A more compact example using SimpleITK’s procedural interface:

  Image image = sitk.ReadImage(inputImageFileName, PixelIDValueEnum.sitkUnknown, "PNGImageIO");
  sitk.WriteImage(image, outputImageFileName);

Similarly, if the imageIO parameter is omitted, SimpleITK will determine which IO to use automatically.

Finally, one can also specify the desired pixel type for the returned image, regardless of the underlying pixel type. This is done via the SetOutputPixelType of the ImageFileReader or setting the ReadImage’s outputPixelType parameter.

When reading a scalar image and specifying a different scalar type from the underlying one, the returned image’s pixel type will be a simple casting of the actual type. This casting can result in loss of data when performing a narrowing conversion. For example, if the underlying pixel type is sitk.sitkFloat32 and the specified pixel type is sitk.sitkUInt8. Explicitly specifying a pixel type is commonly done in conjunction with the SimpleITK registration framework. This framework only accepts images with float pixel types. Therefore, it is common to read the images and specify their pixel type as sitk.sitkFloat32 or sitk.sitkFloat64.

When reading a vector image and specifying the pixel type as a scalar, the returned pixel values are computed based on the number of channels in the original image. This conversion makes several assumptions, three channels mean an RGB image, four channels an RGBA image. These assumptions are useful, but may not be what you need if the original image was not in these color-spaces (e.g. HSV).

For those looking for further details, the ITK class which deals with these conversions is ConvertPixelBuffer.

Transformations

In SimpleITK, transformation files can be written in several different formats. Just as there are numerous IOs for images, there are several for transforms, including TxtTransformIO, MINCTransformIO, HDF5TransformIO, and MatlabTransformIO (although this list can be extended as well). These support a variety of file formats, including .txt, .tfm, .xfm, .hdf and .mat.

Because of the size of displacement fields, writing them may require more careful attention. To save a displacement field we recommend using one of the binary transformation file formats (e.g. .hdf, .mat). Saving it in a text based format results in significantly larger files and longer IO runtimes. Another option is to save the displacement field found in a DisplacementFieldTransform object as an image (.nrrd, .nhdr, .mha, .mhd, .nii, .nii.gz).

Here is a simple example of creating a transformation, writing it to a file, reading it back, and then comparing the results.

  Euler2DTransform basic_transform = new Euler2DTransform();
  VectorDouble trans = new VectorDouble( new double[] {2.0, 3.0} );
  basic_transform.SetTranslation(trans);

  sitk.WriteTransform(basic_transform, "euler2D.tfm");

  Transform read_result = sitk.ReadTransform("euler2D.tfm");

  Debug.Assert( basic_transform.GetName() != read_result.GetName() );

In all languages, except Python, read_result returns an object of the generic sitk.Transform() class and basic_transform creates a sitk.Euler2DTransform() object, but both represent the same transformation. Although this example only uses a single SimpleITK transformation, a .tfm file can hold a composite (set of transformations).