[Insight-users] [ITK Community] modify spacing of an 3D image slice

Brian Helba brian.helba at kitware.com
Fri Nov 8 13:45:08 EST 2013


Hi again Omar,

What version of ITK are you using? I can't seem to reproduce your specific
problem with Git master, v4.4.2, or v3.20.1. Please try one of these three
versions if you aren't already.

Just so we are clear, please note that for CT, slice thickness (encoded in
[0018,0050]) is fundamentally different from slice spacing (which
incidentally is more difficult to extract from DICOM), and the two may
legitimately have different values. Along these lines, I noticed a change
from ITK v3.20 to v4.4 (which internally rely upon GDCM 1.2.x and 2.0.16,
respectively, to determine all of this). The change is such that when
reading a single slice of my own CT test image, the old version extracted
Z-spacing from the optional field [0018,0088] Spacing Between Slices (which
was conveniently set in my test image) but defaults to 1.0 in the new
version. Please see [1] for a good discussion of the issue by GDCM's core
maintainer.

However, this doesn't seem to be your issue. You want to explicitly re-set
the "m_Spacing[2]" value of the itk::Image, regardless of how it is
initially being set by the reader, correct? Is your issue that it's not
remaining changed in later "GetSpacing()" calls, or just that it's not
reflected in some tag of the output DICOM that's written?

[1]
http://www.creatis.insa-lyon.fr/pipermail/dcmlib/2005-September/002141.html

Enjoy,
Brian



On Fri, Nov 8, 2013 at 7:08 AM, O Hamo <ohamo at live.de> wrote:

> Hi Brian,
>
> thanks for your reply.
> Im using GDCM and call reader->Update() bevorehand.
>
> Here is some code reproducing that error:
> Also as seen in the commented lines, I tried to apply the
> ChangeInformationImageFiltet still leading in the same result.
>
> #include <list>
> #include <fstream>
> int main(int argc, char * argv[])
> {
> #if PRINTADDITIONALINFO >= 2
>  std::cout << "Using ITK " << ITK_VERSION_MAJOR << "."
>     << ITK_VERSION_MINOR << std::endl;
> #endif
> //////read command line input////////////////////////////////////////
>  if (!(argc == 3 || argc == 4))
>  {
>   std::cerr << "Wrong input flags." << std::endl; //todo be more precise
>   std::cerr << "Syntax: [input file] [output filename]"
>       << std::endl << std::endl;
>   return 0;
>  }
>  const std::string inputFilename (argv[1]);
>  const std::string outputFilename(argv[2]);
>
>  std::cout << argv[3];
>  std::cout << std::endl
>     << "input file: " << inputFilename.c_str()
>     << std::endl
>     << "output file: " << outputFilename.c_str()
>     << std::endl;
>
> //////end: read command line input///////////////////////////////////
> //////local typdefs//////////////////////////////////////////////////
>  typedef short       ReaderPixelType;
>  const signed short     InputDimension(3);
>  typedef itk::Image
>   <ReaderPixelType, InputDimension> InputImageType;
>  typedef itk::ImageFileReader
>   < InputImageType >      ReaderType;
>
>  typedef itk::GDCMImageIO    ImageIOType;
>  typedef itk::MetaDataDictionary   DictionaryType;
>
>  ///output slice properties
>  typedef ReaderPixelType     OutputPixelType;
>  const unsigned int      OutputDimension(InputDimension);
>  typedef itk::Image
>   <OutputPixelType, OutputDimension> OutputImageType;
>  typedef itk::ImageFileWriter
>   < OutputImageType >     WriterType;
> //////end: local typdefs/////////////////////////////////////////////
> //////read input images//////////////////////////////////////////////
>  ReaderType::Pointer reader (ReaderType::New());
>  ImageIOType::Pointer readerIO (ImageIOType::New());
>  reader->SetFileName(inputFilename.c_str());
>
>  reader->SetImageIO(readerIO);
>  try {reader->Update();}
>  catch (itk::ExceptionObject &excp)
>     {
>   std::cerr <<"Exception thrown while reading image"<<std::endl;
>   std::cerr << excp << std::endl;
>   return EXIT_FAILURE;
>  }
> //////end: read input images/////////////////////////////////////////
> //////read/modify meta data//////////////////////////////////////////
>  InputImageType::Pointer readerOutput(reader->GetOutput());
>  std::cout << "Spacing : "
>    << readerOutput->GetSpacing() << std::endl;
>
>  DictionaryType & mainDictonary = (readerIO->GetMetaDataDictionary());
>  double spacingIn[3];
>  spacingIn[0] = 0.1;
>  spacingIn[1] = 0.4;
>  spacingIn[2] = 0.35;
>  reader->GetOutput()->SetSpacing(spacingIn);
>  //different attempts to modify spacing[2]:
>
>  // I. write to dictionary
>  //itksys_ios::ostringstream helperStr;
>  //helperStr.str("");
>  //helperStr << spacingIn[2];
>  //itk::EncapsulateMetaData<std::string>
>  // (mainDictonary, "0018|0050", helperStr.str());//SpacingBetweenSlices
>  //itk::EncapsulateMetaData<std::string>
>  // (mainDictonary, "0018|0088", helperStr.str());//SliceThickness
>  // II. apply filter
>  //typedef itk::ChangeInformationImageFilter< InputImageType >  FilterType;
>  //FilterType::Pointer filter = FilterType::New();
>  //filter->SetOutputSpacing( spacingIn );
>  //filter->ChangeSpacingOn();
>  //filter->SetInput( reader->GetOutput() );
>  //filter->Update();
>  //std::cout << filter->GetOutputSpacing();
>  // III. modify IO
>  //readerIO->SetSpacing(2,spacingIn[2]);
>
> ////////write single image with updated prop.///////////////////////
>  WriterType::Pointer writer (WriterType::New());
>  writer->SetFileName( outputFilename.c_str() );
>  writer->SetInput( reader->GetOutput() );
>  writer->UseInputMetaDataDictionaryOff ();
>  writer->SetImageIO(readerIO);
>  try {writer->Update();}
>  catch (itk::ExceptionObject &ex)
>  {
>    std::cerr << "Exception thrown while writing single test image "
>     << std::endl;
>    std::cout << ex << std::endl;
>    return EXIT_FAILURE;
>  }
>  return EXIT_SUCCESS;
> }
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkGDCMImageIO.h"
> #include "itkMetaDataObject.h"
> #include "itkChangeInformationImageFilter.h"
>
>
>
> ------------------------------
> Date: Thu, 7 Nov 2013 19:06:16 -0500
> Subject: Re: [ITK Community] [Insight-users] modify spacing of an 3D image
> slice
> From: brian.helba at kitware.com
> To: ohamo at live.de
> CC: insight-users at itk.org
>
>
> Hi Omar,
>
> Are you using GDCM or DCMTK as your reader?
>
> Also, are you explicitly calling "reader->Update()" at some point before /
> after calling "reader->GetOutput()->SetSpacing(..)"?
>
> Thanks,
> Brian
>
>
> On Wed, Nov 6, 2013 at 12:40 PM, O Hamo <ohamo at live.de> wrote:
>
> Hello,
>
> I am reading a single image slice belonging to an DICOM CT series.
> The component type is short and number of dimension is 3.
> Now I want to modify the voxel spacing.
> To do that I tried this:
> double newSpacing[3];
> newSpacing[0] = x_spacing;
> newSpacing[1] = y_spacing;
>  newSpacing[2] = z_spacing;
> reader->GetOutput()->SetSpacing(newSpacing);
> but this will modify only x- and y-spacing while z remains the same (=1mm)
> Also writing z_spacing to the header entry 0018,0050 (Slice Thickness)
> won`t change the fact,
> that calling GetSliceSpacing() at the resulting image will return the
> values of x-and y-spacing but still 1 instead of z-spacing.
>
> How can the 3rd spacing component be modified?
> Is this even possible for the givien image?
> Because Im assuming that ITK is calculating the spacing by using
> ImagePositionPatient(0020,0032) and ImageOrientationPatient(0020,0037),
> but sets it by default to 1 when it cant find more than one slice. (Just a
> guess)
>
> Any help is apreciated.
>
> Kind Regads,
> Omar
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/cgi-bin/mailman/listinfo/community
>
>
>
>
> --
> Brian Helba
> Medical Imaging
> Kitware, Inc.
>



-- 
Brian Helba
Medical Imaging
Kitware, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20131108/1b9fb751/attachment.htm>


More information about the Insight-users mailing list