[Insight-users] How to access pixels of itk::Image<TPixel, Dimension>
Luis Ibanez
luis . ibanez at kitware . com
Fri, 09 Aug 2002 08:56:52 -0400
Hi zhao,
You are completly right !
The GetBufferPointer() method is NOT the appropiate
way to access the pixels on the image.
ImageIterator ARE the right mechanism for pixel access in ITK.
Note that as opossed to STL-iterators, ITK image iterators
are not obtained from the container object (in this case
the image). STL can do this because their containers are
all linear (so a little bit boring :-).
N-D Images are far more interesting since there are many
fun ways to walk over their pixels. ITK provides iterators
as separate classes (not belonging to the image) in order
to account for the variety of way to walk over an image.
With this design you can always create your own way to
walk over an image.
ImageIterators are templated over the image type and
access internally the image buffer. They encode a particular
strategy for walking along the pixels.
Here is the typical example of how to use iterator to access the
pixels in an image:
#include <itkImageRegionIterator.h>
#include <itkImage.h>
typedef itk::Image<char,4> ImageType; // a 4-D image
typedef itk::ImageRegionIterator< ImageType > IteratorType;
ImageType::Pointer image = GetImageSomeHow();
ImageType::ImageRegion region = image->GetLargestPossibleRegion();
IteratorType it( image, region ); // select and image+region to visit
it.GoToBegin(); // move the iterator to the starting point
while( ! it.IsAtEnd() )
{
it.Set( value ); // change the value of the pixel
const ImageType::PixelType value = it.Get(); // get the value
++it;
}
As in STL, all ITK iterators has their "const" version.
This makes possible to write const-correct.
Note that after construction the iterator contains enough
information about the image to be able to determine how
to start and finish its walk. Iterators also allows to
have basically the same code for accessing a 2D image or
a 4D image.
The full doxygenated documentation about iterators is
available at:
http://www.itk.org/Insight/Doxygen/html/group__ImageIterators.html
Commonly used iterators are:
- ImageRegionIterator : visits all pixels (no order guarranteed)
- ImageLineareIteratorWithIndex: visits all pixels by going
along some user-specified dimension (no order among lines)
- ImageSliceIteratorWithIndex: visits all pixels by going in
order over two user-specified dimensions (no order among slices).
Funny iterators:
- RandomIterator: walk randomly among N pixels in a region.
Powerful iterators:
NeighborhoodIterator: visits all the image by sliding a neighborhood
over the pixels. This great iterator solves the classical problem
of obtaining those 3x3x3 or 5x5x5 neighborhods for doing filtering.
FloodFilledImageFunctionConditionalIterator: visit all the pixels in
a region that satisfy a particular criterion. This iterator is the
base for severa region growing filter and connected components.
TODO Iterators :-):
PeanoIterator: an iterator that walks along a peano path so it maximizes
the chances of passing through a region. This is what you want to
explore an image in search for an object that can be anywhere on the
image.
-------------------
You may find interesting the page about Iterators in the
"Concepts" section of ITK's Doxygen documentation:
http://www.itk.org/Insight/Doxygen/html/ImageIteratorsPage.html
Please let us know if you have any questions about the use
of iterators.
Thanks
Luis
===================================================
zhao wrote:
> itkHi everyone,
> I know one way of accessing the bits of itk::Image<...> is to use the
> ember function GetBufferPointer(). But I think a better way is to use
iterators to access the pixels. The online document suggests that
function Begin() be used to return an iterator. However, Begin() is not
a member of itk::Image<...>. Is there any intermediate step I need to
take to solve this problem??
> Thanks for your reply.
>
> Zhao
>
>
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
>
>