Duplicate an Image#

Synopsis#

This example demonstrates how to copy/clone/duplicate an image so it can continue down two separate paths of the pipeline.

Results#

Code#

Python#

#!/usr/bin/env python

import itk

Dimension = 2
PixelType = itk.UC

ImageType = itk.Image[PixelType, Dimension]

randomImageSource = itk.RandomImageSource[ImageType].New()
randomImageSource.SetNumberOfWorkUnits(1)  # to produce non-random results

image = randomImageSource.GetOutput()

clonedImage = itk.image_duplicator(image)

C++#

#include "itkImage.h"
#include "itkImageDuplicator.h"
#include "itkRandomImageSource.h"

int
main()
{
  constexpr unsigned int Dimension = 2;
  using PixelType = unsigned char;

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

  using RandomSourceType = itk::RandomImageSource<ImageType>;

  auto randomImageSource = RandomSourceType::New();
  randomImageSource->SetNumberOfWorkUnits(1); // to produce non-random results

  ImageType::Pointer image = randomImageSource->GetOutput();

  using DuplicatorType = itk::ImageDuplicator<ImageType>;
  auto duplicator = DuplicatorType::New();
  duplicator->SetInputImage(image);
  duplicator->Update();

  ImageType::Pointer clonedImage = duplicator->GetOutput();

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage>
class ImageDuplicator : public itk::Object

A helper class which creates an image which is perfect copy of the input image.

This class is NOT a filter. Although it has an API similar to a filter, this class is not intended to be used in a pipeline. Instead, the typical use will be like it is illustrated in the following code:

medianFilter->Update();
ImageType::Pointer image = medianFilter->GetOutput();
using DuplicatorType = itk::ImageDuplicator< ImageType >;
DuplicatorType::Pointer duplicator = DuplicatorType::New();
duplicator->SetInputImage(image);
duplicator->Update();
ImageType::Pointer clonedImage = duplicator->GetOutput();

Note that the Update() method must be called explicitly in the filter that provides the input to the ImageDuplicator object. This is needed because the ImageDuplicator is not a pipeline filter.

ITK Sphinx Examples:

See itk::ImageDuplicator for additional documentation.