Generate Slices From Volume#
Synopsis#
Create series of 2D images from a given volume.
Results#
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>;
const auto input = itk::ReadImage<InputImageType>(inputFileName);
using OutputPixelType = unsigned char;
using RescaleImageType = itk::Image<OutputPixelType, Dimension>;
using RescaleFilterType = itk::RescaleIntensityImageFilter<InputImageType, RescaleImageType>;
auto rescale = RescaleFilterType::New();
rescale->SetInput(input);
rescale->SetOutputMinimum(0);
rescale->SetOutputMaximum(255);
rescale->UpdateLargestPossibleRegion();
InputImageType::RegionType region = input->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>;
auto writer = WriterType::New();
writer->SetInput(rescale->GetOutput());
writer->SetFileNames(fnames->GetFileNames());
try
{
writer->Update();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#!/usr/bin/env python
import itk
import argparse
itk.auto_progress(2)
parser = argparse.ArgumentParser(description="Generate Slices From Volume.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("extension", nargs="?")
args = parser.parse_args()
if args.extension:
extension = args.extension
else:
extension = ".png"
fileNameFormat = args.output_image + "-%d" + extension
Dimension = 3
PixelType = itk.UC
InputImageType = itk.Image[PixelType, Dimension]
ReaderType = itk.ImageFileReader[InputImageType]
reader = ReaderType.New()
reader.SetFileName(args.input_image)
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:
-
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: