[Insight-users] How to use RawImageIO

Kent Williams norman-k-williams@uiowa.edu
Fri, 4 Apr 2003 10:24:44 -0600


On Friday 04 April 2003 09:18 am, Haiwen Shi wrote:
> Hi, All;
>
> I want to use itk to read a MR image file that
> consists of only binary data(the data type is double),
> not any header information in it. Some people
> suggested me to use RawImageIO to read the data file.
> Can anybody tell me how to use this class? An example
> will be perfect. Or if you have any other suggestion,
> please let me know.
>
> I will really appreciate your help!
>

Well, if you have the source code for ITK handy -- as you should ;-)
Look at Insight/Testing/Code/IO/itkRawImageIOTest.cxx as a start.
Since Raw Image IO has no fixed file extension or in-file signature to de=
fine=20
what is and is not a raw file, you need to explicitly associate an=20
itk:RawImageIO object with the ImageFileReader. That's what going on
here:
  typedef itk::Image<unsigned short,2>    ImageType;
  typedef ImageType::PixelType            PixelType;
  itk::RawImageIO<unsigned short,2>::Pointer io;
  io =3D itk::RawImageIO<unsigned short,2>::New();\
  itk::ImageFileReader<ImageType>::Pointer reader;
  reader =3D itk::ImageFileReader<ImageType>::New();
  reader->SetImageIO(io);

Then the usual file reading process can take place:
  reader->SetFileName("junk.raw");
  reader->Update();

There are several essential and useful functions in the RawImageIO class =
which=20
allow you to read raw data from an arbitrary file:

  void itk::RawImageIO::SetHeaderSize(unsigned long size);
  unsigned long itk::RawImageIO::GetHeaderSize();

SetHeaderSize lets you skip a header, if any to get to the real data.

  void itk::RawImageIO::SetFileDimensionality(unsigned long dim);

SetFileDimensionality says how many dimensions are in a particular data f=
ile.

The other important member functions to use are SetDimensions

=09void itk::ImageIOBase::SetDimensions(unsigned int i, int dim);

which tells RawImageIO how many rows, columns, slices, etc is in the imag=
e
and SetNumberOfComponents

=09void itk::ImageIOBase::SetNumberOfComponents(unsigned int num_componen=
ts);

which tells RawImageIO how many components each pixel comprises -- for ex=
ample=20
if the file has 3 bytes per pixel for RGB, you'd call=20
SetNumberOfComponents(3) to indicate that fact.

Again, please look through the Testing/Code/IO directory for straightforw=
ard=20
examples of using RawImageIO, and you should do fine.