[Insight-users] Writing DICOM metadata

Mario Ceresa mrceresa at gmail.com
Mon Apr 19 13:51:49 EDT 2010


Hello everybody!

I'm sorry to ask a stupid question, but:

How do I copy the DICOM metadata when I copy a serie to a single file?

The approach

writer->SetMetaDataDictionary( dicomIO->GetMetaDataDictionary() );

doesn't work for me.

I looked at the DicomSeriesReadImageWrite? examples but they seem not
to copy the metadata as well.

I attach the utilized code hoping that someone can help me spot the error,

Thanks in advance,

Mario

PS: I compile it with cmake:
ADD_EXECUTABLE(copy_metadata copy_metadata_main.cxx)
TARGET_LINK_LIBRARIES(copy_metadata ITKCommon ITKIO)
-------------- next part --------------


#include "itkGDCMImageIO.h"
#include "itkDICOMSeriesFileNames.h"

#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"

int main( int argc, char * argv[] )
{
 
  char * infilename  = argv[1];

  typedef signed short PixelType;
  const unsigned int Dimension=3;

  typedef unsigned long LabelType;

  typedef itk::Image< PixelType, Dimension >                       	ImageType;
	
  typedef ImageType::IndexType                                     	IndexType;
  typedef ImageType::SizeType                                      	SizeType;
  typedef ImageType::RegionType                                    	OutputImageRegionType;
 
  typedef itk::ImageSeriesReader< ImageType >                      	ReaderType;
	ReaderType::Pointer reader =                                     	ReaderType::New();
 
  typedef itk::ImageFileWriter< ImageType >                        	WriterType;
	WriterType::Pointer writer =                                     	WriterType::New();

//--------- Reading the serie --------------
  
  typedef itk::GDCMImageIO                                         ImageIOType;
  ImageIOType::Pointer dicomIO =                                   ImageIOType::New();
  reader->SetImageIO( dicomIO );

  typedef itk::DICOMSeriesFileNames NamesGeneratorType;
	
  NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
  nameGenerator->SetFileNameSortingOrderToSortByImageNumber();

  nameGenerator->SetDirectory( argv[1] );
  typedef std::vector<std::string> seriesIdContainer;
  const seriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
	
  seriesIdContainer::const_iterator seriesItr = seriesUID.begin();
  seriesIdContainer::const_iterator seriesEnd = seriesUID.end();
  
  std::string seriesIdentifier;
  seriesIdentifier = seriesUID.begin()->c_str();
	
  typedef std::vector<std::string> fileNamesContainer;
  fileNamesContainer fileNames;
  fileNames = nameGenerator->GetFileNames(seriesIdentifier);

  reader->SetFileNames( fileNames );
  reader->SetImageIO( dicomIO );

  try {
    reader->Update();
  } catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught: can't read serie" << std::endl;
    std::cerr << err << std::endl;
    return EXIT_FAILURE;
    }
  


  writer->SetInput( reader->GetOutput() );
  writer->SetFileName("./0input.dcm" );
    
  writer->SetMetaDataDictionary( dicomIO->GetMetaDataDictionary() );

  
  try
    {
  	writer->Update();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught: can't write back serie to a single file" << std::endl;
    std::cerr << err << std::endl;
    return EXIT_FAILURE;
    }

	
   
  return EXIT_SUCCESS;
  
}


More information about the Insight-users mailing list