Convert RGB vtkImageData to an itk::Image#

Synopsis#

Convert RGB vtkImageData to an itk::Image in a processing pipeline. 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:

Image (0x2dc70e0)
  RTTI typeinfo:   itk::Image<itk::RGBPixel<unsigned char>, 2u>
  Reference Count: 2
  Modified Time: 28
  Debug: Off
  Object Name:
  Observers:
    none
  Source: (0x3281a40)
  Source output name: Primary
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 23
  UpdateMTime: 29
  RealTimeStamp: 0 seconds
  LargestPossibleRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [127, 162]
  BufferedRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [127, 162]
  RequestedRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [127, 162]
  Spacing: [1, 1]
  Origin: [0, 0]
  Direction:
1 0
0 1

  IndexToPointMatrix:
1 0
0 1

  PointToIndexMatrix:
1 0
0 1

  Inverse Direction:
1 0
0 1

  PixelContainer:
    ImportImageContainer (0x3282c00)
      RTTI typeinfo:   itk::ImportImageContainer<unsigned long, itk::RGBPixel<unsigned char> >
      Reference Count: 1
      Modified Time: 27
      Debug: Off
      Object Name:
      Observers:
        none
      Pointer: 0x3196130
      Container manages memory: false
      Size: 20574
      Capacity: 20574

Code#

Python#

#!/usr/bin/env python

import itk
import vtk
import argparse

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

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

reader = vtk.vtkPNGReader()
reader.SetFileName(args.input_image)
reader.SetDataScalarTypeToUnsignedChar()

vtkToItkFilter = itk.VTKImageToImageFilter[ImageType].New()
vtkToItkFilter.SetInput(reader.GetOutput())

reader.Update()
vtkToItkFilter.Update()
myitkImage = vtkToItkFilter.GetOutput()
print(myitkImage)

C++#

#include "vtkSmartPointer.h"
#include "vtkPNGReader.h"
#include "itkVTKImageToImageFilter.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>;

  vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
  reader->SetFileName(inputFileName);
  reader->SetDataScalarTypeToUnsignedChar();

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

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

  ImageType::ConstPointer myitkImage = filter->GetOutput();
  myitkImage->Print(std::cout);

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TOutputImage>
class VTKImageToImageFilter : public itk::VTKImageImport<TOutputImage>

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

This class puts together an itk::VTKImageImport and a vtk::ImageExport. 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 a vtkImageData can be plugged as input and an itk::Image is produced as output.

ITK Sphinx Examples:

See itk::VTKImageToImageFilter for additional documentation.