[Insight-users] Writing a Colour Meta Image

Puja Malik puja.malik@memot.unibe.ch
Thu, 07 Nov 2002 10:26:06 +0100


--Boundary_(ID_9gzYGM1gZhQ+MyS/JaCUpQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8BIT

Hello,

I'm having some trouble writing a colour meta image.    I am using
exactly the same format as the MetaImageWriterExample, except I have
declared my image with an RGB pixel type.  The program compiles and
links, but fails at runtime as soon as the
ImageFileWriter::Write() function is called.  I've had no problems when
writing to a grey image.  I'm wondering what I'm doing wrong?  Here is
my code:

  typedef itk::Image<itk::RGBPixel<float>, 3> ImageType;
 ImageType::SizeType imSize = {{256, 256, 10}};

  ImageType::Pointer im = ImageType::New();
  im->SetRegions(imSize);
  im->Allocate();

//To set the pixels I declare a temporary RGB pixel variable and set
each pixel using an iterator:

  itk::ImageRegionIterator< ImageType > it(im,
        im->GetLargestPossibleRegion());

  //create a temporary RGB pixel object to use to set each pixel
  itk::RGBPixel<float> tempPix;

  it.GoToBegin();

  while(!it.IsAtEnd())
  {
   //sets in order Red, Green Blue
   tempPix.SetRed(50);
   tempPix.SetGreen(0);
   tempPix.SetBlue(0);
    it.Set(tempPix);
    ++it;
  }

//>>>>>>>>>> Write the Image to a file <<<<<<<<<<<<<<<<<<<<<//

  typedef itk::ImageFileWriter< ImageType > VolumeWriterType;
  VolumeWriterType::Pointer writer = VolumeWriterType::New();

  itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();

  writer->SetImageIO( metaWriter );
  writer->SetFileName(fName);
  writer->SetInput( im );

//>>>>>>>>>>>>>PROGRAM FAILS HERE<<<<<<<<<<<<<<<<<<//
  writer->Write();









---------------------------------------
Puja Malik
M. E. Müller Institute for Biomechanics
University of Bern
P. O. Box 30
3010 Bern
Switzerland
Phone: +41-31-632-8730
Fax: +41-31-632-4951
Email: Puja.Malik@memot.unibe.ch
---------------------------------------



--Boundary_(ID_9gzYGM1gZhQ+MyS/JaCUpQ)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hello,
<p>I'm having some trouble writing a colour meta image.&nbsp;&nbsp;&nbsp;
I am using exactly the same format as the MetaImageWriterExample, except
I&nbsp;have declared my image with an RGB pixel type.&nbsp; The program
compiles and links, but fails at runtime as soon as the ImageFileWriter::Write()&nbsp;function
is called.&nbsp; I've had no problems when writing to a grey image.&nbsp;
I'm wondering what I'm doing wrong?&nbsp; Here is my code:
<p>&nbsp; typedef itk::Image&lt;itk::RGBPixel&lt;float>, 3> ImageType;
<br>&nbsp;ImageType::SizeType imSize = {{256, 256, 10}};
<p>&nbsp; ImageType::Pointer im = ImageType::New();
<br>&nbsp; im->SetRegions(imSize);
<br>&nbsp; im->Allocate();
<p>//To set the pixels I&nbsp;declare a temporary RGB&nbsp;pixel variable
and set each pixel using an iterator:
<p>&nbsp; itk::ImageRegionIterator&lt; ImageType > it(im,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; im->GetLargestPossibleRegion());
<br>&nbsp;
<br>&nbsp; //create a temporary RGB pixel object to use to set each pixel
<br>&nbsp; itk::RGBPixel&lt;float> tempPix;
<p>&nbsp; it.GoToBegin();
<br>&nbsp;
<br>&nbsp; while(!it.IsAtEnd())
<br>&nbsp; {
<br>&nbsp;&nbsp; //sets in order Red, Green Blue
<br>&nbsp;&nbsp; tempPix.SetRed(50);
<br>&nbsp;&nbsp; tempPix.SetGreen(0);
<br>&nbsp;&nbsp; tempPix.SetBlue(0);
<br>&nbsp;&nbsp;&nbsp; it.Set(tempPix);
<br>&nbsp;&nbsp;&nbsp; ++it;
<br>&nbsp; }
<p>//>>>>>>>>>> Write the Image to a file &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;//
<br>&nbsp;
<br>&nbsp; typedef itk::ImageFileWriter&lt; ImageType > VolumeWriterType;
<br>&nbsp; VolumeWriterType::Pointer writer = VolumeWriterType::New();
<p>&nbsp; itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();
<br>&nbsp;
<br>&nbsp; writer->SetImageIO( metaWriter );
<br>&nbsp; writer->SetFileName(fName);
<br>&nbsp; writer->SetInput( im );
<p>//>>>>>>>>>>>>>PROGRAM&nbsp;FAILS&nbsp;HERE&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;//
<br>&nbsp; writer->Write();
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;
<br>&nbsp;
<pre>&nbsp;
---------------------------------------
Puja Malik
M. E. M&uuml;ller Institute for Biomechanics
University of Bern
P. O. Box 30
3010 Bern
Switzerland
Phone: +41-31-632-8730
Fax: +41-31-632-4951
Email: Puja.Malik@memot.unibe.ch
---------------------------------------</pre>
&nbsp;</html>

--Boundary_(ID_9gzYGM1gZhQ+MyS/JaCUpQ)--