[Insight-users] setting the spacing when writing an image to disk

Lucas Lorenzo lucas at cvrti.utah.edu
Tue, 10 Feb 2004 18:56:48 -0700


--Apple-Mail-10--626838801
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

Hi all,

I'm trying to write an image to disk in vtk format.
By default the spacing is set to 1 1 1. I'd like to change it so I'm 
doing the following:

#include "itkVTKImageIO.h"

int main( int argc, char * argv[] )
{
     		  /* Typedefs of components. */
   const unsigned int    ImageDimension = 2;
   typedef unsigned char BinaryPixelType;
   typedef itk::Image<BinaryPixelType,ImageDimension> BinaryImageType;


// read the input image and get the spacing from it:

   typedef itk::VTKImageIO ImageIOType;
   ImageIOType::Pointer IO1 = ImageIOType::New();
   original_reader->SetImageIO(IO1);
   double dx,dy,dz;
   original_reader->Update();
   dx = IO1->GetSpacing(0);
   dy = IO1->GetSpacing(1);
   dz = IO1->GetSpacing(2);

// here I'm omitting when I  process the input image

// write the image to a file but perviously set the spacing

   binary_writer->SetFileName("out.vtk");

   binary_writer->SetInput(thresholder->GetOutput());

   ImageIOType::Pointer IO2 = ImageIOType::New();
   IO2->SetSpacing(0,dx);
   IO2->SetSpacing(1,dy);
   IO2->SetSpacing(2,dz);
   binary_writer->SetImageIO(IO2);

   try
     {
       binary_writer->Update();
     }
   catch( itk::ExceptionObject & exp )
     {
     std::cerr << "Exception caught ! binary image writer" << std::endl;
     std::cerr <<     exp    << std::endl;
     return -1;
     }


  return 0;
}

When I execute this program I have a "segmentation fault" run time 
error.
If I ommit the line setting the spacing in the z axis (" 
IO2->SetSpacing(2,dz); ") the programs executes without crashing but 
the spacing is not really set, that is, in the header of the output vtk 
file (out.vtk) you can still see "SPACING 1 1 1.0" instead of "SPACING 
dx dy 1.0".
Any clue of what am I doing wrong ?
Thanks,

Lucas Lorenzo

University of Utah
Nora Eccles Harrison CardioVascular Research and Training Institute
Fellows Room
95 South 2000 East
Salt Lake City, UT 84112-5000

e-mail:  lucas at cvrti.utah.edu
telephone: 801-587-9536

--Apple-Mail-10--626838801
Content-Transfer-Encoding: 7bit
Content-Type: text/enriched;
	charset=US-ASCII

Hi all,


I'm trying to write an image to disk in vtk format.

By default the spacing is set to 1 1 1. I'd like to change it so I'm
doing the following:


<italic><color><param>0254,055E,FFFE</param>#include "itkVTKImageIO.h"


int main( int argc, char * argv[] )

{

   
		</color></italic><bold><fontfamily><param>Arial Black</param><color><param>0254,055E,FFFE</param> 
/* Typedefs of components. */</color></fontfamily></bold><italic><color><param>0254,055E,FFFE</param>

  const unsigned int    ImageDimension = 2;

  typedef unsigned char BinaryPixelType;

  typedef itk::Image<<BinaryPixelType,ImageDimension> BinaryImageType;



</color></italic><bold><fontfamily><param>Arial Black</param><color><param>0254,055E,FFFE</param>//
read the input image and get the spacing from it:</color></fontfamily></bold><italic><color><param>0254,055E,FFFE</param>


  typedef itk::VTKImageIO ImageIOType;

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

  original_reader->SetImageIO(IO1);

  double dx,dy,dz;

  original_reader->Update();

  dx = IO1->GetSpacing(0);

  dy = IO1->GetSpacing(1);

  dz = IO1->GetSpacing(2);


</color></italic><bold><fontfamily><param>Arial Black</param><color><param>0254,055E,FFFE</param>//
here I'm omitting when I  process the input image


// write the image to a file but perviously set the spacing

</color></fontfamily></bold><italic><color><param>0254,055E,FFFE</param>

  binary_writer->SetFileName("out.vtk");

  

  binary_writer->SetInput(thresholder->GetOutput());


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

  IO2->SetSpacing(0,dx);

  IO2->SetSpacing(1,dy);

  IO2->SetSpacing(2,dz); 

  binary_writer->SetImageIO(IO2);


  try

    {

      binary_writer->Update();

    }

  catch( itk::ExceptionObject & exp )

    {

    std::cerr <<<< "Exception caught ! binary image writer" <<<<
std::endl;

    std::cerr <<<<     exp    <<<< std::endl;

    return -1;

    }   



 return 0;

}

</color></italic>

When I execute this program I have a "segmentation fault" run time
error.

If I ommit the line setting the spacing in the z axis
("<italic><color><param>0253,055D,FFFD</param> IO2->SetSpacing(2,dz);
</color></italic><color><param>0000,0000,0000</param>") the programs
executes without crashing but the spacing is not really set, that is,
in the header of the output vtk file (out.vtk) you can still see
"SPACING 1 1 1.0" instead of "SPACING dx dy 1.0".

</color>Any clue of what am I doing wrong ?

Thanks,


Lucas Lorenzo


University of Utah

Nora Eccles Harrison CardioVascular Research and Training Institute

Fellows Room

95 South 2000 East

Salt Lake City, UT 84112-5000


e-mail:  lucas at cvrti.utah.edu

telephone: 801-587-9536


--Apple-Mail-10--626838801--