Medical image analysis toolkit providing algorithms for segmentation, registration, and processing of medical images
—
ITK provides comprehensive support for reading and writing medical and standard image formats through a flexible IO framework. The system uses format-specific ImageIO classes behind generic reader/writer interfaces, supporting formats from DICOM to standard image types.
Generic image file reader that automatically detects file format.
template<typename TOutputImage>
class ImageFileReader : public ImageSource<TOutputImage>
{
public:
typedef ImageFileReader Self;
typedef ImageSource<TOutputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
// Object creation
static Pointer New();
// File specification
itkSetStringMacro(FileName);
itkGetStringMacro(FileName);
// Format control
void SetImageIO(ImageIOBase* imageIO);
itkGetObjectMacro(ImageIO, ImageIOBase);
// Pipeline execution
virtual void GenerateOutputInformation() ITK_OVERRIDE;
virtual void GenerateData() ITK_OVERRIDE;
// Convenience methods
virtual void Update() ITK_OVERRIDE;
const TOutputImage * GetOutput() const;
TOutputImage * GetOutput();
};Reads a series of 2D images as a single 3D volume.
template<typename TOutputImage>
class ImageSeriesReader : public ImageSource<TOutputImage>
{
public:
typedef ImageSeriesReader Self;
typedef ImageSource<TOutputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
// File series specification
void SetFileNames(const FileNamesContainer & names);
const FileNamesContainer & GetFileNames() const;
void AddFileName(const std::string & name);
// DICOM-specific methods
void SetImageIO(ImageIOBase* imageIO);
void UseStreamingOn();
void UseStreamingOff();
// Metadata access
void ForceOrthogonalDirectionOff();
void ForceOrthogonalDirectionOn();
};Generic image file writer with automatic format detection.
template<typename TInputImage>
class ImageFileWriter : public ProcessObject
{
public:
typedef ImageFileWriter Self;
typedef ProcessObject Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
// Object creation
static Pointer New();
// Input specification
using Superclass::SetInput;
void SetInput(const TInputImage * image);
const TInputImage * GetInput();
const TInputImage * GetInput(unsigned int idx);
// File specification
itkSetStringMacro(FileName);
itkGetStringMacro(FileName);
// Format control
void SetImageIO(ImageIOBase* io);
itkGetObjectMacro(ImageIO, ImageIOBase);
// Compression control
void SetUseCompression(bool UseCompression);
void UseCompressionOn();
void UseCompressionOff();
itkGetConstMacro(UseCompression, bool);
// Writing operations
virtual void Write();
virtual void Update() ITK_OVERRIDE;
protected:
virtual void GenerateData() ITK_OVERRIDE;
};Writes a 3D image as a series of 2D slices.
template<typename TInputImage, typename TOutputImage>
class ImageSeriesWriter : public ProcessObject
{
public:
typedef ImageSeriesWriter Self;
typedef ProcessObject Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
// Input specification
void SetInput(const TInputImage * input);
const TInputImage * GetInput();
// Output file names
void SetFileNames(const FileNamesContainer & names);
const FileNamesContainer & GetFileNames() const;
// Format control
void SetImageIO(ImageIOBase* io);
void SetMetaDataDictionaryArray(const DictionaryArrayType * dictArray);
// Writing operations
virtual void Write();
virtual void Update() ITK_OVERRIDE;
// Compression
void SetUseCompression(bool UseCompression);
void UseCompressionOn();
void UseCompressionOff();
};Abstract base class for format-specific IO implementations.
class ImageIOBase : public LightProcessObject
{
public:
typedef ImageIOBase Self;
typedef LightProcessObject Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
// Format detection
virtual bool CanReadFile(const char* filename) = 0;
virtual bool CanWriteFile(const char* filename) = 0;
// File information
virtual void ReadImageInformation() = 0;
virtual void WriteImageInformation() = 0;
// Data reading/writing
virtual void Read(void* buffer) = 0;
virtual void Write(const void* buffer) = 0;
// Image properties
void SetNumberOfDimensions(unsigned int dimensions);
unsigned int GetNumberOfDimensions() const;
void SetDimensions(unsigned int i, unsigned int dim);
unsigned int GetDimensions(unsigned int i) const;
void SetOrigin(unsigned int i, double origin);
double GetOrigin(unsigned int i) const;
void SetSpacing(unsigned int i, double spacing);
double GetSpacing(unsigned int i) const;
// Pixel type information
void SetPixelType(const IOPixelType pixelType);
IOPixelType GetPixelType() const;
void SetComponentType(const IOComponentType componentType);
IOComponentType GetComponentType() const;
void SetNumberOfComponents(unsigned int components);
unsigned int GetNumberOfComponents() const;
// File name handling
itkSetStringMacro(FileName);
itkGetStringMacro(FileName);
};// Modern DICOM image IO (preferred)
class GDCMImageIO : public ImageIOBase
{
public:
typedef GDCMImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// DICOM-specific methods
bool CanReadFile(const char* filename) override;
void ReadImageInformation() override;
void Read(void* buffer) override;
bool CanWriteFile(const char* filename) override;
void WriteImageInformation() override;
void Write(const void* buffer) override;
};
// Legacy DICOM image IO (deprecated - use GDCMImageIO instead)
class DICOMImageIO2 : public ImageIOBase
{
public:
typedef DICOMImageIO2 Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// DICOM-specific methods
virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;
virtual void ReadImageInformation() ITK_OVERRIDE;
virtual void Read(void* buffer) ITK_OVERRIDE;
// Metadata access
void GetPatientName(std::string & name);
void GetPatientID(std::string & id);
void GetStudyID(std::string & id);
void GetSeriesID(std::string & id);
};
// DICOM series file names generator
class DICOMSeriesFileNames : public Object
{
public:
typedef DICOMSeriesFileNames Self;
typedef Object Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// Directory scanning
void SetDirectory(const std::string & directory);
const std::string & GetDirectory() const;
// Series discovery
const SeriesUIDContainer & GetSeriesUIDs();
const FileNamesContainer & GetFileNames(const std::string & seriesUID);
// Ordering control
void SetUseSeriesDetails(bool useSeriesDetails);
void AddSeriesRestriction(const std::string & tag);
};// NIFTI format support
class NiftiImageIO : public ImageIOBase
{
public:
typedef NiftiImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;
virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;
virtual void ReadImageInformation() ITK_OVERRIDE;
virtual void WriteImageInformation() ITK_OVERRIDE;
virtual void Read(void* buffer) ITK_OVERRIDE;
virtual void Write(const void* buffer) ITK_OVERRIDE;
};
// Analyze format support
class AnalyzeImageIO : public ImageIOBase
{
public:
typedef AnalyzeImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;
virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;
virtual void ReadImageInformation() ITK_OVERRIDE;
virtual void WriteImageInformation() ITK_OVERRIDE;
virtual void Read(void* buffer) ITK_OVERRIDE;
virtual void Write(const void* buffer) ITK_OVERRIDE;
};
// MetaImage format support
class MetaImageIO : public ImageIOBase
{
public:
typedef MetaImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;
virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;
virtual void ReadImageInformation() ITK_OVERRIDE;
virtual void WriteImageInformation() ITK_OVERRIDE;
virtual void Read(void* buffer) ITK_OVERRIDE;
virtual void Write(const void* buffer) ITK_OVERRIDE;
// MetaImage-specific
void SetDataByteOrderToBigEndian();
void SetDataByteOrderToLittleEndian();
};// JPEG support
class JPEGImageIO : public ImageIOBase
{
public:
typedef JPEGImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// JPEG quality control
void SetQuality(int quality);
int GetQuality() const;
// Progressive JPEG
void SetProgressive(bool progressive);
bool GetProgressive() const;
};
// PNG support
class PNGImageIO : public ImageIOBase
{
public:
typedef PNGImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// PNG-specific options
void SetCompressionLevel(int level);
int GetCompressionLevel() const;
};
// TIFF support
class TIFFImageIO : public ImageIOBase
{
public:
typedef TIFFImageIO Self;
typedef ImageIOBase Superclass;
typedef SmartPointer<Self> Pointer;
static Pointer New();
// TIFF compression
void SetCompressionToNoCompression();
void SetCompressionToPackBits();
void SetCompressionToJPEG();
void SetCompressionToDeflate();
};Manages automatic format detection and IO object creation.
class ImageIOFactory : public Object
{
public:
typedef ImageIOFactory Self;
typedef Object Superclass;
typedef SmartPointer<Self> Pointer;
// Factory registration
static void RegisterBuiltInFactories();
// IO object creation
static ImageIOBase::Pointer CreateImageIO(const char* path, FileModeType mode);
// Factory management
static void RegisterFactory(ObjectFactoryBase* factory);
static void UnRegisterFactory(ObjectFactoryBase* factory);
};// Define image type
typedef itk::Image<short, 3> ImageType;
// Create reader
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
// Set filename and read
reader->SetFileName("brain.nii.gz");
try {
reader->Update();
ImageType::Pointer image = reader->GetOutput();
} catch (itk::ExceptionObject & error) {
std::cerr << "Reading failed: " << error << std::endl;
}// Setup DICOM names generator
typedef itk::DICOMSeriesFileNames NamesGeneratorType;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
nameGenerator->SetDirectory("/path/to/dicom/series");
// Get series UIDs
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
std::string seriesIdentifier = seriesUID.begin()->c_str();
// Get file names for series
typedef std::vector<std::string> FileNamesContainer;
FileNamesContainer fileNames = nameGenerator->GetFileNames(seriesIdentifier);
// Create series reader
typedef itk::Image<short, 3> ImageType;
typedef itk::ImageSeriesReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
// Configure DICOM IO (using preferred GDCMImageIO)
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetImageIO(dicomIO);
reader->SetFileNames(fileNames);
// Read series
reader->Update();
ImageType::Pointer image = reader->GetOutput();// Create writer
typedef itk::Image<short, 3> ImageType;
typedef itk::ImageFileWriter<ImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
// Configure output
writer->SetFileName("output.nii.gz");
writer->SetInput(processedImage);
writer->UseCompressionOn();
// Write file
try {
writer->Update();
} catch (itk::ExceptionObject & error) {
std::cerr << "Writing failed: " << error << std::endl;
}// IO enums
enum IOPixelType {
UNKNOWNPIXELTYPE,
SCALAR,
RGB,
RGBA,
OFFSET,
VECTOR,
POINT,
COVARIANTVECTOR,
SYMMETRICSECONDRANKTENSOR,
DIFFUSIONTENSOR3D,
COMPLEX,
FIXEDARRAY,
MATRIX
};
enum IOComponentType {
UNKNOWNCOMPONENTTYPE,
UCHAR,
CHAR,
USHORT,
SHORT,
UINT,
INT,
ULONG,
LONG,
FLOAT,
DOUBLE
};
enum FileModeType {
ReadMode,
WriteMode
};
// Container types
typedef std::vector<std::string> FileNamesContainer;
typedef std::vector<std::string> SeriesIdContainer;
typedef std::vector<MetaDataDictionary *> DictionaryArrayType;ITK's IO framework provides robust, extensible support for medical and standard image formats, enabling seamless integration with medical imaging workflows and research pipelines.
Install with Tessl CLI
npx tessl i tessl/cmake-itk