[Insight-users] How to access pixels of itk::Image<TPixel, Dimension>

Luis Ibanez luis . ibanez at kitware . com
Fri, 09 Aug 2002 11:37:29 -0400


Hi Zhao,

The ImageSliceIteratorWithIndex was in fact designed for 3D images.

http://www.itk.org/Doxygen/html/classitk_1_1ImageSliceIteratorWithIndex.html

Even though the order among slices is not guarranteed in the API,
the implementation will walk over the slices in order.

The typical example will be something like:


typedef itk::ImageSliceIteratorWithIndex< ImageType > IteratorType;
IteratorType it( myImage, region );

it.GoToBegin();
it.SetFirstDirection( 2 );  // 0=x, 1=y, 2=z
it.SetSecondDirection( 1 ); // 0=x, 1=y, 2=z

while( !it.IsAtEnd() )
  {
  while( !it.IsAtEndOfSlice() )
    {
    while( !it.IsAtEndOfLine() )
      {
      value = it.Get(); // read
      it.Set( value );  // write
      ++it;
      }
    it.NextLine();
    }
  it.NextSlice();
  }


This example will walk faster along Z, then
along Y and finaly along X=k slices.


Note that there is a penalty in performance
when you choose to walk through the image this
way instead of using the ImageRegionIterator.
(which doesn't enforce any particular order).

For pixel-wise operations like adding two images,
computing the maximum, computing statistics...
pixel order is irrelevant.

What kind of operation are you interested on ?


Also, note that most of the time you should not
need to use the iterators but rather use existing
ImageFitlers. Any need to write code that requires
iterators will indicate that the operation you
need is a missing filter in ITK, in which case
we will be quite interested in adding it to the
toolkit.


  Luis


=================================================
zhao wrote:

> Hi Luis and Suzanne,
> 
>      Is there an iterator that can traverse a 3-D image in order, i.e. pixel by pixel, line by line , slice by slice? Form your directions, I know pixels of an 2-D image can be accessed in order by using ImageSliceIteratorWithIndex and its member functions SetFirstDirection and SetSecondDirection. But how to traverse a 3-D image in order? Is it unnecessary?
> 	Thanks a lot.
> 
> Zhao
> 
> 
> 
>