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

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 (534.6 KB)
outputDicom (513.7 KB)

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

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 ?

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 !

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;
1 Like

Thanks for sharing your solution!