[Insight-users] change spacing of a mhd file

jbehar jbehar at messel.emse.fr
Mon Jun 28 15:05:35 EDT 2010


Hi all,

I am trying to convert a stack of png into a .mhd file. The stack is the
result of a segmentation process and I would like, when writing the .mhd, to
specify the spacing. I tried to do it by creating an intermediate image and
using  ImageType::SpacingType spacing. It seems to work when I use
image->GetSpacing() like this:
  const ImageType::SpacingType& sp = image->GetSpacing();
  std::cout << "Spacing =";
  std::cout << sp[0] << ","<<sp[1] << "," << sp[2] << std::endl;

it prints the correct answer but when I write the file as a .mhd
 writer->SetInput( image);
 writer->SetFileName( outputFilename );
and open the .mhd file, the spacing is not changed.

If you have a clue...

thanks a lot,

Joachim





This is my code:

int main( int argc, char ** argv )
{
  if( argc < 4 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " firstSliceValue lastSliceValue 
outputImageFile " << std::endl;
    return EXIT_FAILURE;
    }

  typedef unsigned char                       PixelType;
  const unsigned int Dimension = 3;

  typedef itk::Image< PixelType, Dimension >  ImageType;


  typedef itk::ImageSeriesReader< ImageType >  ReaderType;
  typedef itk::ImageFileWriter<   ImageType >  WriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
 

  const unsigned int first = atoi( argv[1] );
  const unsigned int last  = atoi( argv[2] );

  const char * outputFilename = argv[3];

 typedef itk::NumericSeriesFileNames    NameGeneratorType;

  NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();

  nameGenerator->SetSeriesFormat(
"../data/transition_selected_cluster/%d.png" );
  nameGenerator->SetStartIndex( first );
  nameGenerator->SetEndIndex( last );
  nameGenerator->SetIncrementIndex( 1 );

  reader->SetImageIO( itk::PNGImageIO::New() );


 reader->SetFileNames( nameGenerator->GetFileNames()  );

  ImageType::Pointer image = ImageType::New();
  image=reader->GetOutput();

  ImageType::SpacingType spacing;
  spacing[0]=0.32;
  spacing[1]=0.32;
  spacing[2]=0.625;
  image->SetSpacing(spacing);

  const ImageType::SpacingType& sp = image->GetSpacing();
  std::cout << "Spacing =";
  std::cout << sp[0] << ","<<sp[1] << "," << sp[2] << std::endl;

  ImageType::PointType origin;
  origin[0]=1.0;
  origin[1]=1.0;
  origin[2]=1.0;
  image->SetOrigin(origin);

 writer->SetInput( image);
 writer->SetFileName( outputFilename );


  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    return EXIT_FAILURE;
    }  

return EXIT_SUCCESS;
} 
-- 
View this message in context: http://old.nabble.com/change-spacing-of-a-mhd-file-tp29011345p29011345.html
Sent from the ITK - Users mailing list archive at Nabble.com.



More information about the Insight-users mailing list