From Ohm

Dicom Writer with ITK

Write Multi-Frame Dicom (as file “out.dcm”):

#include "itkImageFileWriter.h"

typedef itk::ImageFileWriter<Image3DType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("out.dcm");
writer->SetInput(filter->GetOutput());
writer->Update();

Write Dicom Image Series (to directory “out”):

#include "itkGDCMImageIO.h"
#include "itkNumericSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"

// image types
typedef unsigned short PixelType;
typedef itk::Image<PixelType, 2> Image2DType;
typedef itk::Image<PixelType, 3> Image3DType, ImageType;

// make the output directory and generate the file names
itksys::SystemTools::MakeDirectory("out");

// get image size
reader->Update();
Image3DType::Pointer image = reader->GetOutput();
Image3DType::RegionType region = image->GetLargestPossibleRegion();
Image3DType::SizeType size = region.GetSize();

// generate the file names
typedef itk::NumericSeriesFileNames OutputNamesGeneratorType;
OutputNamesGeneratorType::Pointer outputNames = OutputNamesGeneratorType::New();
std::string seriesFormat = std::string("out/") + "image-%05d.dcm";
outputNames->SetSeriesFormat(seriesFormat.c_str());
outputNames->SetStartIndex(1);
outputNames->SetEndIndex(size[2]);

// create dicom series writer
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
typedef itk::ImageSeriesWriter< Image3DType, Image2DType > SeriesWriterType;
SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
seriesWriter->SetInput(filter->GetOutput());
seriesWriter->SetImageIO(dicomIO);
seriesWriter->SetFileNames(outputNames->GetFileNames());

// copy metadata dictionary from series reader
seriesWriter->SetMetaDataDictionaryArray(reader->GetMetaDataDictionaryArray());

// trigger series writer
try
{
   seriesWriter->Update();
}
catch(itk::ExceptionObject & excp)
{
   std::cerr << "Exception thrown while writing the series" << std::endl;
   std::cerr << excp << std::endl;

   return(EXIT_FAILURE);
}

Retrieved from http://schorsch.efi.fh-nuernberg.de/roettger/index.php/MedicalVisualization/DicomWriterWithITK

Page last modified on November 16, 2020, at 11:40 PM