Paste Image Into Another One


Paste one itk::Image into another one


Source image

Source image

Destination image

Destination image

Output image

Output image



#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkPasteImageFilter.h"

main(int argc, char * argv[])
  if (argc != 6)
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <SourceFileName> <DestinationFileName> <OutputFileName> <start x> <start y>";
    std::cerr << std::endl;
    return EXIT_FAILURE;

  const char * sourceFileName = argv[1];
  const char * destinationFileName = argv[2];
  const char * outputFileName = argv[3];

  int startX = std::stoi(argv[4]);
  int startY = std::stoi(argv[5]);

  constexpr unsigned int Dimension = 2;

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

  ImageType::IndexType index;
  index[0] = startX;
  index[1] = startY;

  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer sourceReader = ReaderType::New();

  ReaderType::Pointer destinationReader = ReaderType::New();

  using FilterType = itk::PasteImageFilter<ImageType, ImageType>;
  FilterType::Pointer filter = FilterType::New();

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  catch (itk::ExceptionObject & error)
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;

  return EXIT_SUCCESS;

Classes demonstrated

template<typename TInputImage, typename TSourceImage = TInputImage, typename TOutputImage = TInputImage>
class PasteImageFilter : public itk::InPlaceImageFilter<TInputImage, TOutputImage>

Paste an image (or a constant value) into another image.

PasteImageFilter allows a region in a destination image to be filled with a source image or a constant pixel value. The SetDestinationIndex() method prescribes where in the destination input to start pasting data from the source input. The SetSourceRegion method prescribes the section of the second image to paste into the first. When a constant pixel value is set, the SourceRegion describes the size of the region filled. If the output requested region does not include the SourceRegion after it has been repositioned to DestinationIndex, then the output will just be a copy of the input.

This filter supports running “InPlace” to efficiently reuse the destination image buffer for the output, removing the need to copy the destination pixels to the output.

When the source image has a lower dimension than the destination image then the DestinationSkipAxes parameter specifies which axes in the destination image are set to 1 when copying the region or filling with a constant.

The two inputs and output image will have the same pixel type.

ITK Sphinx Examples:

See itk::PasteImageFilter for additional documentation.