[vtkusers] Iterating through a vtkImageData

David Feng dfeng at cs.unc.edu
Sun Nov 1 16:21:38 EST 2009


This is slow, but I usually do something along these lines:

int* dims = image->GetDimensions();
for (int z=0; z<dims[2]; z++)
  {
  for (int y=0; y<dims[1]; y++)
    {
    for (int x=0; x<dims[0]; x++)
      {
      // zero is the component, add another loop if you have more
      // than one component
      double v = image->GetScalarComponentAsDouble(x,y,z,0);
     
      // do something with v
      }
    }
  }

GetScalarComponentAsDouble(...) does a lot of range checking and type 
conversion, so if you want to be faster you can pull out the point data 
and do the indexing yourself:

pd = image->GetPointData()->GetScalars();

for (int i=0; i<pd->GetNumberOfTuples(); i++)
  {
  // etc
  }

They are indexed x first, then y, then z.  If you happen to know what 
type your data is, cast the data array as such and you'll have 
type-specific access.

Is this what you wanted?  I've never used vtkImageIterator, so I can't 
help you there.

David

David Doria wrote:
> I found vtkImageIterator, but from looking at the Test, I saw something like:
>
>   int ext[3] = { 0, 0, 0};
>   vtkImageData *id = vtkImageData::New();
>   id->SetExtent(ext);
>   vtkImageIterator<float*> *it = new vtkImageIterator<float*>(id,ext);
>
> When I try that, I get:
> undefined reference to
> `vtkImageIterator<float*>::vtkImageIterator(vtkImageData*, int*)'
>
> If I try to use the "vtk style" object creation:
> vtkSmartPointer<vtkImageIterator<float*> > iterator =
> vtkSmartPointer<vtkImageIterator<float*> >::New();
>
> I get:
> 'New' is not a member of 'vtkImageIterator<float*>'
>
> What I am trying to do is simply visit every voxel of a 3d
> vtkImageData. It would also be nice to access a particular voxel in an
> (x,y,z) type format. Can anyone comment on how to do either of these
> things?
>
> Thanks,
>
> David
> _______________________________________________
> Powered by 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 VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>




More information about the vtkusers mailing list