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

O Hamo ohamo at live.de
Fri Nov 8 07:08:33 EST 2013


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.

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20131108/be4bda99/attachment.html>
-------------- next part --------------
_____________________________________
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


More information about the Community mailing list