[Insight-users] Access voxel coordinates

Luis Ibanez luis.ibanez at kitware.com
Tue Jun 30 16:01:14 EDT 2009


Hi Gabriela,

Your code will do the trick...

However,
there are much better ways of writing the same code.


In particular


     A) you want to avoid calling GetPixel() because
        it is slower than using Image Iterators.


     B) Your code will only work for 3D images.


     C) Your should use GetBufferedRegion() instead of
        GetLargestPossibleRegion(). The buffered region
        is the only one that is guarranteed to be
        currently available in RAM memory.

     D) You should avoid refering to the index of
        a pixel as the "coordinates" of the pixel.
        In this mailing list we will assume that
        coordinates of a pixel are its Physical
        Point coordinates (that is, taking into
        account image origin, pixel spacing and
        image orientation).



Please read the ITK Software Guide


     http://www.itk.org/ItkSoftwareGuide.pdf

in particular the "Image Iterators" Chapter.



The code that you should use is


   typedef itk::ImageRegionIterator< ImageType > IteratorType;

   IteratorType  it( image, image->GetBufferedRegion() );

   it.GoToBegin();

   while( !it.IsAtEnd() )
     {
     if( it.Get()  > 127 )
       {
       it.Set( 255 );
       }
     else
       {
       it.Set( 0 );
       }
     ++it;
     }



That said,

This functionality is already available
in the BinaryThresholdImageFilter.
http://www.itk.org/Insight/Doxygen/html/classitk_1_1BinaryThresholdImageFilter.html



     Regards,



         Luis



-------------------------
Gabriela de Miranda wrote:
> Hi Luiz,
> 
> I got what i needed using pixelIndex and the getpixel(pixelIndex) 
> methods. I'm participating on a project and my work is implement a 
> threshold filter using the coordinates x, y, z of a voxel. Is something 
> like:
> 
> unsigned long c = image->GetLargestPossibleRegion().GetSize()[0];
> unsigned long r = image->GetLargestPossibleRegion().GetSize()[1];
> unsigned long s = image->GetLargestPossibleRegion().GetSize()[2];
>      
>      for (int z = 0; z < s; z++) {
>          for (int x = 0; x < c ; x++) {
>           for (int y = 0; y < r; y++) {
> 
>             pixelIndex[0] = x; //x
>             pixelIndex[1] = y; //y
>             pixelIndex[2] = z; //z
> 
>             if(image->GetBufferedRegion().IsInside(pixelIndex))
>             {
>                 pixelValue = image->GetPixel(pixelIndex);
>                
>                 if (pixelValue > 127)
>                     image->SetPixel(pixelIndex,255);
>                 else
>                     image->SetPixel(pixelIndex,0);
>             }
>           }
>       }
>   }
> 
> Is that right?
> 
> 
> Thanks!!
> 
> 
> 2009/6/30 Luis Ibanez <luis.ibanez at kitware.com 
> <mailto:luis.ibanez at kitware.com>>
> 
>     Gabriela,
> 
>     You don't need to use ImageVoxel.
> 
>     Please follow Charlotte's advice and use
> 
>          image->TransformIndexToPhysicalPoint( index, point )
> 
>     You will find more information about this in the
>     ITK Software Guide
> 
>        http://www.itk.org/ItkSoftwareGuide.pdf
> 
>     in the "Data Representation" chapter
> 
> 
>     Regards,
> 
> 
>            Luis
> 
> 
>     ----------------------------------------------------------------------------
>     On Thu, Jun 25, 2009 at 12:44 PM, Charlotte Curtis
>     <c.f.curtis at gmail.com <mailto:c.f.curtis at gmail.com>> wrote:
> 
>         On Wed, Jun 24, 2009 at 12:07 PM, Gabriela de
>         Miranda<gabrielademiranda at gmail.com
>         <mailto:gabrielademiranda at gmail.com>> wrote:
>          > Hi all!
>          > I need to access the coordinates x, y and z of a voxel.
> 
>         How are you getting the information about which voxel?  Do you know
>         the index?  If so, you can just use
>         itk::Image::TransformIndexToPhysicalPoint.  If you don't know the
>         index, you can use an iterator (with index) to find the voxels you
>         need.
> 
>          > I guess that the class ImageVoxel is exactly what i need..
>          > How can I use it?
> 
>         I don't think you're supposed to use that class directly, but
>         I'm not
>         sure on that one.
> 
>         Good luck,
> 
>         Charlotte
>         _____________________________________
>         Powered by www.kitware.com <http://www.kitware.com>
> 
>         Visit other Kitware open-source projects at
>         http://www.kitware.com/opensource/opensource.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
> 
> 
> 


More information about the Insight-users mailing list