ImageCheckerboard

VTKExamples/Cxx/Images/ImageCheckerboard


Description

This example compares two images using a checkerboard pattern.

Code

ImageCheckerboard.cxx

#include <vtkImageCheckerboard.h>
#include <vtkSmartPointer.h>

#include <vtkImageReader2Factory.h>
#include <vtkImageReader2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageData.h>
#include <vtkImageViewer2.h>
#include <vtkNamedColors.h>

int main ( int argc, char* argv[] )
{
  // Verify input arguments
  if ( argc != 3 )
  {
    std::cerr << "Usage: " << argv[0]
              << " Input1Filename Input2Filename" << std::endl;
    return EXIT_FAILURE;
  }

  // Read the images
  vtkSmartPointer<vtkImageReader2Factory> readerFactory =
    vtkSmartPointer<vtkImageReader2Factory>::New();
  vtkSmartPointer<vtkImageReader2> reader1 =
    readerFactory->CreateImageReader2(argv[1]);
  reader1->SetFileName(argv[1]);

  vtkSmartPointer<vtkImageReader2> reader2 =
    readerFactory->CreateImageReader2(argv[2]);
  reader2->SetFileName(argv[2]);

  vtkSmartPointer<vtkImageCheckerboard> checkerboardFilter =
    vtkSmartPointer<vtkImageCheckerboard>::New();
  checkerboardFilter->SetInputConnection(0, reader1->GetOutputPort());
  checkerboardFilter->SetInputConnection(1, reader2->GetOutputPort());
  checkerboardFilter->SetNumberOfDivisions(3,3,1);

  // Visualize
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();

  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();
  vtkSmartPointer<vtkImageViewer2> imageViewer =
    vtkSmartPointer<vtkImageViewer2>::New();
  imageViewer->SetInputConnection(checkerboardFilter->GetOutputPort());
  imageViewer->SetupInteractor(renderWindowInteractor);
  imageViewer->GetRenderer()->ResetCamera();
  imageViewer->GetRenderer()->SetBackground(
    colors->GetColor3d("Wheat").GetData());
  imageViewer->GetRenderWindow()->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageCheckerboard)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOImage
  vtkImagingGeneral
  vtkInteractionImage
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping ImageCheckerboard: ${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(ImageCheckerboard MACOSX_BUNDLE ImageCheckerboard.cxx )
  target_link_libraries(ImageCheckerboard PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImageCheckerboard MACOSX_BUNDLE ImageCheckerboard.cxx )
  target_link_libraries(ImageCheckerboard PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImageCheckerboard
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build ImageCheckerboard

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

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

./ImageCheckerboard

WINDOWS USERS

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