[Insight-users] MetaImage Read Problem

Kevin Neff kevin.l.neff at gmail.com
Thu Nov 18 16:25:20 EST 2010


I am trying to read in a raw data file (via MetaImage reader).  It's
skipping the first byte of my file.

The file, according to xxd -b <file> | less

0000000: 00000000 00001001 00000000 00000110 00000000 00001010  ......
0000006: 00000000 00000101 00000000 00000101 00000000 00000110  ......
000000c: 00000000 00000000 00000000 00000100 00000000 00000100  ......
0000012: 00000000 00001001 00000000 00000011 00000000 00000100  ......

The code:

#include <iostream>
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
//#include "itkRawImageIO.h"
#include "itkMetaImageIO.h"

#define print std::cout
#define nl    std::endl

int main( int argc, char** argv )
{
  typedef itk::Image< unsigned short, 3 > ImageType;

  ImageType::Pointer image = ImageType::New();

  ImageType::IndexType start;
  start[0] =   0;  // first index on X
  start[1] =   0;  // first index on Y
  start[2] =   0;  // first index on Z

  ImageType::SizeType  size;
  size[0]  = 1388;  // size along X
  size[1]  =  560;  // size along Y
  size[2]  =   48;  // size along Z

  ImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );
  image->SetRegions( region );

  image->Allocate();

  typedef itk::ImageFileReader< ImageType > ReaderType;
  typedef itk::ImageFileWriter< ImageType > WriterType;
  typedef itk::MetaImageIO                  ImageIOType;

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

  ReaderType::Pointer reader = ReaderType::New();
  const char * infile = argv[1];
  reader->SetFileName( infile );
  image = reader->GetOutput();

  print << "Reading from file (" << argv[0] << ")" << nl;
  try{ reader->Update(); }
  catch( itk::ExceptionObject & err )    {
    std::cerr << "ExceptionObject caught!  Check filename" << std::endl;
    std::cerr << err << std::endl;
    return EXIT_FAILURE;
  }

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

  const ImageType::PointType& orgn = image->GetOrigin();
  std::cout << "Origin = ";
  std::cout << orgn[0] << ", " << orgn[1] << ", " << orgn[2] << std::endl;

  typedef itk::Point< double, ImageType::ImageDimension > PointType;

  PointType p;
  ImageType::IndexType i;
  ImageType::PixelType v;
  for(  int x = 0;  x < 10;  x += 1  )   {
    p[0] = x; p[1] = 0; p[2] = 0;
    image->TransformPhysicalPointToIndex(p,i);
    v = image->GetPixel( i );
    print << "Value of (" << p[0] << "," << p[1] << "," << p[2] << ") is "
<< v << " and has index " << i << nl;
  }

  return 0;
}

The output:


tukey # make && ./register img8.mhd
Scanning dependencies of target register
[100%] Building CXX object CMakeFiles/register.dir/main.cxx.o
Linking CXX executable register
[100%] Built target register
Reading from file (./register)
Spacing = 1.29, 1.29, 5.44
Origin = 0, 0, 0
Value of (0,0,0) is 2304 and has index [0, 0, 0]
Value of (1,0,0) is 1536 and has index [1, 0, 0]
Value of (2,0,0) is 2560 and has index [2, 0, 0]
Value of (3,0,0) is 2560 and has index [2, 0, 0]
Value of (4,0,0) is 1280 and has index [3, 0, 0]
Value of (5,0,0) is 1280 and has index [4, 0, 0]
Value of (6,0,0) is 1536 and has index [5, 0, 0]
Value of (7,0,0) is 1536 and has index [5, 0, 0]
Value of (8,0,0) is 0 and has index [6, 0, 0]
Value of (9,0,0) is 1024 and has index [7, 0, 0]

Looks like the first byte of the file is being skipped.  Is this an error or
a feature?

KLN


---------
Kevin Neff, Ph.D.
Mayo Clinic College of Medicine
Rochester, MN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20101118/f028c8a3/attachment.htm>


More information about the Insight-users mailing list