Skip to content

ImageMapToColors

vtk-examples/Cxx/Images/ImageMapToColors


Question

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

Code

ImageMapToColors.cxx

//
// Displays a "grayscale" image as a full color image via the
// vtkImageMapToColors filter, which uses a lookup table to
// map scalar values to colors
//
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageMapToColors.h>
#include <vtkImageMapper3D.h>
#include <vtkImageProperty.h>
#include <vtkInteractorStyleImage.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

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

  // Create a "grayscale" 16x16 image, 1-component pixels of type "double"
  vtkNew<vtkImageData> image;
  int imageExtent[6] = {0, 15, 0, 15, 0, 0};
  image->SetExtent(imageExtent);
  image->AllocateScalars(VTK_DOUBLE, 1);

  double scalarvalue = 0.0;

  for (int y = imageExtent[2]; y <= imageExtent[3]; y++)
  {
    for (int x = imageExtent[0]; x <= imageExtent[1]; x++)
    {
      double* pixel = static_cast<double*>(image->GetScalarPointer(x, y, 0));
      pixel[0] = scalarvalue;
      scalarvalue += 1.0;
    }
  }

  // Map the scalar values in the image to colors with a lookup table:
  vtkNew<vtkLookupTable> lookupTable;
  lookupTable->SetNumberOfTableValues(256);
  lookupTable->SetRange(0.0, 255.0);
  lookupTable->Build();

  // Pass the original image and the lookup table to a filter to create
  // a color image:
  vtkNew<vtkImageMapToColors> scalarValuesToColors;
  scalarValuesToColors->SetLookupTable(lookupTable);
  scalarValuesToColors->PassAlphaToOutputOn();
  scalarValuesToColors->SetInputData(image);

  // Create an image actor
  vtkNew<vtkImageActor> imageActor;
  imageActor->GetMapper()->SetInputConnection(
      scalarValuesToColors->GetOutputPort());
  imageActor->GetProperty()->SetInterpolationTypeToNearest();

  // Visualize
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(imageActor);
  renderer->ResetCamera();
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("ImageMapToColors");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleImage> style;

  renderWindowInteractor->SetInteractorStyle(style);

  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageMapToColors)

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

if (NOT VTK_FOUND)
  message("Skipping ImageMapToColors: ${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(ImageMapToColors MACOSX_BUNDLE ImageMapToColors.cxx )
  target_link_libraries(ImageMapToColors PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImageMapToColors MACOSX_BUNDLE ImageMapToColors.cxx )
  target_link_libraries(ImageMapToColors PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImageMapToColors
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ImageMapToColors

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

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

./ImageMapToColors

WINDOWS USERS

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