Tile Images Side by Side#

Synopsis#

Tile multiple images side by side.

Results#

Gourds.png

Gourds.png#

output.png

output.png#

Code#

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkTileImageFilter.h"

int
main(int argc, char * argv[])
{
  // Verify arguments
  if (argc < 4)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << "input1 input2 output" << std::endl;
    return EXIT_FAILURE;
  }

  // Parse arguments
  std::string input1FileName = argv[1];
  std::string input2FileName = argv[2];
  std::string outputFileName = argv[3];

  // Output arguments
  std::cout << "input1FileName " << input1FileName << std::endl;
  std::cout << "input2FileName " << input2FileName << std::endl;
  ;
  std::cout << "outputFileName " << outputFileName << std::endl;
  ;

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

  // Read images
  const auto input1 = itk::ReadImage<ImageType>(input1FileName);
  const auto input2 = itk::ReadImage<ImageType>(input2FileName);

  // Tile the images side-by-side
  using TileFilterType = itk::TileImageFilter<ImageType, ImageType>;

  auto tileFilter = TileFilterType::New();

  itk::FixedArray<unsigned int, 2> layout;

  layout[0] = 2;
  layout[1] = 0;

  tileFilter->SetLayout(layout);

  tileFilter->SetInput(0, input1);
  tileFilter->SetInput(1, input2);

  // Set the value of output pixels which are created by mismatched size input images.
  // If the two images are the same height, this will not be used.
  unsigned char fillerValue = 128;
  tileFilter->SetDefaultPixelValue(fillerValue);
  tileFilter->Update();

  itk::WriteImage(tileFilter->GetOutput(), outputFileName);

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class TileImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Tile multiple input images into a single output image.

This filter will tile multiple images using a user-specified layout. The tile sizes will be large enough to accommodate the largest image for each tile. The layout is specified with the SetLayout method. The layout has the same dimension as the output image. If all entries of the layout are positive, the tiled output will contain the exact number of tiles. If the layout contains a 0 in the last dimension, the filter will compute a size that will accommodate all of the images. Empty tiles are filled with the value specified with the SetDefault value method. The input images must have a dimension less than or equal to the output image. The output image have a larger dimension than the input images. This filter can be used to create a volume from a series of inputs by specifying a layout of 1,1,0.

ITK Sphinx Examples:

See itk::TileImageFilter for additional documentation.