Create 3D Volume#
Synopsis#
This example reads in a series of 2D images and stacks them to create a 3D image.
Results#
Code#
Python#
#!/usr/bin/env python
import sys
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()
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 FilterType = itk::TileImageFilter<InputImageType, OutputImageType>;
auto 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)
{
InputImageType::Pointer input;
try
{
input = itk::ReadImage<InputImageType>(argv[ii]);
}
catch (const itk::ExceptionObject & e)
{
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
filter->SetInput(ii - 1, input);
}
constexpr PixelType defaultValue = 128;
filter->SetDefaultPixelValue(defaultValue);
try
{
itk::WriteImage(filter->GetOutput(), argv[argc - 1]);
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
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: