Create 3D Volume

Synopsis

This example reads in a series of 2D images and stacks them to create a 3D image.

Results

Input 2D images

Input 2D images

Output 3D image

Single output 3D image

Code

C++

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

int
main(int argc, char * argv[])
{
  if (argc < 3)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << "<input1> <input2> <input3> ... <output>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int InputDimension = 2;
  constexpr unsigned int OutputDimension = 3;

  using PixelType = unsigned char;
  using InputImageType = itk::Image<PixelType, InputDimension>;
  using OutputImageType = itk::Image<PixelType, OutputDimension>;

  using ReaderType = itk::ImageFileReader<InputImageType>;
  ReaderType::Pointer reader = ReaderType::New();

  using FilterType = itk::TileImageFilter<InputImageType, OutputImageType>;
  FilterType::Pointer filter = FilterType::New();

  itk::FixedArray<unsigned int, OutputDimension> layout;
  layout[0] = 2;
  layout[1] = 2;
  layout[2] = 0;

  filter->SetLayout(layout);

  for (int ii = 1; ii < argc - 1; ++ii)
  {
    reader->SetFileName(argv[ii]);

    try
    {
      reader->Update();
    }
    catch (itk::ExceptionObject & e)
    {
      std::cerr << e << std::endl;
      return EXIT_FAILURE;
    }

    InputImageType::Pointer input = reader->GetOutput();
    input->DisconnectPipeline();

    filter->SetInput(ii - 1, input);
  }

  constexpr PixelType defaultValue = 128;

  filter->SetDefaultPixelValue(defaultValue);

  using WriterType = itk::ImageFileWriter<OutputImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(argv[argc - 1]);
  writer->SetInput(filter->GetOutput());

  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import itk

if len(sys.argv) < 3:
    print("Usage: " + sys.argv[0] + " <input1> <input2> <input3> ... <output>")
    sys.exit(1)

InputDimension = 2
OutputDimension = 3

PixelType = itk.UC

InputImageType = itk.Image[PixelType, InputDimension]
OutputImageType = itk.Image[PixelType, OutputDimension]

reader = itk.ImageFileReader[InputImageType].New()

tileFilter = itk.TileImageFilter[InputImageType, OutputImageType].New()

layout = [2, 2, 0]
tileFilter.SetLayout(layout)

for ii in range(1, len(sys.argv)-1):
    reader.SetFileName(sys.argv[ii])
    reader.Update()

    inputImage = reader.GetOutput()
    inputImage.DisconnectPipeline()

    tileFilter.SetInput(ii-1, inputImage)

defaultValue = 128
tileFilter.SetDefaultPixelValue(defaultValue)
tileFilter.Update()

writer = itk.ImageFileWriter[OutputImageType].New()
writer.SetFileName(sys.argv[-1])
writer.SetInput(tileFilter.GetOutput())
writer.Update()

Classes demonstrated