Dicom Series Reader
Overview
This example illustrates how to read a DICOM series into a 3D volume. Additional actions include printing some information, writing the image and possibly displaying it using the default display program via the SimpleITK Show function. The program makes several assumptions: the given directory contains at least one DICOM series, if there is more than one series the first series is read, and the default SimpleITK external viewer is installed.
See also Dicom Series Read Modify Write, Read Image Meta-Data Dictionary and Print.
Code
using System;
using System.Diagnostics;
using itk.simple;
using sitk = itk.simple.SimpleITK;
namespace itk.simple.examples {
class DicomSeriesReader {
static void Main(string[] args) {
try {
if (args.Length < 2) {
Console.WriteLine( "Usage: DicomSeriesReader <input_directory> <output_file>" );
return;
}
Console.WriteLine( "Reading Dicom directory: " + args[0] );
ImageSeriesReader reader = new ImageSeriesReader();
VectorString dicom_names = ImageSeriesReader.GetGDCMSeriesFileNames( args[0] );
reader.SetFileNames( dicom_names );
Image image = reader.Execute();
VectorUInt32 size = image.GetSize();
Console.WriteLine( "Image size: " + size[0] + " " + size[1] + " " + size[2] );
Console.WriteLine( "Writing image: " + args[1] );
ImageFileWriter writer = new ImageFileWriter();
writer.SetFileName( args[1] );
writer.Execute( image );
if (Environment.GetEnvironmentVariable("SITK_NOSHOW") == null)
SimpleITK.Show( image, "Dicom Series" );
} catch (Exception ex) {
Console.WriteLine( "Usage: DicomSeriesReader <input_directory> <output_file>" );
Console.WriteLine( ex );
}
}
}
}
// This one header will include all SimpleITK filters and external
// objects.
#include <SimpleITK.h>
#include <iostream>
#include <stdlib.h>
// create convenient namespace alias
namespace sitk = itk::simple;
int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Usage: " << argv[0] << " <input_directory> <output_file>\n";
return 1;
}
std::cout << "Reading Dicom directory: " << argv[1] << std::endl;
// Read the Dicom image series
sitk::ImageSeriesReader reader;
auto dicom_names = sitk::ImageSeriesReader::GetGDCMSeriesFileNames(argv[1]);
reader.SetFileNames(dicom_names);
sitk::Image image = reader.Execute();
std::vector<unsigned int> size = image.GetSize();
std::cout << "Image size: " << size[0] << " " << size[1] << " " << size[2] << std::endl;
std::cout << "Writing " << argv[2] << std::endl;
sitk::WriteImage(image, argv[2]);
if (getenv("SITK_NOSHOW") == NULL)
sitk::Show(image, "Hello World: C++", true);
return 0;
}
import org.itk.simple.*;
public class DicomSeriesReader {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: DicomSeriesReader <input_directory> <output_file>");
System.exit(1);
}
System.out.println("Reading Dicom directory: " + args[0]);
ImageSeriesReader imageSeriesReader = new ImageSeriesReader();
final VectorString dicomNames = ImageSeriesReader.getGDCMSeriesFileNames(args[0]);
imageSeriesReader.setFileNames(dicomNames);
Image image = imageSeriesReader.execute();
VectorUInt32 size = image.getSize();
System.out.println("Image size: " + size.get(0) + " " + size.get(1) + " " + size.get(2));
System.out.println("Writing " + args[1]);
SimpleITK.writeImage(image, args[1]);
if (System.getenv("SITK_NOSHOW") == null) {
SimpleITK.show( image, "Hello World: Java", true );
}
}
}
require "SimpleITK"
if #arg < 2 then
print ( "Usage: DicomSeriesReader <input_directory> <output_file>" )
os.exit ( 1 )
end
print( "Reading Dicom directory:", arg[1] )
reader = SimpleITK.ImageSeriesReader()
dicom_names = SimpleITK.ImageSeriesReader.GetGDCMSeriesFileNames( arg[1] )
reader:SetFileNames ( dicom_names )
image = reader:Execute();
size = image:GetSize();
print("Image size:", size[0], size[1], size[2]);
print( "Writing image:", arg[2] )
SimpleITK.WriteImage( image, arg[2] )
if os.getenv("SITK_NOSHOW") == nil then
sitk.Show( image, "Hello World: Lua", true )
end
#!/usr/bin/env python
import sys
import os
import SimpleITK as sitk
if len(sys.argv) < 3:
print("Usage: DicomSeriesReader <input_directory> <output_file>")
sys.exit(1)
print("Reading Dicom directory:", sys.argv[1])
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(sys.argv[1])
reader.SetFileNames(dicom_names)
image = reader.Execute()
size = image.GetSize()
print("Image size:", size[0], size[1], size[2])
print("Writing image:", sys.argv[2])
sitk.WriteImage(image, sys.argv[2])
if "SITK_NOSHOW" not in os.environ:
sitk.Show(image, "Dicom Series")
library(SimpleITK)
args <- commandArgs( TRUE )
if (length(args) < 2) {
write("Usage arguments: <input_directory> <output_file>", stderr())
quit(1)
}
cat( "Reading Dicom directory:", args[[1]] )
reader <- ImageSeriesReader()
dicomNames <- ImageSeriesReader_GetGDCMSeriesFileNames(args[[1]])
reader$SetFileNames(dicomNames)
image <- reader$Execute()
size <- image$GetSize()
cat ("Image size:", size[1], size[2], size[3])
cat("Writing image:", args[[2]])
WriteImage(image, args[[2]])
if(Sys.getenv("SITK_NOSHOW") == "") {
Show(image, "Hello World: R", debugOn=TRUE)
}