From KitwarePublic
Revision as of 11:10, 19 July 2004 by Gavinb (talk | contribs) (Created from antonym)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Hello Insight

Traditionally, the very first program you learn to write in any given language is the simplest code to print "Hello World!". Following in a similar vein, we hereby present the simplest possible ITK program (which also happens to print a boring message). While it doesn't demonstrate anything very sophisticated, it can be used to demonstrate that you can build programs against your freshly installed ITK, and that you can run them. If this seemingly trivial step succeeds, it means:

  1. ITK was built successfully (the build itself succeeded)
  2. ITK development files were installed successfully (and your compiler can find all the right headers and libraries)
  3. The ITK runtime is good (since your OS can find the required shared libraries and so on)

All this working is a good thing.

So without further ado, fire up your favourite text editor and get started!

Hello Insight - Really

Create a new directory. Be imaginative and call it `HelloInsight`. Create a file called `HelloInsight.cxx` therein, and enter the following code:

#include <itkImage.h>
#include <iostream>
typedef itk::Image< float, 3 > ImageType;
int main( int argc, char* argv[] )
    ImageType::Pointer img = ImageType::New();
    std::cout << "Hello, Insight - I have an image!\n" << std::endl;
    std::cout << img << std::endl;
    return 0;

This program first includes the declarations for ITK images, and then the standard C++ streams library. Since everything in the ITK world is templated, we declare what type of image we want; in this case, a 3-dimensional image storing float scalars. Next, in our `main()` function we declare a smart pointer and a new instance of an image, called `img`. We display a simple message, then we print out the image object itself. That's about as good as it gets, at least for now.

Hello Build script

But wait, you're not done yet! You need to build your exciting little program. And you really don't want to bother trying to hand-craft a Makefile, get all the paths right, figure out dependencies and all that sort of thing. Use ["CMake"] instead - trust us, it is much easier.

So create a file called `CMakeLists.txt` and add the following:

   MESSAGE(FATAL_ERROR "Cannot build without ITK.  Please set ITK_DIR.")
ADD_EXECUTABLE(HelloInsight HelloInsight.cxx)

The first line declares the name of the project (which is arbitrary really). Next we have a block that ensures the ITK libraries can be found. The `ADD_EXECUTABLE` command declares that we want to build a program called `HelloInsight` and it is built by compiling `HelloInsight.cxx`. Next, we use `TARGET_LINK_LIBRARIES` to declare that the `HelloInsight` target needs to be linked with the `ITKCommon` library.

Configuring & Building

Now we need to build our little test program, but first we have to configure it with CMake. Basically this involves telling CMake to go out and find everthing it needs (compilers, libraries and so on) to build the program, and then generate a `Makefile` for us. You should always do an OutOfSourceBuild whenever possible, so first create a directory called `Build`, go into it and run CMake, pointing to the source directory (where `CMakeLists.txt` can be found):

% mkdir Build
% cd Build
% cmake ..

This should crunch for a few seconds and eventually tell you it when it has finished generating. Now, you can simply run `make` to build the program. Once this finishes, you should be able to run it and see some output mostly like the following:

% ./HelloInsight 
Hello, Insight - I have an image!
Image (0x8058340)
  RTTI typeinfo:   N3itk5ImageIfLj3EEE
  Reference Count: 2
  Modified Time: 1
  Debug: Off
  Source: (none)
  Source output index:  0
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 0
  UpdateMTime: 0
    Dimension: 3
    Index: [0, 0, 0]
    Size: [0, 0, 0]
    Dimension: 3
    Index: [0, 0, 0]
    Size: [0, 0, 0]
    Dimension: 3
    Index: [0, 0, 0]
    Size: [0, 0, 0]
  Spacing: [1, 1, 1]
  Origin: [0, 0, 0]
    ImportImageContainer (0x8058420)
      RTTI typeinfo:   N3itk20ImportImageContainerImfEE
      Reference Count: 1
      Modified Time: 2
      Debug: Off
      Pointer: 0x8058450
      Container manages memory: true
      Size: 0
      Capacity: 0

That's It!

That's it? Yes, you now have a working install of ITK, and you can successfully build and run programs against it. Now go have a look at the ITK Tutorials and ITK InsightApplications.