Skip to content

ImageWarp

vtk-examples/Cxx/Images/ImageWarp


Description

Combining the imaging and visualization pipelines to deform an image in the z-direction. The vtkMergeFilter is used to combine the warped surface with the original color data.

Other languages

See (Python)

Question

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

Code

ImageWarp.cxx

/*
 * This example shows how to combine data from both the imaging
 *  and graphics pipelines. The vtkMergeData filter is used to
 *  merge the data from each together.
 */
#include <vtkActor.h>
#include <vtkBMPReader.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkImageLuminance.h>
#include <vtkMergeFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkWarpScalar.h>

#include <array>
#include <iomanip>
#include <iostream>
#include <string>

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

  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " fileName" << std::endl;
    std::cout << "where: fileName is the masonry.bmp file." << std::endl;
    return EXIT_FAILURE;
  }

  std::string fileName = argv[1];

  // Set the background color.
  std::array<unsigned char, 4> bkg{{60, 93, 144, 255}};
  colors->SetColor("BkgColor", bkg.data());

  // Read in an image and compute a luminance value-> The image is extracted
  // as a set of polygons (vtkImageDataGeometryFilter). We then will
  // warp the plane using the scalar (luminance) values.
  //
  vtkNew<vtkBMPReader> reader;
  reader->SetFileName(fileName.c_str());
  // Convert the image to a grey scale.
  vtkNew<vtkImageLuminance> luminance;
  luminance->SetInputConnection(reader->GetOutputPort());
  // Pass the data to the pipeline as polygons.
  vtkNew<vtkImageDataGeometryFilter> geometry;
  geometry->SetInputConnection(luminance->GetOutputPort());
  // Warp the data in a direction perpendicular to the image plane.
  vtkNew<vtkWarpScalar> warp;
  warp->SetInputConnection(geometry->GetOutputPort());
  warp->SetScaleFactor(-0.1);

  // Use vtkMergeFilter to combine the original image with the warped geometry.
  vtkNew<vtkMergeFilter> merge;
  merge->SetGeometryConnection(warp->GetOutputPort());
  merge->SetScalarsConnection(reader->GetOutputPort());
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(merge->GetOutputPort());
  mapper->SetScalarRange(0, 255);
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  // Create the rendering window, renderer, and interactive renderer.
  //
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);

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

  // Add the actors to the renderer, set the background and size.
  ren->AddActor(actor);
  ren->ResetCamera();
  ren->SetBackground(colors->GetColor3d("BkgColor").GetData());
  // ren->GetActiveCamera()->Azimuth(20);
  // ren->GetActiveCamera()->Elevation(30);
  // ren->ResetCameraClippingRange();
  // ren->GetActiveCamera()->Zoom(1.3);
  ren->GetActiveCamera()->SetPosition(-100, -130, 325);
  ren->GetActiveCamera()->SetFocalPoint(105, 114, -29);
  ren->GetActiveCamera()->SetViewUp(0.51, 0.54, 0.67);
  ren->ResetCameraClippingRange();

  renWin->SetSize(512, 512);
  renWin->SetWindowName("ImageWarp");

  // Render the image.
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ImageWarp)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersGeneral
  FiltersGeometry
  IOImage
  ImagingColor
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ImageWarp: 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(ImageWarp MACOSX_BUNDLE ImageWarp.cxx )
  target_link_libraries(ImageWarp PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ImageWarp
  MODULES ${VTK_LIBRARIES}
)

Download and Build ImageWarp

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

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

./ImageWarp

WINDOWS USERS

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