VtkKWImage

From KitwarePublic
Jump to navigationJump to search

vtkKWImage is a convenient class that stores a handle to an image both in VTK and ITK format. Here is a simple example to get a handle to the ITK image stored in this class. This is not covered in the original Insight Journal article and is posted here for reference. This example will also use the vtkKWImageIO class to read an image. The image is read in the native format stored on disk. It is up to the user to check the data type and cast the image properly.


Here is the CMakeLists.txt file for the project

PROJECT(KWLoadImageTest)
INCLUDE(${CMAKE_ROOT}/Modules/FindITK.cmake)
 IF (USE_ITK_FILE) 
   INCLUDE(${USE_ITK_FILE})
 ENDIF (USE_ITK_FILE)
 IF(NOT VTK_FOUND) 
   FIND_PACKAGE(VTK REQUIRED) 
   INCLUDE(${VTK_USE_FILE})
 ENDIF(NOT VTK_FOUND)

 INCLUDE_DIRECTORIES(${KWLoadImageTest_SOURCE_DIR} ${KWLoadImageTest_BINARY_DIR})
 ADD_EXECUTABLE(KWLoadImageTest KWLoadImageTest.cxx vtkKWImage.cxx vtkKWImageIO.cxx)
 TARGET_LINK_LIBRARIES(KWLoadImageTest 
   ITKCommon  
   ITKIO 
   ITKBasicFilters 
   vtkCommon 
   vtkImaging 
   vtkIO 
   itkzlib itksys
 )

Here is a simple program that reads an image using the vtkKWImageIO class and then saves the image using both the itk::Image representation as well as the vtkKWImage. This could be extended to also write the image as vtkImageData as well. The program will read DICOM series data as well as a single image file format such as (Nifti, Meta, or NRRD). The DICOM file is assumed to end in .dcm.

/* Include the required headers */
#include "vtkKWImageIO.h"
#include "itkImage.h"
#include "vtkImageData.h"
#include "vtkKWImage.h"
#include "itkImageFileWriter.h"
#include "vtkImageWriter.h"
#include <itksys/SystemTools.hxx>

int main( int argc, char * argv [] )
{

 /* The program expects three input strings:
  *   1) Input image
  *   2) Output ITK image
  *   3) Output vtkKWImage
  */

 /* Check the name of the file */
 std::string directoryName = itksys::SystemTools::GetFilenamePath( argv[1] );
 std::string extension = itksys::SystemTools::GetFilenameExtension( argv[1] );
 
 vtkKWImageIO *reader = vtkKWImageIO::New();
 
 if (extension == ".dcm")
   {
   /* DICOM Image Series */
   reader->SetFileName( argv[1] );
   reader->SetDirectory( directoryName );
 
   try
     {
     reader->ReadImageSeries();
     }
   catch( itk::ExceptionObject & excp )
     {
     std::cerr << excp << std::endl;
     return EXIT_FAILURE;
     }
   }
 else
   {
   /* 3D Image stored as a single file */
   reader->SetFileName( argv[1] );
   try
     {
     reader->ReadImage();
     }
   catch( itk::ExceptionObject & excp )
     {
     std::cerr << excp << std::endl;
     return EXIT_FAILURE;
     }
   }
 
 /* Get a pointer to the vtkKWImage */
 vtkKWImage  *kwImage = reader->HarvestReadImage();

 /* Get a pointer to the itk::Image */
 typedef itk::Image<signed short, 3>  ImageType; 
 const ImageType * testImage = static_cast< const ImageType * >(kwImage->GetITKImageBase() );
 
 /* Write the itk::Image */
 typedef itk::ImageFileWriter<ImageType>  WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetInput(testImage);
 writer->SetFileName( argv[2] );
 try
   {
   writer->Update();
   }
 catch( itk::ExceptionObject & excp )
   {
   std::cerr  << excp << std::endl;
   }
 
 /* Write the vtkKWImage */
 vtkKWImageIO * kwWriter = vtkKWImageIO::New();
 kwWriter->SetFileName( argv[3] );
 kwWriter->SetImageToBeWritten( kwImage );  
 try
   {
   kwWriter->WriteImage();
   }
 catch( itk::ExceptionObject & excp )
   {
   std::cerr << excp << std::endl;
   return EXIT_FAILURE;
   }

 kwWriter->Delete();
 kwImage->Delete();
 reader->Delete();	
 
 return EXIT_SUCCESS;
}