[Dcmlib] [Gdcm2] Problem on DICOM images written by ITK GDCM

jiafucang jiafucang at asisz.com
Tue Nov 21 07:24:55 CET 2006


Hello, Mathieu,

I use ITK CVS GDCM to write DICOM images, but the DICOM file 
can not be read by several DICOM compatible software, such
as MIPAV 3.0 (http://mipav.cit.nih.gov) , XMedcon 0.9.9.3
, eFilm 2.0.0, BrainLAB surgery plan or Stryker navigation software.
But it can be read by MRICro 1.40build 1 and Irfanview.

My intention is wrote fMRI statistical activation images computed by
SPM or AFNI into DICOM images for use in image navigation system.
So I use the original DICOM infomation to construct the new generated
DICOM image. The DICOM image is in RGB color  format.

The code is as follows:

#include "itkImageFileReader.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkOrientedImage.h"
#include "itkMetaDataObject.h"
#include "itkOrientImageFilter.h"
#include "itkRGBPixel.h"

#include <vector>
#include <itksys/SystemTools.hxx>

int main( int argc, char* argv[] )
{
  if( argc < 1 )
    {
    std::cerr << "Usage: " << argv[0] << 
      " DicomDirectory  OutputDicomDirectory" << std::endl;
    return EXIT_FAILURE;
    }


  typedef itk::RGBPixel<unsigned char>    RGBPixelType;
  const unsigned int      Dimension = 3;

  typedef itk::OrientedImage< RGBPixelType, Dimension >      RGBImageType;
  typedef itk::ImageFileReader< RGBImageType >       RGBReaderType;

  RGBReaderType::Pointer rgbreader = RGBReaderType::New();

  rgbreader->SetFileName( "E:/RGBfMRIDICOM/3drgb.mhd" );

  try
    {
    rgbreader->Update();
    }
  catch (itk::ExceptionObject &excp)
    {
    std::cerr << "Exception thrown while writing the image" << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


  typedef signed short    PixelType;
  

  typedef itk::Image< PixelType, Dimension >      ImageType;
  typedef itk::ImageSeriesReader< ImageType >     ReaderType;

  typedef itk::GDCMImageIO                        ImageIOType;
  typedef itk::GDCMSeriesFileNames                NamesGeneratorType;

  ImageIOType::Pointer gdcmIO = ImageIOType::New();
  NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();

  namesGenerator->SetInputDirectory( "E:/RGBfMRIDICOM/014_3D_SPGR" );

  const ReaderType::FileNamesContainer & filenames = 
                            namesGenerator->GetInputFileNames();


  unsigned int numberOfFilenames =  filenames.size();
  std::cout << numberOfFilenames << std::endl; 
  for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
    {
    std::cout << "filename # " << fni << " = ";
    std::cout << filenames[fni] << std::endl;
    }
 
  ReaderType::Pointer reader = ReaderType::New();

  reader->SetImageIO( gdcmIO );
  reader->SetFileNames( filenames );

  try
    {
    reader->Update();
    }
  catch (itk::ExceptionObject &excp)
    {
    std::cerr << "Exception thrown while writing the image" << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


 
  typedef itk::GDCMImageIO                        ImageIOType;
 

  ImageIOType::Pointer rgbgdcmIO = ImageIOType::New();

  const char * outputDirectory = "E:/RGBfMRIDICOM/outputrgb";

  itksys::SystemTools::MakeDirectory( outputDirectory );


  typedef itk::RGBPixel<unsigned char>    OutputPixelType;
  const unsigned int      OutputDimension = 2;

  typedef itk::OrientedImage< OutputPixelType, OutputDimension >    Image2DType;

  typedef itk::ImageSeriesWriter< 
                         RGBImageType, Image2DType >  SeriesWriterType;

  typedef itk::NumericSeriesFileNames                NamesGeneratorType2;
  NamesGeneratorType2::Pointer namesGenerator2 = NamesGeneratorType2::New();

  itk::MetaDataDictionary & dict = gdcmIO->GetMetaDataDictionary();
  std::string tagkey, value;
  tagkey = "0008|0060"; // Modality
  value = "MRI";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value );
  tagkey = "0008|0008"; // Image Type
  value = "DERIVED\\SECONDARY";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);
  tagkey = "0008|0064"; // Conversion Type
  value = "WSD";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);

  tagkey = "0010|0010";  // Patient's Name
  value  = "fMRI result";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);


  tagkey = "0008|0070"; // Manufacturer
  value  = "Alekta Medical Instruments Co., Ltd.";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);


  tagkey = "0010|0020"; // Patient ID
  value  = "12343567890";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);

  
  tagkey = "0008|0080"; //Institution Name
  value  = "Alekta Medical Instruments Co. Ltd.";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);

  tagkey = "0008|0090";
  value  = "Jia Fucang";
  itk::EncapsulateMetaData<std::string>(dict, tagkey, value);


  SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();

  RGBImageType::Pointer image = rgbreader->GetOutput();

  RGBImageType::DirectionType direction;
  direction(0,0) = 1; 
  direction(0,1) = 0;
  direction(0,2) = 0;
  direction(1,0) = 0;
  direction(1,1) = 1;
  direction(1,2) = 0;
  direction(2,0) = 0;
  direction(2,1) = 0;
  direction(2,2) = 1;
 
  image->SetDirection(direction);

  image->Update();
  

   itk::OrientImageFilter<RGBImageType,RGBImageType>::Pointer orienter =
     itk::OrientImageFilter<RGBImageType,RGBImageType>::New();
   orienter->UseImageDirectionOn();
   orienter->SetDesiredCoordinateOrientation(itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RAI);
   orienter->SetInput(image);
   orienter->Update();


  seriesWriter->SetInput( orienter->GetOutput() );
  seriesWriter->SetImageIO( rgbgdcmIO );
  seriesWriter->SetMetaDataDictionaryArray( 
                        reader->GetMetaDataDictionaryArray() );

  RGBImageType::RegionType region = 
     rgbreader->GetOutput()->GetLargestPossibleRegion();

  RGBImageType::IndexType start = region.GetIndex();
  RGBImageType::SizeType  size  = region.GetSize();


  std::string format = outputDirectory;
  
  format += "/%04d.dcm";
  
  namesGenerator2->SetSeriesFormat( format.c_str() );

  namesGenerator2->SetStartIndex( start[2] + 1 );
  namesGenerator2->SetEndIndex( start[2] + size[2] );
  namesGenerator2->SetIncrementIndex( 1 );


  seriesWriter->SetFileNames( namesGenerator2->GetFileNames() );

  
  try
    {
    seriesWriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Exception thrown while writing the series " << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }


  return 0;
}


I attached a DICOM file, if any other images are needed, I can upload them.

Thank you for any hints.

Best,
Fucang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.creatis.insa-lyon.fr/pipermail/dcmlib/attachments/20061121/bd3dc4d7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0070.dcm.gz
Type: application/octet-stream
Size: 1456 bytes
Desc: not available
URL: <http://www.creatis.insa-lyon.fr/pipermail/dcmlib/attachments/20061121/bd3dc4d7/attachment.obj>
-------------- next part --------------
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
-------------- next part --------------
_______________________________________________
Gdcm-developers mailing list
Gdcm-developers at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gdcm-developers


More information about the Dcmlib mailing list