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#

Python#

#!/usr/bin/env python

import sys
import itk
import argparse

from distutils.version import StrictVersion

if StrictVersion(itk.Version.GetITKVersion()) < StrictVersion("5.2.0"):
    print("ITK 5.2.0 is required.")
    sys.exit(1)

parser = argparse.ArgumentParser(
    description="Convert An RBG itk Image to vtk Image Data."
)
parser.add_argument("input_image")
args = parser.parse_args()

inputImage = itk.imread(args.input_image)

vtkImage = itk.vtk_image_from_image(inputImage)

print(vtkImage)

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>;

  const auto input = itk::ReadImage<ImageType>(inputFileName);

  using FilterType = itk::ImageToVTKImageFilter<ImageType>;
  auto filter = FilterType::New();
  filter->SetInput(input);

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

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

  return EXIT_SUCCESS;
}

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.