ITK/Release 4/Wrapping/Examples
From KitwarePublic
Jump to navigationJump to search
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:
python Example.py input.bmp output.png
ITK Image Read/Write 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()
ITK Image Read/Write 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