Skip to content

CombineImages

vtk-examples/Cxx/Images/CombineImages


Description

This example takes two images and superimposes them. The opacity of each image can be set to control how they are combined.

Question

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

Code

CombineImages.cxx

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

#include <string>

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

  // Parse input arguments
  if (argc != 3)
  {
    std::cerr << "Usage: " << argv[0]
              << " Input1Filename Input2Filename  e.g. Ox.jpg Gourds2.jpg"
              << std::endl;
    return EXIT_FAILURE;
  }

  // Read the images
  vtkNew<vtkImageReader2Factory> readerFactory;
  vtkSmartPointer<vtkImageReader2> imgReader1;
  imgReader1.TakeReference(readerFactory->CreateImageReader2(argv[1]));
  imgReader1->SetFileName(argv[1]);

  vtkSmartPointer<vtkImageReader2> imgReader2;
  imgReader2.TakeReference(readerFactory->CreateImageReader2(argv[2]));
  imgReader2->SetFileName(argv[2]);

  // Combine the images (blend takes multiple connections on the 0th input port)
  vtkNew<vtkImageBlend> blend;
  blend->AddInputConnection(imgReader1->GetOutputPort());
  blend->AddInputConnection(imgReader2->GetOutputPort());
  blend->SetOpacity(0, 0.5);
  blend->SetOpacity(1, 0.5);

  // Display the result
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;

  vtkNew<vtkImageViewer2> imageViewer;
  imageViewer->SetInputConnection(blend->GetOutputPort());
  imageViewer->SetupInteractor(renderWindowInteractor);
  imageViewer->GetRenderer()->ResetCamera();
  imageViewer->GetRenderer()->SetBackground(
      colors->GetColor3d("Peru").GetData());
  imageViewer->GetRenderWindow()->SetWindowName("CombineImages");

  imageViewer->GetRenderer()->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(CombineImages)

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

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

Download and Build CombineImages

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

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

./CombineImages

WINDOWS USERS

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