# Reading and writing a DICOM slice in ITK with GDCM removes tags

**URL:** https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861
**Category:** Engineering
**Tags:** dicom
**Created:** [April 20, 2018, 3:30pm UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861 "2018-04-20T15:30:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![benpresles](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@benpresles](https://discourse.itk.org/u/benpresles)
#### Post date: [April 20, 2018, 3:30pm UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861/1 "2018-04-20T15:30:51Z")

</div>

Dear ITK users,

I created a simple ITK program that reads a dicom slice and writes it back. The output generated dicom does not contain as many tags as the input dicom. Some tags are missing like 0008,1032. I don’t understand why.  
Here is the source code of the program along with the dicom slice I used to test it and the output generated dicom:

#include “itkImage.h”  
#include   
// ITK  
#include “itkImageFileReader.h”  
#include “itkImageFileWriter.h”  
#include “itkGDCMImageIO.h”

int main( int argc, char\* argv[] )  
{  
if( argc != 2 ) {  
std::cerr \<\< "Usage: "\<\< std::endl;  
std::cerr \<\< argv[0];  
std::cerr \<\< " ";  
std::cerr \<\< std::endl;  
return EXIT\_FAILURE;  
}  
const char\* inputDicomSliceFilename = argv[1];  
typedef itk::Image\< short, 2 \> DicomInputImageType;

```
DicomInputImageType::Pointer image = DicomInputImageType::New();
// Reading input
typedef itk::ImageFileReader< DicomInputImageType > ReaderType;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputDicomSliceFilename);
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader->SetImageIO(gdcmImageIO);
try {
    reader->Update();
}
catch (itk::ExceptionObject & e) {
    std::cerr << "exception in file reader " << std::endl;
    std::cerr << e << std::endl;
}
// Save
typedef itk::ImageFileWriter< DicomInputImageType > writerType;
typename writerType::Pointer writer = writerType::New();
writer->SetFileName("outputDicom");
writer->SetInput(reader->GetOutput());
writer->SetImageIO(gdcmImageIO);
try {
    writer->Update();
}
catch (itk::ExceptionObject & e) {
    std::cerr << "Exception in file writer " << std::endl;
    std::cerr << e << std::endl;
}

return EXIT_SUCCESS;

```

}  
[CT000000AnonI.dcm](https://discourse.itk.org/uploads/default/original/1X/898ba1b44ba0e64f2661daee8511092b5358c2a3.dcm) (534.6 KB)  
[outputDicom](https://discourse.itk.org/uploads/default/original/1X/ea5fa45d49caea7fe6b63e1faac121c540e0ec12) (513.7 KB)

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 20, 2018, 5:18pm UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861/2 "2018-04-20T17:18:48Z")

</div>

Private tags are removed by default. You have to invoke `KeepPrivateTags` or something similar somewhere - perhaps on `gdcmImageIO`.

---

<div class="post-metadata">

### Author: ![benpresles](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@benpresles](https://discourse.itk.org/u/benpresles)
#### Post date: [April 23, 2018, 9:47am UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861/3 "2018-04-23T09:47:01Z")

</div>

Thanks for your reply. I added in my program gdcmImageIO-\>SetLoadPrivateTags(true); and indeed, the output generated dicom has more tags than before. However, some tags from the input dicom are still missing like 0008,1032 SQ (Sequence with undefined length). In itkGDCMImageIO.h, I only see functions to load sequences for GDCM 1.x. Is there any for GDCM 2.x ?

---

<div class="post-metadata">

### Author: ![benpresles](https://discourse.itk.org/letter_avatar_proxy/v4/letter/b/cab0a1/32.png) [@benpresles](https://discourse.itk.org/u/benpresles)
#### Post date: [April 24, 2018, 9:21am UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861/4 "2018-04-24T09:21:51Z")

</div>

I did not manage to find an ITK way to preserve these tags (sequence tags). Therefore, I am using gdcm directly now and it works !

```auto
if( argc != 2 ) {
  std::cerr << "Usage: "<< std::endl;
  std::cerr << argv[0];
  std::cerr << " <InputDicomSlice> ";
  std::cerr << std::endl;
  return EXIT_FAILURE;
}
gdcm::Reader reader;
const char* inputDicomSliceFilename = argv[1];
reader.SetFileName(inputDicomSliceFilename);
if( !reader.Read() )
{
  std::cerr << "Could not read: " << inputDicomSliceFilename << std::endl;
  return 1;
}
gdcm::File &file = reader.GetFile();
gdcm::Writer writer;
writer.CheckFileMetaInformationOff();
writer.SetFileName( "outputDicom" );
writer.SetFile( file );
if( !writer.Write() )
{
  std::cerr << "Could not write: " << "outputDicom" << std::endl;
  return EXIT_FAILURE;
}
return EXIT_SUCCESS;

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 24, 2018, 1:53pm UTC](https://discourse.itk.org/t/reading-and-writing-a-dicom-slice-in-itk-with-gdcm-removes-tags/861/5 "2018-04-24T13:53:30Z")

</div>

Thanks for sharing your solution!
