Resample A Vector Image

Synopsis

Linearly interpolate a vector image.

The Python wrapping for the LinearInterpolateImageFunction using vector images was added in ITK 4.7.0.

Results

Input image

Input image

Output image

Output image

Code

C++

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkResampleImageFilter.h"
#include "itkIdentityTransform.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkRGBPixel.h"

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

  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];

  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::ResampleImageFilter<ImageType, ImageType>;

  FilterType::Pointer filter = FilterType::New();

  using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, double>;

  InterpolatorType::Pointer interpolator = InterpolatorType::New();

  filter->SetInterpolator(interpolator);

  using TransformType = itk::IdentityTransform<double, Dimension>;
  TransformType::Pointer transform = TransformType::New();

  filter->SetTransform(transform);

  PixelType defaultValue;
  defaultValue.Fill(50);
  filter->SetDefaultPixelValue(defaultValue);

  ImageType::SpacingType spacing;
  spacing[0] = .5; // pixel spacing in millimeters along X
  spacing[1] = .5; // pixel spacing in millimeters along Y

  filter->SetOutputSpacing(spacing);

  ImageType::PointType origin;
  origin[0] = 30.0; // X space coordinate of origin
  origin[1] = 40.0; // Y space coordinate of origin
  filter->SetOutputOrigin(origin);

  ImageType::DirectionType direction;
  direction.SetIdentity();
  filter->SetOutputDirection(direction);

  ImageType::SizeType size;

  size[0] = 300; // number of pixels along X
  size[1] = 300; // number of pixels along Y

  filter->SetSize(size);
  filter->SetInput(reader->GetOutput());

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFileName);
  writer->SetInput(filter->GetOutput());
  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import itk

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

if len(sys.argv) != 3:
    print("Usage: " + sys.argv[0] + " [inputImage] [outputImage]")
    sys.exit(1)

inputImage = sys.argv[1]
outputImage = sys.argv[2]

Dimension = 2

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

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

filterType = itk.ResampleImageFilter[ImageType, ImageType]
resampleImageFilter = filterType.New()

interpolatorType = itk.LinearInterpolateImageFunction[ImageType, itk.D]
interpolator = interpolatorType.New()

resampleImageFilter.SetInterpolator(interpolator)

transformType = itk.IdentityTransform[itk.D, Dimension]
transform = transformType.New()

resampleImageFilter.SetTransform(transform)

resampleImageFilter.SetDefaultPixelValue([50, 50, 50])
resampleImageFilter.SetOutputSpacing([0.5, 0.5])
resampleImageFilter.SetOutputOrigin([30, 40])

resampleImageFilter.SetSize([300, 300])
resampleImageFilter.SetInput(reader.GetOutput())

WriterType = itk.ImageFileWriter[ImageType]
writer = WriterType.New()
writer.SetFileName(outputImage)
writer.SetInput(resampleImageFilter.GetOutput())
writer.Update()

Classes demonstrated