Paste Image Into Another One#

Synopsis#

Paste one itk::Image into another one

Results#

Source image

Source image#

Destination image

Destination image#

Output image

Output image#

Code#

C++#

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

int
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;

  const auto source = itk::ReadImage<ImageType>(sourceFileName);
  const auto destination = itk::ReadImage<ImageType>(destinationFileName);

  using FilterType = itk::PasteImageFilter<ImageType, ImageType>;
  auto filter = FilterType::New();
  filter->SetSourceImage(source);
  filter->SetSourceRegion(source->GetLargestPossibleRegion());
  filter->SetDestinationImage(destination);
  filter->SetDestinationIndex(index);

  try
  {
    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 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.