Skip to content

IterateImageData

Repository source: IterateImageData

Description

This example demonstrates how to set and access locations in a 3D image.

Question

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

Code

IterateImageData.cxx

#include <vtkImageData.h>
#include <vtkIntArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>

int main(int, char*[])
{
  // Create an image data
  vtkNew<vtkImageData> imageData;

  // Specify the size of the image data
  imageData->SetDimensions(2, 3, 1);
  imageData->AllocateScalars(VTK_DOUBLE, 1);

  const int* dims = imageData->GetDimensions();
  // int dims[3]; // can't do this

  std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1]
            << " z: " << dims[2] << std::endl;

  std::cout << "Number of points: " << imageData->GetNumberOfPoints()
            << std::endl;
  std::cout << "Number of cells: " << imageData->GetNumberOfCells()
            << std::endl;

  // Fill every entry of the image data with "2.0"
  for (int z = 0; z < dims[2]; z++)
  {
    for (int y = 0; y < dims[1]; y++)
    {
      for (int x = 0; x < dims[0]; x++)
      {
        double* pixel =
            static_cast<double*>(imageData->GetScalarPointer(x, y, z));
        pixel[0] = 2.0;
      }
    }
  }

  // Retrieve the entries from the image data and print them to the screen
  for (int z = 0; z < dims[2]; z++)
  {
    for (int y = 0; y < dims[1]; y++)
    {
      for (int x = 0; x < dims[0]; x++)
      {
        const double* pixel =
            static_cast<double*>(imageData->GetScalarPointer(x, y, z));
        // do something with v
        std::cout << pixel[0] << " ";
      }
      std::cout << std::endl;
    }
    std::cout << std::endl;
  }

  // Access the data linearly

  vtkNew<vtkImageData> image;
  image->SetExtent(0, 1, 0, 1, 0, 0);
  image->AllocateScalars(VTK_INT, 1);

  int* pixel;

  pixel = static_cast<int*>(image->GetScalarPointer(0, 0, 0));
  pixel[0] = 1;

  pixel = static_cast<int*>(image->GetScalarPointer(1, 0, 0));
  pixel[0] = 2;

  pixel = static_cast<int*>(image->GetScalarPointer(0, 1, 0));
  pixel[0] = 3;

  pixel = static_cast<int*>(image->GetScalarPointer(1, 1, 0));
  pixel[0] = 4;

  vtkIntArray* scalars = dynamic_cast<vtkIntArray*>(
      image->GetPointData()->GetArray("ImageScalars"));
  std::cout << "Scalars has " << scalars->GetNumberOfComponents()
            << "components " << std::endl;
  std::cout << " Scalars has " << scalars->GetNumberOfTuples() << " tuples"
            << std::endl;

  for (vtkIdType i = 0; i < scalars->GetNumberOfTuples(); i++)
  {
    std::cout << scalars->GetValue(i) << std::endl;
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(IterateImageData)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "IterateImageData: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(IterateImageData MACOSX_BUNDLE IterateImageData.cxx )
  target_link_libraries(IterateImageData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS IterateImageData
  MODULES ${VTK_LIBRARIES}
)

Download and Build IterateImageData

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

cd IterateImageData/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:

./IterateImageData

WINDOWS USERS

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