This example illustrates the basic image viewing capabilities provided by SimpleITK.
The focus of SimpleITK is on image analysis and not display. We therefor use an ad-hoc image viewing approach, relying on an external program. By default this is the Fiji/ImageJ program.
When using this functionality, SimpleITK writes the image to disc in a temporary location and then launches the viewer. Potential issues with this approach:
The external viewer program is not found. Either it is not installed, or not installed in the expected location, for details see Why isn’t Fiji or ImageJ found by the Show function (RuntimeError: Exception thrown…)?)
Writing the image to disc fails when there isn’t enough disc space.
The external program cannot read the image file because it was saved in a format not supported by the program.
To control viewing you have two options:
Procedural approach. Use the Show function, with control primarily done via environment variable settings.
Object oriented approach. Use the ImageViewer class, with control via the object’s interface.
One can either set the viewing application, if you just want to change the specific viewer, or set the viewing command. The latter allows you to specify viewer specific arguments such as -z for ITK-SNAP, see below, to open in a zoomed view.
import sys import SimpleITK as sitk grid_image = sitk.GridSource( outputPixelType=sitk.sitkUInt16, size=(512, 512), sigma=(0.1, 0.1), gridSpacing=(20.0, 20.0), ) # Procedural interface, using the default image viewer (Fiji/ImageJ) or # any viewer specified by the SITK_SHOW_COMMAND environment variable. sitk.Show(grid_image, title="grid using Show function", debugOn=True) # Object oriented interface: image_viewer = sitk.ImageViewer() image_viewer.SetTitle("grid using ImageViewer class") # Use the default image viewer. image_viewer.Execute(grid_image) # Change viewer, and display again. image_viewer.SetApplication( "/Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP" ) image_viewer.Execute(grid_image) # Change the viewer command, (use ITK-SNAP's -z option to open the image in # zoomed mode) image_viewer.SetCommand( "/Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP -z 2" ) image_viewer.Execute(grid_image) sys.exit(0)
# Run with: # # Rscript --vanilla ImageViewing.R # library(SimpleITK) grid_image <- GridSource(outputPixelType="sitkUInt16", size=c(512,512), sigma=c(0.1,0.1), gridSpacing=c(20.0,20.0)) # Procedural interface, using the default image viewer (Fiji/ImageJ) or # any viewer specified by the SITK_SHOW_COMMAND environment variable. Show(grid_image, title = "grid using Show function", debugOn = TRUE) # Object oriented interface: image_viewer <- ImageViewer() image_viewer$SetTitle('grid using ImageViewer class') # Use the default image viewer. image_viewer$Execute(grid_image) # Change viewer, and display again. image_viewer$SetApplication('/Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP') image_viewer$Execute(grid_image) # Change the viewer command, (use ITK-SNAP's -z option to open the image in zoomed mode) image_viewer$SetCommand('/Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP -z 2') image_viewer$Execute(grid_image)