IO Selection for Image Reading


This example illustrates how to explicitly select a specific IO for image reading.

When using the default settings for the ImageFileReader class or the ReadImage function you have minimal control over the reading. That is, the specific IO mechanism is determined automatically based on the file type and the file is read into memory.

In some cases there are multiple IO classes that support reading the same image format and you do not know which one was used. For example the ‘SCIFIOImageIO’ and ‘JPEGImageIO’ both support reading JPEG images. As these are different implementations they may vary in performance and in the support of the features associated with the specific format. As a consequence, you may want or need to select one implementation over the other. Explicitly selecting the IO allows you to do so.


using System;
using itk.simple;

namespace itk.simple.examples {
    class ImageIOSelection {
        static void Main(string[] args) {
            try {
                if (args.Length < 1) {
                    Console.WriteLine("Usage: ImageIOSelection image_input_file");

                // Find out which image IOs are supported
                ImageFileReader reader = new ImageFileReader();
                itk.simple.VectorString image_ios = reader.GetRegisteredImageIOs();
                Console.Write("The supported image IOs are: ");
                for (int i=0; i<image_ios.Count; i++) {
                    Console.Write(image_ios[i] + " ");

                // Another option is to just print the reader and see which
                // IOs are supported

                // Force the use of a specific IO. If the IO doesn't support
                // reading the image type it will throw an exception.
                Image image = reader.Execute();
                Console.WriteLine("Read image: " + args[0]);

                VectorUInt32 size = image.GetSize();
                Console.WriteLine( "Image size: " + size[0] + " " + size[1] );

            } catch (Exception ex) {
                Console.WriteLine("Read failed: " + ex);