ITK/Scripting

From KitwarePublic
Jump to navigationJump to search

ITK itself is written in C++, however a tool called CableSwig can be used to generate bindings for other languages. This allows you to use the same runtime (running in optimised C++ code) but develop in a scripting language (and take advantage of all the attendant benefits).

The following languages are supported via the bindings:

In order to build the bindings, you will need to:

  1. Build and install CableSwig (more info)
  2. Ensure you have installed both the runtime and the development versions of your chosen language
  3. Build ITK using the appropriate settings for your platform, and enable the CABLE_* option for your language
  4. Install ITK as per normal
  5. Test!

Tcl Bindings

Tcl (Tool Command Language) is a very simple language for writing scripts.

Python Bindings

Python is a very elegant and powerful language.

Here is some example Python code for ITK (taken from `Insight/Examples/Filtering`):

#!/usr/bin/env python
 
# BinaryThresholdImageFilter.py
 
from InsightToolkit import *
 
from sys import argv
 
reader = itkImageFileReaderUS2_New()
writer = itkImageFileWriterUS2_New()
 
filter  = itkBinaryThresholdImageFilterUS2US2_New()
 
filter.SetInput( reader.GetOutput() )
writer.SetInput( filter.GetOutput() )
 
reader.SetFileName( argv[1] )
writer.SetFileName( argv[2] )
 
filter.SetLowerThreshold( eval( argv[3] )  )
filter.SetUpperThreshold( eval( argv[4] )  )
filter.SetOutsideValue(   eval( argv[5] )  )
filter.SetInsideValue(    eval( argv[6] )  )
 
writer.Update()

Java Bindings

Java is a modern object-oriented language.

How to wrap additional classes

Given that ITK is based on Generic Programming, most of the C++ classes are implemented as templates. Therefore, their final type is not defined until the actual template is instantiated with specific template arguments. This generality makes difficult to automatically wrap ITK classes for scripting languages such as Tcl and Python. The solution adopted by ITK is to use the powerful Cable-Swig platform and to select specific instantiation for the most commonly used types. In practice it is impossible to wrap all ITK classes, simply because the combinations of types will result in an very large number of classes.

It is then likely that you may find that your favorite class is not being wrapped...

What to do in that case ?

You have two independent lines of action:

  1. Write an email to the users-list and request the developers to wrap the class that you need.
  2. Wrap the class yourself by following the instructions in the remaining part of this section.


Since we have learned that programmers are great at learning-by-example, the following section will teach you how to wrap a new class by walking step-by-step through the process of wrapping a new class. The subject of our example is the itkOtsuMultipleThresholdCalculator, simply because at the time of writing this text there was a request pending for adding this class to the wrapping, so we thought that by writing these instructions we will solve two problems simultaneously :-)

Finding the directory

The first step in writing the wrapping for a class is to find the directory where such class has been cataloged. In the particular case of our subject, the itkOtsuMultipleThresholdCalculator, it is located in the directory

   Insight/Code/Algorithms

Therefore we know that its wrapping files will have to be placed under

   Insight/Wrapping/CSwig/Algorithms

As you probably noticed already, all the files defining the wrapping process are located in the directory

   Insight/Wrapping/CSwig

What files to modify

Now that we found the directory, we have to keep in mind that three files will have to be modified in order to add wrapping for our subject class. These files are

  1. CMakeLists.txt
  2. wrap_ITKAlgorithms.cxx
  3. the new file that defines the instantiations for our class

Let's start with the CMakeLists.txt file since it will be the simplest change to make. When you open any of the CMakeLists.txt files in the Wrapping/CSwig subdirectories you will find that the initial section contains a list of source files to wrap. Something like


  SET(WRAP_SOURCES
      wrap_itkCurvatureFlowImageFilter
      wrap_itkDemonsRegistrationFilter
      wrap_itkHistogramMatchingImageFilter
      wrap_itkImageRegistrationMethod
      ...
      )

The only thing that you have to change in this file is to add your class to this list. Note that you must add the prefix "wrap_itk" to the name of your class. In our case, for wrapping the itkOtsuMultipleThresholdCalculator we will add it as


 SET(WRAP_SOURCES
     wrap_itkCurvatureFlowImageFilter
     wrap_itkDemonsRegistrationFilter
     wrap_itkOtsuMultipleThresholdCalculator
     wrap_itkHistogramMatchingImageFilter
     wrap_itkImageRegistrationMethod
     ...
     )


Let's now move into the wrap_ITKAlgorithms.cxx file. If you open this file, you will find code like the following.


  #ifdef CABLE_CONFIGURATION
  #include "itkCSwigMacros.h"
  namespace _cable_
  {
    const char* const package = ITK_WRAP_PACKAGE_NAME(ITK_WRAP_PACKAGE);
    const char* const groups[] =
      {
      ITK_WRAP_GROUP(itkCurvatureFlowImageFilter),
      ITK_WRAP_GROUP(itkDemonsRegistrationFilter),
      ITK_WRAP_GROUP(itkHistogramMatchingImageFilter),
      ITK_WRAP_GROUP(itkImageRegistrationMethod),
      ITK_WRAP_GROUP(itkImageToImageMetric),
      ITK_WRAP_GROUP(itkMeanSquaresImageToImageMetric),
      ...


As you probably already guessed, the only thing that you have to do in this file is to add a line for your class to be wrapped, in our case we add the line

      ITK_WRAP_GROUP(itkOutsuMultipleThresholdCalculator),

and the code will look like


  #ifdef CABLE_CONFIGURATION
  #include "itkCSwigMacros.h"
  namespace _cable_
  {
    const char* const package = ITK_WRAP_PACKAGE_NAME(ITK_WRAP_PACKAGE);
    const char* const groups[] =
      {
      ITK_WRAP_GROUP(itkCurvatureFlowImageFilter),
      ITK_WRAP_GROUP(itkDemonsRegistrationFilter),
      ITK_WRAP_GROUP(itkHistogramMatchingImageFilter),
      ITK_WRAP_GROUP(itkOutsuMultipleThresholdCalculator),
      ITK_WRAP_GROUP(itkImageRegistrationMethod),
      ITK_WRAP_GROUP(itkImageToImageMetric),
      ITK_WRAP_GROUP(itkMeanSquaresImageToImageMetric),
      ...


At this point we are done with the easy part  :-)

Let's now take a look at the new file that we must write in order to specify the instantiations that we want to create for our class, the beloved itkOutsuMultipleThresholdCalculator. First of all, the name of the file must match what we already wrote in the CMakeLists.txt file, that is the file must be named wrap_itkOutsuMultipleThresholdCalculator.cxx. The skeleton of this file should contain the following code

   #include "itkOtsuThresholdImageCalculator.h"
   #include "itkImage.h"
   #ifdef CABLE_CONFIGURATION
   #include "itkCSwigMacros.h"
   #include "itkCSwigImages.h"
   namespace _cable_
   {
     const char* const group = ITK_WRAP_GROUP(itkOtsuThresholdImageCalculator);
     namespace wrappers
     {
     ... Instantitation go here ...
     }
   }
   #endif

Note the presence in the first line of the header file of our class to be wrapped. The second line includes the header of the itkImage because we anticipate that the Otsu calculator will be templated over the image type. This also motivates the includsion of the fifth line

   #include "itkCSwigImages.h"

that contains a large set of pre-instantiated image types.

Line #8 defines the name of the GROUP. The group is a set of multiple instantiations of the same class. Groups help to indicate to the wrappers what code must be put in the same compilation unit (.obj or .o file). By convention, the name of the group should always match the name of the class to be wrapped.

Now we are down to the fun part !!

Defining the instantiations for this class


Specifying the instantiations

Once we have the skeleton of the file as it is described in the previous section, we can proceed to specify the instantiation that we want for our class. For this purpose, ITK has defined a large set of macros that facilitate the writing of the instantiation. The Macro to use depend on the number of template parameters of the class to be wrapped. In the case of the itkOutsuMultipleThresholdCalculator, this class has a only one template parameter, that is the ImageType. Therefore we need the Macro that wrap a class with a single template parameter, this is the ITK_WRAP_OBJECT1() macro. Where the number "1" states that this macro is intended for a class with a single template parameter. The macro itself expects the following three parameters

  1. Name of the class to be wrapped (without the itk prefix)
  2. Type to be used for its template parameter
  3. Name that you want to be used at the level of the scripting language

A typical instantiation for our calculator will look like

   ITK_WRAP_OBJECT1(OtsuThresholdImageCalculator, image::F2,itkOtsuThresholdImageCalculatorF2);

The first argument OtsuThresholdImageCalculator is the name of our class. The second argument "image::F2" is a name predefined for an Image fo pixel type float and dimension 2. This symbol has been defined in the file "itkCSwigImages.h", you may want to look at that file for other image types already defined. The last argument of the macro is the class name that you will use at the level of your scripting language once the class has been wrapped. In principle you could use any name you want, but calling your class 'ElvisPrestley' may be confusing for your scripts  :-). Therefore we defined a "convention" in ITK in order to have a consisten naming scheme. This convention specifies that the wrapped class is composed by the C++ classname and we add letters and numbers representing the fundamental types and dimensions for which this class has been wrapped. In the particular case of the instantiation above, we are saying that the Otsu calculator has been wrapped for images of pixel type float (that is the reason for the "F"), and dimension == 2. This, of course, matches the image type that we used for the second argument of the macro: image::F2.