[Insight-users] ImageRegion Interator help

PY pyl111 at rogers.com
Thu Mar 8 05:45:18 EST 2007


Hi All,

    I'm attempting to try out the image iterator, but I'm stuck on a 
problem.  I have pretty much copied out the code based on the guide 
(section 11.3), but have changed some values around.  All I want to do 
first is to basically see if it reads my image correctly, hence the 
reason I put the iCount counter in the for loop at the end.  I found 
that when I run my program, it goes through the first row of pixels, but 
once it goes to the second row, the program crashes.  Why?

    Below I have provided the code I used. 

    Note: InternalPixelType is a float type, Dimension = 2
             My input image is a .png file --> is this conflicting with 
the float type from above? 
             I want to start at the beginning of the input image, hence 
inputStart is at (0,0)
             The png image I'm using is 661 x 375

//-----------------------Code begins here---------------------------------
  typedef itk::Image< InternalPixelType, Dimension> ImageType;
  typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
  typedef itk::ImageRegionIterator< ImageType > IteratorType;
     
  ImageType::RegionType inputRegion;
  ImageType::RegionType::IndexType inputStart;
  ImageType::RegionType::SizeType size;

  inputStart[0] = 0;
  inputStart[1] = 0;

  size[0] = 661;
  size[1] = 375;

  inputRegion.SetSize( size );
  inputRegion.SetIndex( inputStart );

  ImageType::RegionType outputRegion;
  ImageType::RegionType::IndexType outputStart;

  outputStart[0] = 0;
  outputStart[1] = 0;

  outputRegion.SetSize( size );
  outputRegion.SetIndex( outputStart );

  ImageType::Pointer outputImage = ImageType::New();
  outputImage->SetRegions( outputRegion );

  const ImageType::SpacingType& spacing = reader->GetOutput()->GetSpacing();
  const ImageType::PointType& inputOrigin = 
reader->GetOutput()->GetOrigin();
  double outputOrigin[ Dimension ];

  for (unsigned int i = 0; i < Dimension; i++)
  {
      outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
  }

  outputImage->SetSpacing( spacing );
  outputImage->SetOrigin( outputOrigin );
  outputImage->Allocate();

  ConstIteratorType inputIt( reader->GetOutput(), inputRegion );
  IteratorType outputIt( outputImage, outputRegion );

  int iCount = 0;
  for ( inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); 
++inputIt, ++outputIt )
  {
      cout << iCount++ << endl;  //replaced normal code to see if each 
row being read
  }




More information about the Insight-users mailing list