[Insight-users] convert unsign int pixel type dicom file into MetaImage with gdcm

Hsiang-Chi Kuo kuoxgx at gmail.com
Tue Apr 28 12:39:05 EDT 2009


Hi Luis and Mathieu,
Sorry to trouble you, but I have been spending a whole month and trying to
convert a Dicom RT file (unsign int pixel type) to mhd file with very little
progress.
The Dicom file is a muti fram format with "Grid Frame Offset Vector tag:
(3004,000C)". I used gdcm (1.2) to read the size, spacing, and origin, and
pixel data. Then convert the pixel format into MetaImage.
The line of "  unsigned int *imageData = fh->GetImageDataRaw(); "  output
the error message: 'initializing' : cannot convert from 'uint8_t *' to
'unsigned short *'.
The line of "  itk::ImageRegionIterator< ImageType > it(im,
im->GetLargestPossibleRegion()); " returned : cannot convert from 'unsigned
short *' to 'const unsigned int .
Please take a look at the code and give me an idea to work around it. I
appreciate any of your input and your time.
Howard

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "gdcmFileHelper.h"
#include "gdcmFile.h"
#include <itkImage.h>
#include <itkMetaImageIO.h>
#include <itkImageRegionIterator.h>
#include <stdio.h>  //sscanf
int main( int argc, char* argv[] )
{
  if( argc < 2 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " DicomImage OutputDicomImage ";
    //std::cerr << " OutputImage RescaleDicomImage\n";
    return EXIT_FAILURE;
    }
  typedef unsigned int InputPixelType;
  const unsigned int   InputDimension = 3;
  typedef itk::Image< InputPixelType, InputDimension > InputImageType;
  typedef itk::ImageFileReader< InputImageType > ReaderType;
  gdcm::File *f1 = new gdcm::File();
   f1->SetFileName( argv[1] );
   f1->Load();
   int linesNumber   = f1->GetYSize();
   int rawsNumber    = f1->GetXSize();
   int framesNumber  = f1->GetZSize();// defaulted to 1 if not
found
            float xs = f1->GetXSpacing();
   float ys = f1->GetYSpacing();
   float zs = f1->GetZSpacing();// defaulted to 1.0 if not found strSpacing
(0x3004,0x000c)
            const std::string strSpacing = f1->GetEntryValue(0x3004,0x000c);

            const int MaxZ = 256;
     float sp[MaxZ] ;
               if ( strSpacing != gdcm::GDCM_UNFOUND )
                        {
                                    if ( sscanf( strSpacing.c_str(), "%f \\
%f ", &sp[0], &sp[1]) > 0 )
                                    zs = sp[1]-sp[0];
                        }
   float xo = f1->GetXOrigin();
   float yo = f1->GetYOrigin();
   float zo = f1->GetZOrigin();
            gdcm::FileHelper *fh = new gdcm::FileHelper();
            unsigned int *imageData = fh->GetImageDataRaw();
            typedef itk::Image<unsigned int, 3> ImageType;
  ImageType::SizeType imSize = {{linesNumber, rawsNumber, framesNumber}};
  ImageType::Pointer im = ImageType::New();
  im->SetRegions(imSize);
  im->Allocate();
  // Set some values in the image
  itk::ImageRegionIterator< ImageType > it(im,
        im->GetLargestPossibleRegion());
  it.GoToBegin();
  while(imageData)
    {
    it.Set(imageData );
    ++it;
    }
  typedef itk::ImageFileWriter< ImageType > VolumeWriterType;
  VolumeWriterType::Pointer writer = VolumeWriterType::New();
  itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();
  writer->SetImageIO( metaWriter );
  writer->SetFileName( "test.mhd" );
  writer->SetInput( im );
  writer->Update();
  delete f1;
  delete fh;
  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090428/4a69ace5/attachment.htm>


More information about the Insight-users mailing list