Hi,<br><br>Since this is not shown in the ITK software guide,<br>I thought this code that I wrote based on <a href="http://www.paraview.org/Bug/view.php?id=6258&amp;nbn=1">http://www.paraview.org/Bug/view.php?id=6258&amp;nbn=1</a> ,  might help someone:<br>
It shows how to change a MetaDataDictionaryArray when dealing with changing header info in Dicom series.<br><br>Thanks,<br>John<br><br>#if defined(_MSC_VER)<br>#pragma warning ( disable : 4786 )<br>#endif<br><br>#ifdef __BORLANDC__<br>
#define ITK_LEAN_AND_MEAN<br>#endif<br><br>//<br>//  This example illustrates how to change a MetaDataDictionaryArray.<br>//  This example illustrates how to read two DICOM series, adds key <br>//  tags that are in the second DICOM series to the first DICOM series<br>
//  and write it back with some changed header information as another <br>//  DICOM series.  This can be useful when sometimes conversion between<br>//  different file formats as between NIFTI and DICOM and some dicom tags <br>
//  are lost.<br>//  The keys are defined in the file<br>//<br>//  \code{Insight/Utilities/gdcm/Dicts/dicomV3.dic}<br>//  <br>//  Please note that modifying the content of a DICOM header is a very risky<br>//  operation. The Header contains fundamental information about the patient<br>
//  and therefore its consistency must be protected from any data corruption.<br>//  Before attempting to modify the DICOM headers of your files, you must make<br>//  sure that you have a very good reason for doing so, and that you can ensure<br>
//  that this information change will not result in a lower quality of health<br>//  care to be delivered to the patient.<br>//<br>//  \index{DICOM!Changing Headers}<br>//<br><br>// Software Guide : BeginLatex<br>// <br>// We must start by including the relevant header files. Here we include the<br>
// image reader, image writer, the image, the Meta data dictionary and its<br>// entries the Meta data objects and the GDCMImageIO. The Meta data dictionary<br>// is the data container that stores all the entries from the DICOM header once<br>
// the DICOM image file is read into an ITK image.<br>//<br>// Software Guide : EndLatex<br><br>// Software Guide : BeginCodeSnippet<br>#include &quot;itkImageFileReader.h&quot;<br>#include &quot;itkImageFileWriter.h&quot;<br>
#include &quot;itkImage.h&quot;<br>#include &quot;itkMetaDataDictionary.h&quot;<br>#include &quot;itkMetaDataObject.h&quot;<br>#include &quot;itkGDCMImageIO.h&quot;<br>// Software Guide : EndCodeSnippet<br><br>#include &lt;list&gt;<br>
#include &lt;fstream&gt;<br><br><br>#include &quot;itkGDCMImageIO.h&quot;<br>#include &quot;itkGDCMSeriesFileNames.h&quot;<br>#include &quot;itkImageSeriesReader.h&quot;<br>#include &quot;itkImageSeriesWriter.h&quot;<br><br>
<br>int main(int argc, char* argv[])<br>{<br><br>  if( argc &lt; 4 )<br>    {<br>      std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; OriginalDicomImageDIR ProcessedDicomImageDIR OutputDicomImageDIR\n&quot;;<br>
      //DIR means Directory<br><br><br>    return EXIT_FAILURE;<br>    }<br> <br>  typedef unsigned short InputPixelType;<br><br>  <br>  const unsigned int   Dimension = 3;<br><br>  typedef itk::Image&lt; InputPixelType, Dimension &gt; InputImageType;<br>
<br>  typedef itk::GDCMSeriesFileNames                NamesGeneratorTypeOrig;<br>  NamesGeneratorTypeOrig::Pointer namesGeneratororig = NamesGeneratorTypeOrig::New();<br><br>  namesGeneratororig-&gt;SetInputDirectory( argv[1] );<br>
<br>  typedef itk::Image&lt; InputPixelType, Dimension &gt;      ImageTypeOrig;<br>  typedef itk::ImageSeriesReader&lt; ImageTypeOrig &gt;     ReaderTypeOrig;<br><br>  const ReaderTypeOrig::FileNamesContainer &amp; filenamesorig = <br>
                            namesGeneratororig-&gt;GetInputFileNames();<br><br>  unsigned int numberOfFilenames =  filenamesorig.size();<br>  std::cout &lt;&lt; numberOfFilenames &lt;&lt; std::endl; <br>  for(unsigned int fni = 0; fni&lt;numberOfFilenames; fni++)<br>
    {<br>    std::cout &lt;&lt; &quot;filename # &quot; &lt;&lt; fni &lt;&lt; &quot; = &quot;;<br>    std::cout &lt;&lt; filenamesorig[fni] &lt;&lt; std::endl;<br>    }<br><br>  typedef itk::GDCMImageIO           ImageIOTypeOrig;<br>
  ImageIOTypeOrig::Pointer gdcmImageIOorig = ImageIOTypeOrig::New();<br>  //reader-&gt;SetImageIO( gdcmImageIOorig );<br><br>  ReaderTypeOrig::Pointer readerorig = ReaderTypeOrig::New();<br><br>  readerorig-&gt;SetImageIO( gdcmImageIOorig );<br>
  readerorig-&gt;SetFileNames( filenamesorig );<br><br>  //end of added code<br><br>// Software Guide : BeginLatex<br>// <br>// The reading of the image is triggered by invoking \code{Update()} in the<br>// reader.<br>//<br>
// Software Guide : EndLatex<br><br><br>  try<br>    {<br>// Software Guide : BeginCodeSnippet<br>    readerorig-&gt;Update();<br>// Software Guide : EndCodeSnippet<br>    }<br>  catch (itk::ExceptionObject &amp; e)<br>    {<br>
    std::cerr &lt;&lt; &quot;exception in file reader &quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; e.GetDescription() &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; e.GetLocation() &lt;&lt; std::endl;<br>    return EXIT_FAILURE;<br>
    }<br><br>  typedef itk::Image&lt; InputPixelType, Dimension &gt; InputImageType2;<br><br>  typedef itk::GDCMSeriesFileNames                NamesGeneratorTypeProc;<br>  NamesGeneratorTypeProc::Pointer namesGeneratorproc = NamesGeneratorTypeProc::New();<br>
<br>  namesGeneratorproc-&gt;SetInputDirectory( argv[2] );<br><br>  typedef itk::Image&lt; InputPixelType, Dimension &gt;      ImageTypeProc;<br>  typedef itk::ImageSeriesReader&lt; ImageTypeProc &gt;     ReaderTypeProc;<br>
<br>  const ReaderTypeProc::FileNamesContainer &amp; filenamesproc = <br>                            namesGeneratorproc-&gt;GetInputFileNames();<br>  <br>  numberOfFilenames =  filenamesproc.size();<br>  std::cout &lt;&lt; numberOfFilenames &lt;&lt; std::endl; <br>
  for(unsigned int fni = 0; fni&lt;numberOfFilenames; fni++)<br>    {<br>    std::cout &lt;&lt; &quot;filename # &quot; &lt;&lt; fni &lt;&lt; &quot; = &quot;;<br>    std::cout &lt;&lt; filenamesproc[fni] &lt;&lt; std::endl;<br>
    }<br><br>  typedef itk::GDCMImageIO           ImageIOTypeProc;<br>  ImageIOTypeProc::Pointer gdcmImageIOproc = ImageIOTypeProc::New();<br><br>  ReaderTypeProc::Pointer readerproc = ReaderTypeProc::New();<br><br>  readerproc-&gt;SetImageIO( gdcmImageIOproc );<br>
  readerproc-&gt;SetFileNames( filenamesproc );<br><br>  try<br>    {<br>    readerproc-&gt;Update();<br>    }<br>  catch (itk::ExceptionObject &amp; e)<br>    {<br>    std::cerr &lt;&lt; &quot;exception in file reader &quot; &lt;&lt; std::endl;<br>
    std::cerr &lt;&lt; e.GetDescription() &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; e.GetLocation() &lt;&lt; std::endl;<br>    return EXIT_FAILURE;<br>    }<br><br>  typedef itk::MetaDataObject&lt; std::string &gt; MetaDataStringType;<br>
  <br>  typedef itk::MetaDataDictionary   DictionaryType1b;<br>  typedef itk::MetaDataDictionary   DictionaryType2b;<br>  <br>  std::vector&lt;DictionaryType2b*&gt;         pdictArray2b;<br><br>  DictionaryType1b::ConstIterator itr1b;<br>
  DictionaryType1b::ConstIterator end1b;<br><br>  DictionaryType2b::ConstIterator itr2b;<br>  DictionaryType2b::ConstIterator end2b;<br>  <br>  for ( unsigned int idx = 0;<br>                  idx &lt; readerproc-&gt;GetMetaDataDictionaryArray()-&gt;size();<br>
                  idx++ )<br>            {<br><br>                itk::MetaDataDictionary* pdictionary1b = new itk::MetaDataDictionary;<br>                itk::MetaDataDictionary* pSrc1b = (*(readerorig-&gt;GetMetaDataDictionaryArray()))[idx];<br>
<br>        itk::MetaDataDictionary* pdictionary2b = new itk::MetaDataDictionary;<br>                itk::MetaDataDictionary* pSrc2b = (*(readerproc-&gt;GetMetaDataDictionaryArray()))[idx];<br><br>                if ( pSrc2b )<br>
                {<br>          *pdictionary1b = (*pSrc1b);<br>          *pdictionary2b = (*pSrc2b);<br><br><br>          itr1b = pdictionary1b-&gt;Begin();<br>          end1b = pdictionary1b-&gt;End();<br><br>          itr2b = pdictionary2b-&gt;Begin();<br>
          end2b = pdictionary2b-&gt;End();<br>          <br>          while( itr1b != end1b )<br>            {<br>              itk::MetaDataObjectBase::Pointer  entry1b = itr1b-&gt;second;<br><br>              MetaDataStringType::Pointer entryvalue1b = <br>
            dynamic_cast&lt;MetaDataStringType *&gt;( entry1b.GetPointer() );<br><br>              std::string tagkey1b   = itr1b-&gt;first;<br><br>    <br>              DictionaryType2b::ConstIterator tagItr2b = pdictionary2b-&gt;Find( tagkey1b );<br>
    <br>              if( tagItr2b == end2b )<br>            {<br>              std::string tagkey   = itr1b-&gt;first;<br>              std::string tagvalue = entryvalue1b-&gt;GetMetaDataObjectValue();<br>              //std::cout &lt;&lt; tagkey &lt;&lt;  &quot; = &quot; &lt;&lt; tagvalue &lt;&lt; std::endl;<br>
              itk::EncapsulateMetaData&lt;std::string&gt;( *pdictionary2b, tagkey , tagvalue ); <br>            }<br>              <br>              ++itr1b;<br>            }<br>          <br>  <br>          std::string entryIdMR = &quot;0008|0060&quot;;<br>
          std::string valueMR = &quot;MR&quot;;<br>          itk::EncapsulateMetaData&lt;std::string&gt;( *pdictionary2b, entryIdMR, valueMR );<br><br>                    <br>          pdictArray2b.push_back( pdictionary2b );<br>
          <br>                }<br>            }<br><br>  typedef unsigned short    OutputPixelType;<br>  const unsigned int      OutputDimension = 2;<br><br>  typedef itk::Image&lt; OutputPixelType, OutputDimension &gt;    Image2DType;<br>
<br>  typedef itk::ImageSeriesWriter&lt; <br>  ImageTypeProc, Image2DType &gt;  SeriesWriterType;<br><br>  SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();<br><br>  seriesWriter-&gt;SetMetaDataDictionaryArray( &amp;pdictArray2b );<br>
    <br>  seriesWriter-&gt;SetInput( readerproc-&gt;GetOutput() );<br>  seriesWriter-&gt;SetImageIO( gdcmImageIOproc );<br><br>  namesGeneratorproc-&gt;SetOutputDirectory( argv[3] );<br><br>  seriesWriter-&gt;SetFileNames( namesGeneratorproc-&gt;GetOutputFileNames() );<br>
<br>  try<br>    {<br>    seriesWriter-&gt;Update();<br>    }<br>  catch (itk::ExceptionObject &amp; e)<br>    {<br>    std::cerr &lt;&lt; &quot;exception in file writer &quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; e.GetDescription() &lt;&lt; std::endl;<br>
    std::cerr &lt;&lt; e.GetLocation() &lt;&lt; std::endl;<br>    return EXIT_FAILURE;<br>    }<br><br>  // Software Guide : BeginLatex<br>  // <br>  // Remember again, that modifying the header entries of a DICOM file involves<br>
  // very serious risks for patients and therefore must be done with extreme<br>  // caution.<br>  //<br>  // Software Guide : EndLatex<br><br><br>  return EXIT_SUCCESS;<br><br>}<br><br><br>Thanks,<br>John<br>