Resample a Scalar Image#


Resample a scalar image.


Input image

Input image#

Output image

Output image#



#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Resample A Scalar Image.")
parser.add_argument("size_x", type=int)
parser.add_argument("size_y", type=int)
args = parser.parse_args()

output_size = [args.size_x, args.size_y]

input_image = itk.imread(args.input_image)

input_spacing = itk.spacing(input_image)
input_size = itk.size(input_image)
dimension = input_image.GetImageDimension()
output_spacing = [
    input_spacing[dim] * input_size[dim] / output_size[dim] for dim in range(dimension)

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

output_image = itk.resample_image_filter(

itk.imwrite(output_image, args.output_image)


#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkResampleImageFilter.h"
#include "itkIdentityTransform.h"

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

  constexpr unsigned int Dimension = 2;

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

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

  ImageType::SizeType outputSize;

  for (unsigned int dim = 0, k = 3; dim < Dimension; ++dim)
    outputSize[dim] = std::stoi(argv[k++]);

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

  ImageType::SizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
  std::cout << "Input Size: " << inputSize << std::endl;

  ImageType::SpacingType inputSpacing = inputImage->GetSpacing();
  std::cout << "Input Spacing: " << inputSpacing << std::endl;

  ImageType::SpacingType outputSpacing;

  for (unsigned int dim = 0; dim < Dimension; ++dim)
    outputSpacing[dim] = static_cast<double>(inputSpacing[dim]) * static_cast<double>(inputSize[dim]) /

  std::cout << "Output Size: " << outputSize << std::endl;
  std::cout << "Output Spacing: " << outputSpacing << std::endl;

  using TransformPrecisionType = double;
  using TransformType = itk::IdentityTransform<TransformPrecisionType, Dimension>;
  using FilterType = itk::ResampleImageFilter<ImageType, ImageType>;
  auto filter = FilterType::New();

    itk::WriteImage(filter->GetOutput(), outputFileName);
  catch (const itk::ExceptionObject & error)
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;

  return EXIT_SUCCESS;

Classes demonstrated#

template<typename TInputImage, typename TOutputImage, typename TInterpolatorPrecisionType = double, typename TTransformPrecisionType = TInterpolatorPrecisionType>
class ResampleImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Resample an image via a coordinate transform.

ResampleImageFilter resamples an existing image through some coordinate transform, interpolating via some image function. The class is templated over the types of the input and output images.

Note that the choice of interpolator function can be important. This function is set via SetInterpolator(). The default is LinearInterpolateImageFunction<InputImageType, TInterpolatorPrecisionType>, which is reasonable for ordinary medical images. However, some synthetic images have pixels drawn from a finite prescribed set. An example would be a mask indicating the segmentation of a brain into a small number of tissue types. For such an image, one does not want to interpolate between different pixel values, and so NearestNeighborInterpolateImageFunction< InputImageType, TCoordRep > would be a better choice.

If an sample is taken from outside the image domain, the default behavior is to use a default pixel value. If different behavior is desired, an extrapolator function can be set with SetExtrapolator().

Output information (spacing, size and direction) for the output image should be set. This information has the normal defaults of unit spacing, zero origin and identity direction. Optionally, the output information can be obtained from a reference image. If the reference image is provided and UseReferenceImage is On, then the spacing, origin and direction of the reference image will be used.

Since this filter produces an image which is a different size than its input, it needs to override several of the methods defined in ProcessObject in order to properly manage the pipeline execution model. In particular, this filter overrides ProcessObject::GenerateInputRequestedRegion() and ProcessObject::GenerateOutputInformation().

This filter is implemented as a multithreaded filter. It provides a DynamicThreadedGenerateData() method for its implementation.


For multithreading, the TransformPoint method of the user-designated coordinate transform must be threadsafe.

ITK Sphinx Examples:

See itk::ResampleImageFilter for additional documentation.