If someone is interrested, here is the code of a program that takes as input :<br><br>> a repertory of a DICOM series<br>> a volume .MHD having the same resolution, spacing, origin, number of slices,... than the series (containing )<br>
> an output directory<br><br>an that gives, as output, a DICOM Series with the headers of the first DICOM series but with the pixels of the volume MHD<br><br>(It's a modification of the code "Examples/IO/DicomSeriesReadSeriesWrite.cxx")<br>
<br>++<br><br>Stéphane<br><br><br><span style="font-family: courier new,monospace;">#if defined(_MSC_VER)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#pragma warning ( disable : 4786 )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#ifdef __BORLANDC__</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#define ITK_LEAN_AND_MEAN</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#include "itkGDCMImageIO.h"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#include "itkGDCMSeriesFileNames.h"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#include "itkImageSeriesReader.h"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#include "itkImageSeriesWriter.h"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#include <vector></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#include <itksys/SystemTools.hxx></span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">int main( int argc, char* argv[] )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if( argc < 4 )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cerr << "Usage: " << argv[0] << </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> " DicomDirectory_having_good_headers MHD_volume_to_convert_to_DICOMseies OutputDicomDirectory" << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return EXIT_FAILURE;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef signed short PixelType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> const unsigned int Dimension = 3;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::Image< PixelType, Dimension > ImageType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef itk::ImageSeriesReader< ImageType > ReaderType;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::ImageFileReader< ImageType > ReaderMHDType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef itk::GDCMImageIO ImageIOType;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::GDCMSeriesFileNames NamesGeneratorType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ImageIOType::Pointer gdcmIO = ImageIOType::New();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> namesGenerator->SetInputDirectory( argv[1] );</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> const ReaderType::FileNamesContainer & filenames = </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> namesGenerator->GetInputFileNames();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> unsigned int numberOfFilenames = filenames.size();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> std::cout << numberOfFilenames << std::endl; </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for(unsigned int fni = 0; fni<numberOfFilenames; fni++)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cout << "filename # " << fni << " = ";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> std::cout << filenames[fni] << std::endl;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ReaderType::Pointer reader = ReaderType::New();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> reader->SetImageIO( gdcmIO ); // ici on précise qu'on est en train de lire une image au format DICOM</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> reader->SetFileNames( filenames );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> reader->Update();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch (itk::ExceptionObject &excp)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cerr << "Exception thrown while writing the image" << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> std::cerr << excp << std::endl;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return EXIT_FAILURE;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // à partir d'ici, reader->getOutput = le volume chargé en mémoire contenant toutes les slices</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // ici on va loader le volume MHD</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ReaderMHDType::Pointer readerMHD = ReaderMHDType::New();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> readerMHD->SetFileName( argv[2] );</span><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> const char * outputDirectory = argv[3];</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> itksys::SystemTools::MakeDirectory( outputDirectory );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef signed short OutputPixelType;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> const unsigned int OutputDimension = 2;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::Image< OutputPixelType, OutputDimension > Image2DType;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::ImageSeriesWriter< </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ImageType, Image2DType > SeriesWriterType;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> seriesWriter->SetInput( readerMHD->GetOutput() ); // ici on écrit le volume du fichier MHD au lieu de l'original</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> seriesWriter->SetImageIO( gdcmIO ); // ici on précise qu'on va écrire une image au format DICOM</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> namesGenerator->SetOutputDirectory( outputDirectory );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> seriesWriter->SetFileNames( namesGenerator->GetOutputFileNames() );</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> // l'instruction qui suit est très importante, elle copie toutes les infos provenant des headers DICOM (renseignements patients,...)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> // et ici, on prend bien le bon reader (celui de la série DICOM qu'on a chargé au tout début)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> seriesWriter->SetMetaDataDictionaryArray( </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> reader->GetMetaDataDictionaryArray() );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> seriesWriter->Update();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> catch( itk::ExceptionObject & excp )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cerr << "Exception thrown while writing the series " << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> std::cerr << excp << std::endl;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return EXIT_FAILURE;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return EXIT_SUCCESS;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br>