[Insight-users] The last "nested for" loop

Luis Ibanez luis . ibanez at kitware . com
Wed, 29 May 2002 09:34:55 -0400


   for(int j=0; j<height; j++)
     {
     for(int i=0; i<width; i++)
       {
       image.pixel(i,j) = 2 * image.pixel(i,j)
       }
     }


Looks familiar  ?


Tired of writing for() loops ?


What would you do if you need to port this
code to a 3D image ?
or maybe a 4D image ?

add another for() loop ?

what will you do about borders ?


Left behind your FORTRAN heritage and enter the
object oriented way of walking through an image:

                   "Iterators"

Replace this old FORTRAN-style code with real C++:

        IteratorType it( image, region );
        for( it.GoToBegin(); !it.IsAtEnd(); ++it )
          {
          it.Set(  2 * it.Get() );
          }	


What's the advantage:

1) Iterators will work on N-D, no change
         required in the code.

2) The strategy for walking the image
         can be changed just by changing the
         type of the iterator.



Details about the use of Image Iterators
can be found here:
http://www.itk.org/Insight/Doxygen/html/ImageIteratorsPage.html

The summary of ITK image iterators can be found here:
http://www.itk.org/Insight/Doxygen/html/group__ImageIterators.html


Enjoy ITK !



        Luis