Convert an RGB itk::Image To vtkImageData

Synopsis

Convert an itk::Image with RGB pixels to vtkImageData. This transfers the image buffer data along with image size, pixel spacing, and origin. Since vtkImageData does not yet support an orientation matrix, the direction cosines are lost. This requires Module_ITKVtkGlue to be turned on in ITK’s CMake configuration.

Results

Output:

vtkImageData (0x35dc400)
  Debug: Off
  Modified Time: 242
  Reference Count: 2
  Registered Events: (none)
  Information: 0x35dc590
  Data Released: False
  Global Release Data: Off
  UpdateTime: 243
  Field Data:
    Debug: Off
    Modified Time: 187
    Reference Count: 1
    Registered Events: (none)
    Number Of Arrays: 0
    Number Of Components: 0
    Number Of Tuples: 0
  Number Of Points: 20574
  Number Of Cells: 20286
  Cell Data:
    Debug: Off
    Modified Time: 195
    Reference Count: 1
    Registered Events: (none)
    Number Of Arrays: 0
    Number Of Components: 0
    Number Of Tuples: 0
    Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
    Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
    Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
    Scalars: (none)
    Vectors: (none)
    Normals: (none)
    TCoords: (none)
    Tensors: (none)
    GlobalIds: (none)
    PedigreeIds: (none)
    EdgeFlag: (none)
  Point Data:
    Debug: Off
    Modified Time: 242
    Reference Count: 1
    Registered Events: (none)
    Number Of Arrays: 1
    Array 0 name = scalars
    Number Of Components: 3
    Number Of Tuples: 20574
    Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
    Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
    Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
    Scalars:
      Debug: Off
      Modified Time: 242
      Reference Count: 1
      Registered Events: (none)
      Name: scalars
      Data type: unsigned char
      Size: 61722
      MaxId: 61721
      NumberOfComponents: 3
      Information: 0
      Name: scalars
      Number Of Components: 3
      Number Of Tuples: 20574
      Size: 61722
      MaxId: 61721
      LookupTable: (none)
      Array: 0x35c3430
    Vectors: (none)
    Normals: (none)
    TCoords: (none)
    Tensors: (none)
    GlobalIds: (none)
    PedigreeIds: (none)
    EdgeFlag: (none)
  Bounds:
    Xmin,Xmax: (0, 126)
    Ymin,Ymax: (0, 161)
    Zmin,Zmax: (0, 0)
  Compute Time: 256
  Spacing: (1, 1, 1)
  Origin: (0, 0, 0)

Code

C++

#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "itkRGBPixel.h"

int
main(int argc, char * argv[])
{
  if (argc != 2)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  const char * inputFileName = argv[1];

  constexpr unsigned int Dimension = 2;

  using PixelComponentType = unsigned char;
  using PixelType = itk::RGBPixel<PixelComponentType>;
  using ImageType = itk::Image<PixelType, Dimension>;

  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFileName);

  using FilterType = itk::ImageToVTKImageFilter<ImageType>;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(reader->GetOutput());

  try
  {
    filter->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  vtkImageData * myvtkImageData = filter->GetOutput();
  myvtkImageData->Print(std::cout);

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import itk

from distutils.version import StrictVersion as VS
if VS(itk.Version.GetITKVersion()) < VS("4.10.0"):
    print("ITK 4.10.0 is required.")
    sys.exit(1)

if len(sys.argv) != 2:
    print('Usage: ' + sys.argv[0] + ' <InputFileName>')
    sys.exit(1)
imageFileName = sys.argv[1]

Dimension = 2
PixelComponentType = itk.UC
PixelType = itk.RGBPixel[PixelComponentType]
ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(imageFileName)

itkToVtkFilter = itk.ImageToVTKImageFilter[ImageType].New()
itkToVtkFilter.SetInput(reader.GetOutput())

itkToVtkFilter.Update()
myvtkImageData = itkToVtkFilter.GetOutput()
print(myvtkImageData)

Classes demonstrated

template<typename TInputImage>
class ImageToVTKImageFilter : public itk::ProcessObject

Converts an ITK image into a VTK image and plugs a itk data pipeline to a VTK datapipeline.

This class puts together an itkVTKImageExporter and a vtkImageImporter. It takes care of the details related to the connection of ITK and VTK pipelines. The User will perceive this filter as an adaptor to which an itk::Image can be plugged as input and a vtkImage is produced as output.

ITK Sphinx Examples:

See itk::ImageToVTKImageFilter for additional documentation.