ITK/Release 4/Wrapping/Examples: Difference between revisions

From KitwarePublic
< ITK‎ | Release 4‎ | Wrapping
Jump to navigationJump to search
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= ITK Image Read/Write in C++ =
= ITK Image Read/Write =
 
== in C++ ==
Following listing shows a simple program for ITK image read/write in C++
Following listing shows a simple program for ITK image read/write in C++


Line 24: Line 26:
   writer->SetInput( reader->GetOutput() );
   writer->SetInput( reader->GetOutput() );
   try  
   try  
    {  
  {  
writer->Update();  
        writer->Update();  
    }  
  }  
catch( itk::ExceptionObject & err )  
  catch( itk::ExceptionObject & err )  
    {  
  {  
std::cerr << err << std::endl;  
                std::cerr << err << std::endl;  
return 1;
                return 1;
    }  
  }  
return 0;
    return 0;
}
}
 
'''Compile & Execute:'''  
'''Compile & Execute:'''  
  python Example.py input.bmp output.png
Compile the code with CMake setup and using ITKCommon and ITKIO libraries. The CMakeLists.txt would look as below:
cmake_minimum_required (VERSION 2.6)
project (Example)
# add the ITK libraries here
#
FIND_PACKAGE (ITK REQUIRED)
IF( ITK_FOUND )
  include( ${ITK_USE_FILE} )
ENDIF( ITK_FOUND )
  # set ITK directories
set (EXTRA_LIBS ${EXTRA_LIBS} ITKCommon ITKIO)
# add the executable
add_executable (Example Example.cxx)
target_link_libraries (Example  ${EXTRA_LIBS})


= ITK Image Read/Write in Python =  
For executing the code, run it as:
Example input.bmp output.png
 
== in Python ==
Listing below is the same example as above but using WrapITK and python. This assumes that WrapITK is installed with the ITK build. You just need to set the DYLD_LIBRARY_PATH from the terminal as:
Listing below is the same example as above but using WrapITK and python. This assumes that WrapITK is installed with the ITK build. You just need to set the DYLD_LIBRARY_PATH from the terminal as:
  export DYLD_LIBRARY_PATH= <ITK installation directory>
  export DYLD_LIBRARY_PATH= <ITK installation directory>
Line 56: Line 75:
  writer.Update()
  writer.Update()


= ITK Image Read/Write in Java =
Execute the code as:
python Example.py
 
== in Java ==
Similar to python, the ITK functionality can also be accessed in Java. The listing below presents the above example in Java. Compile and execution instructions follow the listing:  
Similar to python, the ITK functionality can also be accessed in Java. The listing below presents the above example in Java. Compile and execution instructions follow the listing:  



Latest revision as of 16:00, 9 December 2011

ITK Image Read/Write

in C++

Following listing shows a simple program for ITK image read/write in C++

// Simple program for ITK image read/write in C++
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkRGBPixel.h"
int main (int argc, char *argv[])
{
   if (argc < 2)
   {
               fprintf(stdout,"Usage: %s inputImagename outputImagename\n",argv[0]);
               return 1;
   }
  typedef itk::RGBPixel< unsigned char >   PixelType;
  typedef itk::Image< PixelType, 2 >       ImageType;
  typedef itk::ImageFileReader< ImageType >  ReaderType;
  typedef itk::ImageFileWriter< ImageType >  WriterType;
  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
  reader->SetFileName( argv[1] );
  writer->SetFileName( argv[2] );
  writer->SetInput( reader->GetOutput() );
  try 
  { 
        writer->Update(); 
  } 
  catch( itk::ExceptionObject & err ) 
  { 
               std::cerr << err << std::endl; 
               return 1;
  } 
   return 0;
}

Compile & Execute: Compile the code with CMake setup and using ITKCommon and ITKIO libraries. The CMakeLists.txt would look as below:

cmake_minimum_required (VERSION 2.6)
project (Example)
# add the ITK libraries here
#
FIND_PACKAGE (ITK REQUIRED) 
IF( ITK_FOUND )
  include( ${ITK_USE_FILE} ) 
ENDIF( ITK_FOUND )
# set ITK directories
set (EXTRA_LIBS ${EXTRA_LIBS} ITKCommon ITKIO)
# add the executable
add_executable (Example Example.cxx)
target_link_libraries (Example  ${EXTRA_LIBS})

For executing the code, run it as:

Example input.bmp output.png

in Python

Listing below is the same example as above but using WrapITK and python. This assumes that WrapITK is installed with the ITK build. You just need to set the DYLD_LIBRARY_PATH from the terminal as:

export DYLD_LIBRARY_PATH= <ITK installation directory>
// Simple program for ITK image read/write in Python
#!/usr/bin/env python
import itk
from sys import argv
pixelType = itk.UC
imageType = itk.Image[pixelType, 2]
readerType = itk.ImageFileReader[imageType]
writerType = itk.ImageFileWriter[imageType]
reader = readerType.New()
writer = writerType.New()
reader.SetFileName( argv[1] )
writer.SetFileName( argv[2] )
writer.SetInput( reader.GetOutput() )
writer.Update()

Execute the code as:

python Example.py

in Java

Similar to python, the ITK functionality can also be accessed in Java. The listing below presents the above example in Java. Compile and execution instructions follow the listing:

// Simple program for ITK image read/write in Java
import org.itk.io.*;
public class Example
{
  public static void main( String argv[] )
  {
    itkImageFileReaderIUC2 reader = new itkImageFileReaderIUC2();
    itkImageFileWriterIUC2 writer = new itkImageFileWriterIUC2();

    reader.SetFileName( argv[0] );
    writer.SetFileName( argv[1] );
    writer.SetInput( reader.GetOutput() );
    writer.Update();
  }
}

Compile & Execute:

javac Example.java -classpath "<path-to-WrapITK-build-directory>/org.itk.io.jar"
java -classpath ".:<path-to-WrapITK-build-directory/org.itk.io.jar" -Djava.library.path="<path-to-WrapITK-build-directory>" Example input.bmp output.png