Personal tools
You are here: Home / cmgui / Wiki / Writing programs that use ITK
Navigation
Log in


Forgot your password?
 

Writing programs that use ITK

Introduction

ITK is often used as a library for software such as Cmgui and Osirix. It is also possible to write stand alone programs that use ITK. This can be useful if you want to test a feature and get a feel for how it works before using it in a larger software project.

For example, there are many small stand alone programs written to test the various different filters. You can write similar programs fairly easy.

Writing a stand alone application

First make sure you have downloaded the itk source and library files. If in the institute you can use the versions installed on /hpc/cmiss or copy these files to your local machine. Once you have the source and library files you can create a stand alone program as follows:

  1. Create a directory for your program

  2. Create a CMakeLists.txt file

  3. Create a .cxx source file, eg HelloWorld.cxx

  4. Use ccmake to configure for make for the target directory that contains your code. If you are actually in the target directory, just use the character ".", otherwise you will need to specify the directory name. I recommend changing into the direcory containing your source as it makes it simpler. If you have /hpc/cmiss mounted you can start ccmake by running:

    ccmake . -DITK_DIR:PATH=/hpc/cmiss/itk/lib/i686-linux
    
    Then press c to configure (You may need to do this twice the first time).
    Next press g to generate files and exit the ccmake program.
    
  5. Build your program:

    make
    
  6. Execute your program:

    ./ProgramName
    

Hello world example

To begin with I suggest copying over the example CMakeLists.txt and HelloWorld.cxx files from the following location:

/hpc/cmiss/itk/src/Examples/Installation

If you have placed these files in an appropriate target directory you should now be able to compile a Hello world executable by following steps 4 through 6 from above. It is worth having a look at the CMakeLists.txt file and HelloWorld.cxx file to see the basic syntax for a small project. Of particular interest is the code just after the project name in the CMakeLists.txt file

# Find ITK.
FIND_PACKAGE(ITK)
IF(ITK_FOUND)
  INCLUDE(${ITK_USE_FILE})
ELSE(ITK_FOUND)
  MESSAGE(FATAL_ERROR
          "Cannot build without ITK.  Please set ITK_DIR.")
ENDIF(ITK_FOUND)

This tells the program to find the ITK directory. If you copy a CmakeLists.txt file from one of the many examples in the ITK src to an external directory you will need to add this code to your CMakeLists.txt file.

Other resources

See the link http://www.itk.org/HTML/Tutorials.htm The first tutorial explains how to set up a simple project (similar to what is described above but with a bit more detail).