Register IO Factories#

Synopsis#

When CMake is not used to build an executable or library against ITK, the Image and Transform IO format objects are not automatically registered to ITK’s object factory system, so they must be manually registered. There is not a static list of IO classes because the classes available depend on which modules are enabled when ITK is configured. This examples shows how to register the ImageIOBase objects so the ImageFileReader can read the formats corresponding to the registered objects.

Results#

When CMake is not used to register the IO classes, there are
0 IO objects available to the ImageFileReader.

When we try to read a MetaImage, we will fail.

After registering the MetaImageIO object, there are
1 IO objects available to the ImageFileReader.

Now, when we try to read a MetaImage, we will succeed.

Every format desired to be supported by the reader
must be registered.

Code#

C++#

#include "itkImageFileReader.h"
#include "itkMetaImageIOFactory.h"
#include "itkPNGImageIOFactory.h"

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

  const char * metaImageFileName = argv[1];
  const char * pngFileName = argv[2];

  constexpr unsigned int Dimension = 2;

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

  using ReaderType = itk::ImageFileReader<ImageType>;
  auto reader = ReaderType::New();

  using RegisteredObjectsContainerType = std::list<itk::LightObject::Pointer>;


  RegisteredObjectsContainerType registeredIOs = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");
  std::cout << "When CMake is not used to register the IO classes, there are\n"
            << registeredIOs.size() << " IO objects available to the ImageFileReader.\n"
            << std::endl;


  std::cout << "When we try to read a MetaImage, we will ";
  reader->SetFileName(metaImageFileName);
  try
  {
    reader->Update();
  }
  catch (const itk::ImageFileReaderException &)
  {
    std::cout << "fail.\n" << std::endl;
  }


  std::cout << "After registering the MetaImageIO object, ";
  itk::MetaImageIOFactory::RegisterOneFactory();

  std::cout << "there are\n";
  registeredIOs = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");
  std::cout << registeredIOs.size() << " IO objects available to the ImageFileReader.\n" << std::endl;

  std::cout << "Now, when we try to read a MetaImage, we will ";
  try
  {
    reader->Update();
    std::cout << "succeed.\n" << std::endl;
  }
  catch (const itk::ImageFileReaderException & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }


  std::cout << "Every format desired to be supported by the reader\n"
            << "must be registered." << std::endl;
  itk::PNGImageIOFactory::RegisterOneFactory();
  reader->SetFileName(pngFileName);
  try
  {
    reader->Update();
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }


  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TOutputImage, typename ConvertPixelTraits = DefaultConvertPixelTraits<typename TOutputImage::IOPixelType>>
class ImageFileReader : public itk::ImageSource<TOutputImage>

Data source that reads image data from a single file.

This source object is a general filter to read data from a variety of file formats. It works with a ImageIOBase subclass to actually do the reading of the data. Object factory machinery can be used to automatically create the ImageIOBase, or the ImageIOBase can be manually created and set. Note that this class reads data from a single file; if you wish to read data from a series of files use ImageSeriesReader.

TOutputImage is the type expected by the external users of the filter. If data stored in the file is stored in a different format then specified by TOutputImage, than this filter converts data between the file type and the external expected type. The ConvertPixelTraits template parameter is used to do the conversion.

A Pluggable factory pattern is used this allows different kinds of readers to be registered (even at run time) without having to modify the code in this class. Normally just setting the FileName with the appropriate suffix is enough to get the reader to instantiate the correct ImageIO and read the file properly. However, some files (like raw binary format) have no accepted suffix, so you will have to manually create the ImageIO instance of the write type.

See

ImageSeriesReader

See

ImageIOBase

ITK Sphinx Examples:

See itk::ImageFileReader for additional documentation.
class ObjectFactoryBase : public itk::Object

Create instances of classes using an object factory.

ObjectFactoryBase is used to create itk objects. The base class ObjectFactoryBase contains a static method CreateInstance() that is used to create itk objects from the list of registerd ObjectFactoryBase sub-classes. The first time CreateInstance() is called, all dll’s or shared libraries in the environment variable ITK_AUTOLOAD_PATH are loaded into the current process. The C function itkLoad is called on each dll. itkLoad should return an instance of the factory sub-class implemented in the shared library. ITK_AUTOLOAD_PATH is an environment variable containing a colon separated (semi-colon on win32) list of paths.

This can be use to override the creation of any object in ITK.

Subclassed by itk::BioRadImageIOFactory, itk::BMPImageIOFactory, itk::Bruker2dseqImageIOFactory, itk::BYUMeshIOFactory, itk::DCMTKImageIOFactory, itk::FileListVideoIOFactory, itk::FreeSurferAsciiMeshIOFactory, itk::FreeSurferBinaryMeshIOFactory, itk::GDCMImageIOFactory, itk::GE4ImageIOFactory, itk::GE5ImageIOFactory, itk::GEAdwImageIOFactory, itk::GiftiMeshIOFactory, itk::GiplImageIOFactory, itk::GPUBinaryThresholdImageFilterFactory, itk::GPUDemonsRegistrationFilterFactory, itk::GPUGradientAnisotropicDiffusionImageFilterFactory, itk::GPUImageFactory, itk::GPUMeanImageFilterFactory, itk::HDF5ImageIOFactory, itk::HDF5TransformIOFactory, itk::JPEG2000ImageIOFactory, itk::JPEGImageIOFactory, itk::LSMImageIOFactory, itk::MatlabTransformIOFactory, itk::MetaImageIOFactory, itk::MINCImageIOFactory, itk::MRCImageIOFactory, itk::NiftiImageIOFactory, itk::NrrdImageIOFactory, itk::ObjectFactory< T >, itk::OBJMeshIOFactory, itk::OFFMeshIOFactory, itk::OpenCVVideoIOFactory, itk::PNGImageIOFactory, itk::RawImageIOFactory< TPixel, VImageDimension >, itk::SiemensVisionImageIOFactory, itk::SpatialObjectFactoryBase, itk::StimulateImageIOFactory, itk::TIFFImageIOFactory, itk::TransformFactoryBase, itk::TxtTransformIOFactory, itk::VoxBoCUBImageIOFactory, itk::VTKImageIOFactory, itk::VTKPolyDataMeshIOFactory, itk::VXLVideoIOFactory

See itk::ObjectFactoryBase for additional documentation.
class MetaImageIOFactory : public itk::ObjectFactoryBase

Create instances of MetaImageIO objects using an object factory.

See itk::MetaImageIOFactory for additional documentation.
class PNGImageIOFactory : public itk::ObjectFactoryBase

Create instances of PNGImageIO objects using an object factory.

See itk::PNGImageIOFactory for additional documentation.