[vtkusers] Iterating through a vtkImageData
David Doria
daviddoria+vtk at gmail.com
Sun Nov 1 16:34:00 EST 2009
On Sun, Nov 1, 2009 at 4:21 PM, David Feng <dfeng at cs.unc.edu> wrote:
> 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
David,
So GetScalarComponentAsFloat (int x, int y, int z, int component) is
specifically for 3d images? What do you do if you have a 2d image? Do
you simply set z=0? What is 'int component' that you have set to 0?
Also, you mention if I know what kind of data it is I can cast it and
then use type specific access. I'm assuming you mean to cast either to
a StructuredPoints or UniformGrid since these are the only two
apparent subclasses of vtkImageData. I am currently particularly at
the output of vtkVoxelModeller
(http://www.vtk.org/doc/nightly/html/classvtkVoxelModeller-members.html).
It is a vtkImageAlgorithm - I was assuming its GetOutput() would
return a vtkImageData, but how would I tell if the type is more
specific? Looking at both of those subclasses I actually don't see any
better looking accessors - can you elaborate/give an example of what
you meant?
Thanks for the help so far!
David
More information about the vtkusers
mailing list