[vtkusers] vtk image iterator

Jorge Perez josp.jorge at gmail.com
Thu Oct 16 10:31:27 EDT 2014


Hi Zein, attached is the example I have modified (there are some errors in
the wiki example). In TestImageInterpolator.cxx you can see that there are
2 loops one for spans and the inner along the span. If you execute the
example you should obtain the following output. In this sample image there
two slices with size 4x3.

val: 0 0 0 0
val: 0 0 0 0
val: 0 0 0 0
val: 1 1 1 1
val: 1 1 1 1
val: 1 1 1 1

best regards,


Jorge


2014-10-16 13:07 GMT+02:00 Zein Salah <zeinsalah at gmail.com>:

> well Jorge,
>
> this is actually what I am trying to understand.
>
> it seems the example works for 2d images. since I am using a similar code
> and it does not iterate in the third dimension,
>
> are spans meat to be slices?
>
> On Thu, Oct 16, 2014 at 12:23 PM, Jorge Perez <josp.jorge at gmail.com>
> wrote:
>
>> Hi, you can see how it is used in this example
>>
>> http://www.vtk.org/Wiki/VTK/Examples/Cxx/Broken/ImageData/vtkImageIterator
>>
>> regards,
>>
>> Jorge
>>
>> 2014-10-16 12:18 GMT+02:00 Zein Salah <zeinsalah at gmail.com>:
>>
>>> Hi,
>>>
>>> I have an understanding problem, I think I am missing something.
>>> what is a span iterator within the vtk image iterator? what is it needed
>>> for?
>>> I need to only iterate over an extent in the image and access data values
>>> within it. How should I move the iterator to the next position? how to
>>> access
>>> the underlying data?
>>>
>>> thx
>>>
>>>
>>> _______________________________________________
>>> 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://public.kitware.com/mailman/listinfo/vtkusers
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20141016/7900f8db/attachment.html>
-------------- next part --------------
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageIterator.h>
 
int main(int, char *[])
{
  // Create an image data
  vtkSmartPointer<vtkImageData> imageData =
      vtkSmartPointer<vtkImageData>::New();
 
  // Specify the size of the image data
  imageData->SetDimensions(4,3,2);
#if VTK_MAJOR_VERSION <= 5
  imageData->SetNumberOfScalarComponents(1);
  imageData->SetScalarTypeToDouble();
#else
  imageData->AllocateScalars(VTK_DOUBLE,1);
#endif
 
  // Fill every entry of the image data with "2.0"
  int* dims = imageData->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++)
        {
        imageData->SetScalarComponentFromDouble(x,y,z,0,double(z));
        }
      }
    }
 
  int extent[6];
  imageData->GetExtent(extent);
 
  // Retrieve the entries from the image data and print them to the screen
  vtkImageIterator<double> it(imageData, extent);
 
  for(double* val = it.BeginSpan(); !it.IsAtEnd(); it.NextSpan())
    {
    std::cout << "val:";
    for(; val != it.EndSpan(); ++val)
      {
      std::cout << " " << *val;
      }
    std::cout << std::endl;
    }

  return EXIT_SUCCESS;
}


More information about the vtkusers mailing list