[Insight-users] Getting a reference to a pixel from an iterator

Julien Lamy lamy at unistra.fr
Mon Feb 28 09:15:56 EST 2011


Le 28/02/2011 14:47, David Doria a écrit :
> I often find myself doing:
>
> itk::ImageRegionIterator<ImageType> imageIterator =
> itk::ImageRegionIterator<ImageType>(image, region);
>    while(!imageIterator.IsAtEnd())
>      {
>      PixelType pixel = image->GetPixel(imageIterator.GetIndex());
>      // changing 'pixel' changes the actual pixel in the image since
> GetPixel returns a reference
>      }
>
> Is there a reason that the iterator can't return a reference like this?
> That is,
>
>      PixelType pixel = imageIterator->Get();
>      // changing 'pixel' does NOT change the actual pixel in the image
> since Get does NOT return a reference
>
> Even this doesn't work:
>
>       PixelType* pixel = &(imageIterator->Get());
>
> because it complains "warning: taking the address of a temporary" (and
> it is correct, it doesn't not work correctly).
>
> Any thoughts on this?

It would be nice if iterators return could references instead of values, 
but as far as I can tell, it would be difficult to implement with the 
current design of image accessors : since ImageConstIterator::Get uses 
the accessor of the image being iterated, and since the accessor can do 
whatever it wants, including returning values not from pixel data (cf. 
Software Guide, section 12.4 for such an example), it does not seem 
possible to return a reference in a generic way.

-- 
Julien


More information about the Insight-users mailing list