Generate Slices From Volume

Synopsis

Create series of 2D images from a given volume.

Results

Input image

Input image

Output image, slice 20

Output image, slice 20

Output image, slice 30

Output image, slice 30

Code

C++

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkImageSeriesWriter.h"

int
main(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " <InputFileName> <OutputFileName>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];

  std::string format = std::string(outputFileName) + std::string("-%d.png");

  constexpr unsigned int Dimension = 3;

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

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

  using OutputPixelType = unsigned char;
  using RescaleImageType = itk::Image<OutputPixelType, Dimension>;

  using RescaleFilterType = itk::RescaleIntensityImageFilter<InputImageType, RescaleImageType>;
  RescaleFilterType::Pointer rescale = RescaleFilterType::New();
  rescale->SetInput(reader->GetOutput());
  rescale->SetOutputMinimum(0);
  rescale->SetOutputMaximum(255);
  rescale->UpdateLargestPossibleRegion();

  InputImageType::RegionType region = reader->GetOutput()->GetLargestPossibleRegion();
  InputImageType::SizeType   size = region.GetSize();

  itk::NumericSeriesFileNames::Pointer fnames = itk::NumericSeriesFileNames::New();
  fnames->SetStartIndex(0);
  fnames->SetEndIndex(size[2] - 1);
  fnames->SetIncrementIndex(1);
  fnames->SetSeriesFormat(format.c_str());

  using OutputImageType = itk::Image<OutputPixelType, 2>;

  using WriterType = itk::ImageSeriesWriter<RescaleImageType, OutputImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(rescale->GetOutput());
  writer->SetFileNames(fnames->GetFileNames());

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

  return EXIT_SUCCESS;
}
#!/usr/bin/env python
import itk
itk.auto_progress(2)

if len(sys.argv) < 3:
    print("Usage: " + sys.argv[0] +
          " <InputFileName> <OutputFileName> [Extension]")
    sys.exit(1)

inputFileName = sys.argv[1]
outputFileName = sys.argv[2]
if len(sys.argv) > 3:
    extension = sys.argv[3]
else:
    extension = ".png"

fileNameFormat = outputFileName + "-%d" + extension

Dimension = 3

PixelType = itk.UC
InputImageType = itk.Image[PixelType, Dimension]

ReaderType = itk.ImageFileReader[InputImageType]
reader = ReaderType.New()
reader.SetFileName(inputFileName)

OutputPixelType = itk.UC
RescaleImageType = itk.Image[OutputPixelType, Dimension]

RescaleFilterType = itk.RescaleIntensityImageFilter[InputImageType,
                                                    RescaleImageType]
rescale = RescaleFilterType.New()
rescale.SetInput(reader.GetOutput())
rescale.SetOutputMinimum(0)
rescale.SetOutputMaximum(255)
rescale.UpdateLargestPossibleRegion()

region = reader.GetOutput().GetLargestPossibleRegion()
size = region.GetSize()

fnames = itk.NumericSeriesFileNames.New()
fnames.SetStartIndex(0)
fnames.SetEndIndex(size[2] - 1)
fnames.SetIncrementIndex(1)
fnames.SetSeriesFormat(fileNameFormat)

OutputImageType = itk.Image[OutputPixelType, 2]

WriterType = itk.ImageSeriesWriter[RescaleImageType, OutputImageType]
writer = WriterType.New()
writer.SetInput(rescale.GetOutput())
writer.SetFileNames(fnames.GetFileNames())

writer.Update()

Classes demonstrated

class NumericSeriesFileNames : public itk::Object

Generate an ordered sequence of filenames.

This class generate an ordered sequence of files whose filenames contain a single unique, non-negative, integral value (e.g. test.1.png, test2.png, foo.3, etc.).

The file name is created from a sprintf-style series format which should contain an integer format string like “%d”. Bad formats will cause the series reader to throw an exception.

Warning: returned filenames (which may be full or relative paths) are not checked against any system-imposed path-length limit, because of difficulties finding a portable method to do so.

ITK Sphinx Examples:

See itk::NumericSeriesFileNames for additional documentation.
template<typename TInputImage, typename TOutputImage>
class ImageSeriesWriter : public itk::ProcessObject

Writes image data to a series of data files.

ImageSeriesWriter writes its input data to a series of output files. The writer is templated over an input image type and an output image type. Usually, the output image type will have fewer dimensions than the input image type. Each file has a name created using the SeriesFormat. This string is used as a sprintf argument to build a filename. The string should contain zero or one “%d” or equivalent. The “%d” is an incremental file number that starts at StartIndex and is incremented by IncrementIndex. Since this writer uses an internal instance of an ImageFileWriter, the type of file is determined by either the file extension or an ImageIO class if specified.

See

ImageFileWriter

See

ImageIOBase

See

ImageSeriesReader

ITK Sphinx Examples:

See itk::ImageSeriesWriter for additional documentation.