IO Selection for Image Reading
Overview
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.
Code
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");
return;
}
// 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] + " ");
}
Console.WriteLine("\n--------------------");
// Another option is to just print the reader and see which
// IOs are supported
Console.WriteLine(reader.ToString());
Console.WriteLine("--------------------");
// Force the use of a specific IO. If the IO doesn't support
// reading the image type it will throw an exception.
reader.SetImageIO("PNGImageIO");
reader.SetFileName(args[0]);
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);
}
}
}
}
// This one header will include all SimpleITK filters and external
// objects.
#include <SimpleITK.h>
#include <iostream>
#include <exception>
// create convenient namespace alias
namespace sitk = itk::simple;
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " image_file_name\n";
return 1;
}
// Find out which image IOs are supported
sitk::ImageFileReader reader;
std::vector<std::string> image_ios = reader.GetRegisteredImageIOs();
std::cout << "The supported image IOs are: ";
std::vector<std::string>::iterator it;
for (it = image_ios.begin(); it != image_ios.end(); it++)
{
std::cout << (*it) << " ";
}
std::cout << std::endl;
std::cout << "--------------------" << std::endl;
// Another option is to just print the reader and see which
// IOs are supported
std::cout << reader.ToString();
std::cout << "--------------------" << std::endl;
// Read the image file
reader.SetImageIO("PNGImageIO");
reader.SetFileName(std::string(argv[1]));
try
{
sitk::Image image = reader.Execute();
std::cout << "Read image: " << argv[1] << std::endl;
std::vector<unsigned int> size = image.GetSize();
std::cout << "Image size: " << size[0] << " " << size[1] << std::endl;
}
catch (std::exception & e)
{
std::cout << "Read failed: " << e.what() << std::endl;
}
return 0;
}
import org.itk.simple.*;
class ImageIOSelection {
public static void main(String argv[]) {
if ( argv.length < 1 ) {
System.out.println("Usage: java ImageIOSelection image_file_name");
return;
}
org.itk.simple.ImageFileReader reader = new org.itk.simple.ImageFileReader();
// Find out which image IOs are supported
VectorString image_ios;
image_ios = reader.getRegisteredImageIOs();
System.out.print("The supported image IOs are: ");
System.out.println(image_ios);
System.out.println("--------------------");
// Another option is to just print the reader and see which
// IOs are supported
System.out.println(reader.toString());
System.out.println("--------------------");
reader.setImageIO("PNGImageIO");
reader.setFileName(argv[0]);
try {
Image image = reader.execute();
System.out.println("Read image: " + argv[0]);
VectorUInt32 size = image.getSize();
System.out.println("Image size: " + size.get(0) + " " + size.get(1));
} catch(Exception e) {
System.out.println("Read failed: " + e);
}
}
}
local write = io.write
require "SimpleITK"
if #arg < 1 then
print ( "Usage: ImageIOSelection image_file_name" )
os.exit ( 1 )
end
-- Find out which image IOs are supported
reader = SimpleITK.ImageFileReader()
image_ios = reader:GetRegisteredImageIOs()
write("The supported image IOs are: ")
for i=0,image_ios:size()-1 do
write(image_ios[i], " ")
end
write("\n")
print("--------------------")
-- Another option is to just print the reader and see which
-- IOs are supported
print(reader:ToString())
print("--------------------")
-- Force the use of a specific IO.
reader:SetImageIO("PNGImageIO")
-- Remember that Lua arrays are 1-based, and that arg does not contain the application name!
reader:SetFileName ( arg[1] )
-- We need a function to pass to pcall, the Lua error handler.
-- So we embed the reader's Execute method in this function.
function read_execute( reader )
return reader:Execute()
end
-- Call the reader's execute method through Lua's pcall error handler
--
local status, rv = pcall(read_execute, reader)
if status then
-- The reader succeeded
write("Read image: ")
write(arg[1])
write("\n")
image = rv
size = image:GetSize();
print("Image size:", size[0], size[1]);
else
-- The reader failed
write("Read failed: ")
write(err)
write("\n")
end
import sys
import SimpleITK as sitk
if len(sys.argv) < 2:
print("Wrong number of arguments.", file=sys.stderr)
print("Usage: " + __file__ + " image_file_name", file=sys.stderr)
sys.exit(1)
# Find out which image IOs are supported
file_reader = sitk.ImageFileReader()
image_ios_tuple = file_reader.GetRegisteredImageIOs()
print("The supported image IOs are: " + str(image_ios_tuple))
print("-" * 20)
# Another option is to just print the reader and see which
# IOs are supported
print(file_reader)
print("-" * 20)
# Force the use of a specific IO. If the IO doesn't support
# reading the image type it will throw an exception.
file_reader.SetImageIO("PNGImageIO")
file_reader.SetFileName(sys.argv[1])
try:
image = file_reader.Execute()
print("Read image: " + sys.argv[1])
size = image.GetSize()
print("Image size:", size[0], size[1])
except IOError as err:
print("Reading failed: ", err)
sys.exit(1)
sys.exit(0)
library(SimpleITK)
args <- commandArgs( TRUE )
if (length(args) < 1) {
write('Usage arguments: <image_file_name>', stderr())
quit(save="no", status=1)
}
# Find out which image IOs are supported
file_reader <- ImageFileReader()
image_ios <- file_reader$GetRegisteredImageIOs()
cat('The supported image IOs are: ', image_ios, '\n')
cat(rep('-',20),'\n', sep='')
# Another option is to just print the reader and see which
# IOs are supported
print(file_reader)
cat(rep('-',20),'\n', sep='')
# Force the use of a specific IO.
file_reader$SetImageIO('PNGImageIO')
file_reader$SetFileName(args[1])
# If the IO doesn't support reading the image type it
# will throw an exception.
image <- tryCatch(file_reader$Execute(),
warning = function(err) {
message(err)
quit(save="no", status=1)
}
)
cat('Read image:',args[1],'\n')
size <- image$GetSize()
cat ("Image size:", size[1], size[2])
quit(save="no", status=0)
require 'simpleitk'
if ARGV.length != 1 then
puts "Usage: SimpleGaussian image_input_file";
exit( 1 )
end
# Find out which image IOs are supported
reader = Simpleitk::ImageFileReader.new
reader.set_file_name( ARGV[0] )
image_ios = reader.get_registered_image_ios()
print "The supported image IOs are: "
for i in image_ios do
print i, " "
end
puts "\n--------------------"
# Another option is to just print the reader and see which
# IOs are supported
puts reader.to_string()
puts "\n--------------------"
# Force the use of a specific IO. If the IO doesn't support
# reading the image type it will throw an exception.
reader.set_image_io("PNGImageIO")
begin
image = reader.execute
puts "Read image: " + ARGV[0]
size = image.get_size
puts "Image size: " + size[0] + " " + size[1]
rescue StandardError => e
puts "Read failed: " + e.message
end
if { $argc < 1 } {
puts "Usage: ImageIOSelection image_input_file"
exit 1
}
# Find out which image IOs are supported
ImageFileReader reader
set image_ios [ reader GetRegisteredImageIOs]
puts "The supported image IOs are: $image_ios"
puts "--------------------"
# Another option is to just print the reader and see which
# IOs are supported
puts [reader ToString]
puts "--------------------"
reader SetFileName [ lindex $argv 0 ]
reader SetImageIO "PNGImageIO"
if {[catch {set image [ reader Execute ]} errmsg]} {
puts "Read failed: $errmsg"
}
set size [ $image GetSize ]
puts "Image size: $size"
# Tcl requires explicit cleanup Cleanup
reader -delete
if {[info exists image]} {
$image -delete
}