Skip to content

ImageIteratorDemo

vtk-examples/Cxx/ImageData/ImageIteratorDemo


Description

vtkImageIterator is an efficient way to access the regions of a vtkImageData. A span in the vrkImageData is a continuous segment of pixels. The NextSpan() method increments the iterator to the next continuous segment.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ImageIteratorDemo.cxx

#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageIterator.h>
#include <vtkImageViewer2.h>
#include <vtkInteractorStyleImage.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create an image data
  vtkNew<vtkImageData> imageData;

  // Specify the size of the image data
  imageData->SetDimensions(100, 200, 30);
  imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);

  // Fill every entry of the image data with a color
  int* dims = imageData->GetDimensions();

  unsigned char* ptr =
      static_cast<unsigned char*>(imageData->GetScalarPointer(0, 0, 0));
  unsigned char r, g, b, a;
  colors->GetColor("Banana", r, g, b, a);
  for (int z = 0; z < dims[2]; z++)
  {
    for (int y = 0; y < dims[1]; y++)
    {
      for (int x = 0; x < dims[0]; x++)
      {
        *ptr++ = r;
        *ptr++ = g;
        *ptr++ = b;
      }
    }
  }

  // Define the extent to be modified
  int extent[6];
  extent[0] = 20;
  extent[1] = 50;
  extent[2] = 30;
  extent[3] = 60;
  extent[4] = 10;
  extent[5] = 20;

  // Set the entries in the region to another color
  colors->GetColor("Tomato", r, g, b, a);
  vtkImageIterator<unsigned char> it(imageData, extent);
  unsigned int counter = 0;
  while (!it.IsAtEnd())
  {
    unsigned char* valIt = it.BeginSpan();
    unsigned char* valEnd = it.EndSpan();
    while (valIt != valEnd)
    {
      // Increment for each component
      *valIt++ = r;
      *valIt++ = g;
      *valIt++ = b;
    }
    it.NextSpan();
    ++counter;
  }
  std::cout << "# of spans: " << counter << std::endl;

  std::cout << "Increments: " << imageData->GetIncrements()[0] << ", "
            << imageData->GetIncrements()[1] << ", "
            << imageData->GetIncrements()[2] << std::endl;
  vtkIdType incX, incY, incZ;
  imageData->GetContinuousIncrements(extent, incX, incY, incZ);
  std::cout << "ContinuousIncrements: " << incX << ", " << incY << ", " << incZ
            << std::endl;

  // Visualize
  vtkNew<vtkImageViewer2> imageViewer;
  imageViewer->SetInputData(imageData);

  vtkNew<vtkInteractorStyleImage> style;
  style->SetInteractionModeToImageSlicing();

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetInteractorStyle(style);
  imageViewer->SetupInteractor(renderWindowInteractor);
  imageViewer->SetSlice((extent[5] - extent[4]) / 2 + extent[4]);

  imageViewer->GetRenderer()->SetBackground(
      colors->GetColor3d("Slate_grey").GetData());
  imageViewer->GetImageActor()->InterpolateOff();

  imageViewer->Render();
  imageViewer->GetRenderer()->ResetCamera();
  imageViewer->GetRenderWindow()->SetWindowName("ImageIteratorDemo");

  imageViewer->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageIteratorDemo)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkInteractionImage
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ImageIteratorDemo: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(ImageIteratorDemo MACOSX_BUNDLE ImageIteratorDemo.cxx )
  target_link_libraries(ImageIteratorDemo PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImageIteratorDemo MACOSX_BUNDLE ImageIteratorDemo.cxx )
  target_link_libraries(ImageIteratorDemo PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImageIteratorDemo
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ImageIteratorDemo

Click here to download ImageIteratorDemo and its CMakeLists.txt file. Once the tarball ImageIteratorDemo.tar has been downloaded and extracted,

cd ImageIteratorDemo/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ImageIteratorDemo

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.