Skip to content

CheckerboardWidget

vtk-examples/Cxx/Widgets/CheckerboardWidget


Description

Compare two images with a checkerboard. The widget permits interactive control of the number of checkers in the x/y directions. Checkerboards are often used to compare the results of image registration. For an alternative image comparison widget try the RectilinearWipeWidget.

Question

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

Code

CheckerboardWidget.cxx

#include <vtkCheckerboardRepresentation.h>
#include <vtkCheckerboardWidget.h>
#include <vtkCommand.h>
#include <vtkImageActor.h>
#include <vtkImageCheckerboard.h>
#include <vtkImageData.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int argc, char* argv[])
{
  if (argc < 3)
  {
    std::cerr << "Usage: " << argv[0]
              << " Input1Filename Input2Filename e.g. Gourds2.jpg, Ox.jpg"
              << std::endl;
    return EXIT_FAILURE;
  }

  // Read the images
  vtkNew<vtkJPEGReader> reader1;
  reader1->SetFileName(argv[1]);

  vtkNew<vtkJPEGReader> reader2;
  reader2->SetFileName(argv[2]);

  // Create a checker pipeline
  vtkNew<vtkImageCheckerboard> checker;
  checker->SetInputConnection(0, reader1->GetOutputPort());
  checker->SetInputConnection(1, reader2->GetOutputPort());
  checker->SetNumberOfDivisions(3, 3, 1);

  // Create the RenderWindow, Renderer and both Actors
  //
  vtkNew<vtkNamedColors> colors;
  vtkNew<vtkRenderer> ren1;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);
  renWin->SetWindowName("CheckerboardWidget");

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  vtkNew<vtkImageActor> checkerActor;
  checkerActor->GetMapper()->SetInputConnection(checker->GetOutputPort());

  // VTK widgets consist of two parts: the widget part that handles
  // event processing; and the widget representation that defines how
  // the widget appears in the scene
  // (i.e., matters pertaining to geometry).
  vtkNew<vtkCheckerboardWidget> checkerWidget;
  checkerWidget->SetInteractor(iren);

  vtkCheckerboardRepresentation* checkerWidgetRep =
      static_cast<vtkCheckerboardRepresentation*>(
          checkerWidget->GetRepresentation());

  checkerWidgetRep->SetImageActor(checkerActor);
  checkerWidgetRep->SetCheckerboard(checker);

  // Add the actors to the renderer, set the background and size
  //
  ren1->AddActor(checkerActor);
  ren1->SetBackground(colors->GetColor3d("Wheat").GetData());
  renWin->SetSize(300, 300);

  // render the image
  //
  iren->Initialize();
  renWin->Render();
  checkerWidget->On();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(CheckerboardWidget)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOImage
  vtkImagingGeneral
  vtkInteractionStyle
  vtkInteractionWidgets
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

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

Download and Build CheckerboardWidget

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

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

./CheckerboardWidget

WINDOWS USERS

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