[Insight-users] Getting data in and out of ITK

Bjorn Hanch Sollie bhs at pvv.org
Thu Feb 14 12:52:37 EST 2002


Hello to you all,

I have been struggling for some time now trying to get 3D raw image
data in and out of ITK.  Basically what I wish to do is this:

read data into buffer -> filters -> write output buffer to disk

or in other words:

Read data, filter calls reader->GetOutput(), another filter calls
filter->GetOutput() etc., writer calls filter->GetOutput(), write
data.

I've studied the examples, but unfortunately I have so far been unable
to use the techniques they provide to fit my own needs and actually
produce meaningful output.  (Yes, I'm aware that there are minor
hitches, such as, for example, that some filters require data of
certain types.)

I have attached a simple sample program (it's just short over 50
lines) that attempts to just read some image data into a buffer, ready
to be accessed by a filter and write the processed data back to disk,
the present problem being that it doesn't produce any output at all.

I will be most grateful if any one of you are willing to bear with me,
take a look at it and give me some hints as to what I need to
do/change in order to make it work accoding to my wishes.

-Beorn
-- 
The History of the Universe
Chapter 1: Bang!  Chapter 2: Sss...  Chapter 3: Crunch!
The End
-------------- next part --------------
#include "itkRawImageIO.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRawImageWriter.h"
#include "itkGradientMagnitudeImageFilter.h"


typedef itk::Image<unsigned char, 3> ImageType;


int main(int argc, char *argv[])
{
  char     *input_file     = "in.raw";      // raw input image
  char     *output_file    = "out.raw";     // raw output image

  // Create a RawImageIO object for the reader.
  itk::RawImageIO<unsigned char, 3>::Pointer read_io;
  read_io = itk::RawImageIO<unsigned char, 3>::New();
  read_io->SetNumberOfDimensions(3);
  read_io->SetDimensions(0, 256);
  read_io->SetDimensions(1, 256);
  read_io->SetDimensions(2, 110);

  // Set up an image reader.
  itk::ImageFileReader<ImageType>::Pointer reader;
  reader = itk::ImageFileReader<ImageType>::New();
  reader->SetImageIO(read_io);
  reader->SetFileName(input_file);

  // Set up a gradient magnitude image filter.
  itk::GradientMagnitudeImageFilter<ImageType, ImageType>::Pointer magnitude;
    magnitude = itk::GradientMagnitudeImageFilter<ImageType, ImageType>::New();
  magnitude->SetInput(reader->GetOutput());

  // Create a RawImageIO object for the writer.
  itk::RawImageIO<unsigned char, 3>::Pointer write_io;
  write_io = itk::RawImageIO<unsigned char, 3>::New();
  read_io->SetNumberOfDimensions(3);
  read_io->SetDimensions(0, 256);
  read_io->SetDimensions(1, 256);
  read_io->SetDimensions(2, 110);

  // Write the filtered image to disk.
  itk::ImageFileWriter<ImageType>::Pointer writer;
    writer = itk::ImageFileWriter<ImageType>::New();
  writer->SetInput(magnitude->GetOutput());
  writer->SetFileName(output_file);
  writer->SetImageIO(write_io);
  writer->Write();

  return 0;
}



More information about the Insight-users mailing list