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

Cory Quammen cquammen at cs.unc.edu
Mon Feb 28 09:10:34 EST 2011


David,

The Get() method in the iterator makes a copy of the pixel value and
returns it to the caller. This copy exists in a temporary variable,
and you are taking the address of that copy (not the original), thus
the warning. To my knowledge, there is no way to operate directly on
the pixel value through the Get() method in the iterator.

This should let you do what you are looking to do.

PixelType pixel = imageIterator->Get();
// modify pixel value
imageIterator->Set(pixel);

It's an extra line to set the new pixel value, but this is likely less
error prone than operating on a reference to a pixel value.

Cory

On Mon, Feb 28, 2011 at 8:47 AM, David Doria <daviddoria at gmail.com> wrote:
> 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?
>
> David
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>



-- 
Cory Quammen
Computer Integrated Systems for Microscopy and Manipulation (CISMM)
Department of Computer Science
University of North Carolina at Chapel Hill
http://www.cs.unc.edu/~cquammen


More information about the Insight-users mailing list