ITK/HelloInsight: Difference between revisions

From KitwarePublic
< ITK
Jump to navigationJump to search
(Created from antonym)
 
(→‎Write the source code: Update minimum CMake version)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Hello Insight =
= 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:
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 demonstrate here a simple ITK program.  While simple, it is 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:


# ITK was built successfully (the build itself succeeded)
# ITK was built successfully (the build itself succeeded)
Line 9: Line 9:
All this working is a good thing.
All this working is a good thing.


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


== Hello Insight - Really ==
== Write the source code ==


Create a new directory.  Be imaginative and call it `HelloInsight`.  Create a file called `HelloInsight.cxx` therein, and enter the following code:
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>
<pre>
&nbsp;
#include "itkImage.h"
#include <iostream>
#include <iostream>
&nbsp;
typedef itk::Image< float, 3 > ImageType;
&nbsp;
int main( int argc, char* argv[] )
{
    ImageType::Pointer img = ImageType::New();
&nbsp;
    std::cout << "Hello, Insight - I have an image!\n" << std::endl;
&nbsp;
    std::cout << img << std::endl;
&nbsp;
    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.
int main()
{
  typedef itk::Image< unsigned short, 3 > ImageType;


== Hello Build script ==
  ImageType::Pointer image = ImageType::New();


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.
  std::cout << "Hello ITK World !" << std::endl;


So create a file called `CMakeLists.txt` and add the following:
  return 0;
}
</pre>


PROJECT(HelloInsight)
Next to ''HelloInsight.cxx'', create a file, ''CMakeLists.txt'', and populate it with:
&nbsp;
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)
&nbsp;
ADD_EXECUTABLE(HelloInsight HelloInsight.cxx)
&nbsp;
TARGET_LINK_LIBRARIES(HelloInsight ITKCommon)


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.
<pre>
cmake_minimum_required(VERSION 3.10.2)


== Configuring & Building ==
project(HelloInsight)


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):
# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})


% mkdir Build
add_executable(HelloInsight HelloInsight.cxx )
% 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:
target_link_libraries(HelloInsight ${ITK_LIBRARIES})
</pre>


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


== That's It! ==
Next to the ''HelloInsight'' directory, create a directory to build the project, ''HelloInsight_build''.  '''cd''' into that directory:


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]].
  cd HelloInsight_build
 
Run the CMake executable.  You may have to specify the location of the ITK build directory if it was not installed.
 
  cmake ../HelloInsight
 
or
 
  cmake -DITK_DIR=/path/to/ITK_build ../HelloInsight
 
Once the CMake configuration completes successfully, build the project.  On Linux or Mac,
 
  make
 
On Windows, open the Visual Studio project file and build it.
 
  HelloInsight.sln
 
== Run the new executable ==
 
  ./HelloInsight
 
{{ITK/Template/Footer}}

Latest revision as of 21:01, 4 December 2019

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 demonstrate here a simple ITK program. While simple, it is 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 let's get started!

Write the source code

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>

int main()
{
  typedef itk::Image< unsigned short, 3 > ImageType;

  ImageType::Pointer image = ImageType::New();

  std::cout << "Hello ITK World !" << std::endl;

  return 0;
}

Next to HelloInsight.cxx, create a file, CMakeLists.txt, and populate it with:

cmake_minimum_required(VERSION 3.10.2)

project(HelloInsight)

# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable(HelloInsight HelloInsight.cxx )

target_link_libraries(HelloInsight ${ITK_LIBRARIES})

Build the project

Next to the HelloInsight directory, create a directory to build the project, HelloInsight_build. cd into that directory:

 cd HelloInsight_build

Run the CMake executable. You may have to specify the location of the ITK build directory if it was not installed.

 cmake ../HelloInsight

or

 cmake -DITK_DIR=/path/to/ITK_build ../HelloInsight

Once the CMake configuration completes successfully, build the project. On Linux or Mac,

 make

On Windows, open the Visual Studio project file and build it.

 HelloInsight.sln

Run the new executable

 ./HelloInsight



ITK: [Welcome | Site Map]