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

C++

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

int
main(int, char *[])
{
  constexpr unsigned int Dimension = 2;
  using PixelType = unsigned char;

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

  using RandomSourceType = itk::RandomImageSource<ImageType>;

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

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

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

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

  return EXIT_SUCCESS;
}

Classes demonstrated