[Insight-users] incorrect z-spacing in GDCM reading: oops

Mathieu Malaterre mathieu.malaterre at kitware.com
Wed May 11 14:53:40 EDT 2005


George,

	Thanks for your comment, but the solution proposed indeed take care of 
any number of space (and even tabulation !). Just compile the simple 
program I just sent with:

  std::string s = "0.100000               \\0.200000\t\\0.300000  ";
                                                    ^tabulation
  sscanf( s.c_str(), "%f \\%f \\%f ", &x, &y, &z)
                        ^only one space here

sscanf will return that it sucessfully read 3 float.

Regards,
Mathieu



Li, George (NIH/NCI) wrote:
> Mathieu:
> 
> You are right, many of the C functions are not thread safe, and it 
> does follow the iostream, the C++ I/O style.
> 
> In dicom, there is no specification that indicates what format for 
> the coordinates, as long as the length of the string is within the 
> character limit. Therefore, chances are someone might place numbers 
> that happen to be with 2 or more spaces in between. Anyway, this is 
> just a concern, and it would be better fixed once for all.
> 
> Thanks for your efforts in addressing my question.
> 
> Regards,
> 
> George
> 
> 
> 
> -----Original Message-----
> From: Mathieu Malaterre [mailto:mathieu.malaterre at kitware.com] 
> Sent: Wednesday, May 11, 2005 2:27 PM
> To: Li, George (NIH/NCI)
> Cc: Miller, James V (Research); insight-users at itk.org; Luis Ibanez
> Subject: Re: [Insight-users] incorrect z-spacing in GDCM reading: oops
> 
> 
> George,
> 
> 	strtok is definitely one function I would strongly suggest not to
> use. 
> Not only does it modify your input (so you cannot even use 
> string::c_str()) but also it is not thread safe.
> 
> Anyway a nicer patch would be:
> 
> #include <iostream>
> 
> int main()
> {
>    float x,y,z;
>    std::string s = "0.100000  \\0.200000\t\\0.300000  ";
>    std::cout <<   sscanf( s.c_str(), "%f \\%f \\%f ", &x, &y, &z) << 
> std::endl;
>    std::cout << x << "," << y << "," << z << std::endl;
>    return 1;
> }
> 
> 
> Seems to solve the problem. I am committing it to both gdcm CVS/ gdcm 
> 1.0 and ITK
> 
> Thanks,
> Mathieu
> 
> Li, George (NIH/NCI) wrote:
> 
>>Mathieu and Jim:
>>
>>The dicom file is generated from my own program, which can communicate
>>with several commercial treatment planning software, where dicom is used
>>as standard image format. 
>>
>>For more robust I/O, I would suggest to use fgets() then parse the 
>>entire
>>line with strtok() with "\\" as the deliminator. They are all C function 
>>calls though. Therefore, a slight format change in the file won't corrupt 
>>the reading.
>>
>>Of course, it takes the performance as the expense. But, it does not 
>>cost
>>much.
>>
>>Regards,
>>
>>George
>>
>>
>>-----Original Message-----
>>From: Mathieu Malaterre [mailto:mathieu.malaterre at kitware.com]
>>Sent: Wednesday, May 11, 2005 1:53 PM
>>To: Miller, James V (Research)
>>Cc: Li, George (NIH/NCI); insight-users at itk.org; Luis Ibanez
>>Subject: Re: [Insight-users] incorrect z-spacing in GDCM reading: oops
>>
>>
>>Jim,
>>
>>	Ok my mistake you are right sscanf is failing on that:
>>
>>#include <iostream>
>>
>>int main()
>>{
>>   float x,y,z;
>>   std::string s = "0.000000  \\0.000000  \\0.000000  ";
>>   std::cout <<   sscanf( s.c_str(), "%f\\%f\\%f", &x, &y, &z) <<
> 
> std::endl;
> 
>>   return 1;
>>}
>>
>>It only reads the first floating point value...
>>
>>
>>George where did you get this DICOM file ?
>>
>>I'll convert the sscanf to a more robust stringstream approach,
>>
>>
>>Any suggestion welcome,
>>Mathieu
>>
>>
>>Miller, James V (Research) wrote:
>>
>>
>>>It looks like the ImagePositionPatients in these headers
>>>are formatted like
>>>
>>>0.00000 \0.0000 \0.0000
>>>
>>>with a space before the "\".  Normally, these fields appears as
>>>
>>>0.0000\0.0000\0.0000
>>>
>>>I have never seen spaces before the "\", does DICOM allow this?
>>>
>>>Jim
>>>
>>>-----Original Message-----
>>>From: insight-users-bounces+millerjv=crd.ge.com at itk.org
>>>[mailto:insight-users-bounces+millerjv=crd.ge.com at itk.org]On Behalf Of
>>>Li, George (NIH/NCI)
>>>Sent: Wednesday, May 11, 2005 12:44 PM
>>>To: Li, George (NIH/NCI); 'Mathieu Malaterre'
>>>Cc: 'insight-users at itk.org'; 'Luis Ibanez'
>>>Subject: RE: [Insight-users] incorrect z-spacing in GDCM reading
>>>
>>>
>>>Mathieu:
>>>
>>>Here are the dicom header information. The strings are direct dump, So
>>>should provide some info on the images. Hopefully, you can tell Me an 
>>>alternative way to send images to you and test them from your End.
>>>
>>>Thanks,
>>>
>>>George
>>>
>>>
>>>-----Original Message-----
>>>From: Li, George (NIH/NCI)
>>>Sent: Wednesday, May 11, 2005 12:38 PM
>>>To: 'Mathieu Malaterre'
>>>Cc: 'insight-users at itk.org'; 'Luis Ibanez'
>>>Subject: RE: [Insight-users] incorrect z-spacing in GDCM reading
>>>
>>>
>>>
>>>Mathieu:
>>>
>>>I cannot send the 2 images as attachment, because they are 205KB each.
>>>I got a message from ITK, saying that there is limit of 40KB per mail 
>>>And my following mail was discarded.
>>>
>>>Any other way I can send the images?
>>>
>>>Thanks,
>>>
>>>George
>>>
>>>
>>>
>>>-----Original Message-----
>>>From: Li, George (NIH/NCI)
>>>Sent: Wednesday, May 11, 2005 12:32 PM
>>>To: 'Mathieu Malaterre'
>>>Cc: 'Luis Ibanez'; insight-users at itk.org
>>>Subject: RE: [Insight-users] incorrect z-spacing in GDCM reading
>>>
>>>
>>>Thanks, Mathieu:
>>>
>>>I have included two image slices, together with their header
>>>information,
>>>which is printed into text files from one of my own program. Actually, the
>>
>>
>>>dicom image files are generated from my program, too. Theoretically, 
>>>the
>>>syntax should follow dicom3 definition on 'Image Position Patient' /
>>
>>'Image
>>
>>
>>>Orientation Patient'. I did not check how GDCM parses the string.
>>>
>>>The spacing should be 3.4mm, but GDCM got 1.00mm.
>>>
>>>Look forward to hearing from you.
>>>
>>>Thanks.
>>>
>>>George
>>>
>>>
>>>-----Original Message-----
>>>From: Mathieu Malaterre [mailto:mathieu.malaterre at kitware.com]
>>>Sent: Wednesday, May 11, 2005 12:00 PM
>>>To: Li, George (NIH/NCI)
>>>Cc: 'Luis Ibanez'; insight-users at itk.org
>>>Subject: Re: [Insight-users] incorrect z-spacing in GDCM reading
>>>
>>>
>>>George,
>>>
>>>	Your code looks fine. The algorithm for finding the z spacing is
>>
>>kind
>>
>>
>>>of tricky. So I am wondering if:
>>>
>>>1. Your images might not have proper 'Image Position Patient' / 'Image 
>>>Orientation Patient'
>>>
>>>2. If they have, maybe gdcm is not able to parse the string properly.
>>>
>>>Could it be possible that you send me at least two images from this 
>>>dataset ?
>>>
>>>If not, you'll have to turn the code to be more verbose in particular 
>>>when entering the function:
>>>
>>>
>>>    gdcm::SerieHelper::ImagePositionPatientOrdering
>>>
>>>Thanks,
>>>Mathieu
>>>
>>>Li, George (NIH/NCI) wrote:
>>>
>>>
>>>
>>>>Hi, Luis and ITK users:
>>>>
>>>>I recently tried to adopt the GDCM dicom code for loading dicom 
>>>>series
>>>>images, and then consequently saving images in dicom series. However, 
>>>>when I read the images, the spacing in z-direction is incorrect, 
>>>>leading to a compressed image
>>>>in z-direction. Here is the related code, could you help me to identify 
>>>>anything
>>>>wrong?
>>>>
>>>>class CMDIDataITKIO
>>>>{
>>>>public:
>>>>CMDIDataITKIO();
>>>>virtual ~CMDIDataITKIO();
>>>>public:
>>>>void ReadGDCM_DicomImages(const char*, SeriesReaderType::Pointer);  
>>>>void WriteGDCM_DicomImages(const char*, ImageType::Pointer);
>>>>private:
>>>>// initiate in the ctor, and raise clean flag in dtor.  
>>>>GDCMImageIOType::Pointer m_gdcmIO;  GDCMSeriesNames::Pointer 
>>>>m_gdcmNames;  SeriesReaderType::Pointer m_gdcmReader;
>>>>}
>>>>
>>>>
>>>>void CMDIDataITKIO::ReadGDCM_DicomImages(const char* folderName,
>>>>                                                                     
>>>>SeriesReaderType::Pointer reader)
>>>>{
>>>>if(!m_gdcmIO) m_gdcmIO = GDCMImageIOType::New();
>>>>if(!m_gdcmNames) m_gdcmNames = GDCMSeriesNames::New();
>>>>
>>>>// Get the DICOM filenames from the directory 
>>>>m_gdcmNames->SetInputDirectory(folderName);
>>>>
>>>>reader->SetFileNames( m_gdcmNames->GetInputFileNames() ); SetImageIO( 
>>>>reader->m_gdcmIO );
>>>>
>>>>try {
>>>>reader->Update();
>>>>  }
>>>>catch (itk::ExceptionObject &excp) {
>>>>std::cerr << "Exception thrown while writing the image" <<
>>>>std::endl;
>>>>std::cerr << excp << std::endl;
>>>>  }
>>>>}
>>>>Thanks,
>>>>
>>>>George
>>>>
>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>-
>>>>--
>>>>
>>>>_______________________________________________
>>>>Insight-users mailing list
>>>>Insight-users at itk.org 
>>>>http://www.itk.org/mailman/listinfo/insight-users
>>>
>>>_______________________________________________
>>>Insight-users mailing list
>>>Insight-users at itk.org
>>>http://www.itk.org/mailman/listinfo/insight-users
>>>
>>>
>>
>>
> 



More information about the Insight-users mailing list