VTK/Examples/Cxx/Images/InteractWithImage

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
VTK Examples Baseline Images TestInteractWithImage.png


This example shows how to display an image and zoom/pan/adjust brightness interactively. A picture of the bunny is provided for the demo.

InteractWithImage.cxx

#include <vtkJPEGReader.h>
#include <vtkImageMapper3D.h>
#include <vtkImageActor.h> // Note: this is a 3D actor (c.f. vtkImageMapper which is 2D)
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkSmartPointer.h>
 
int main(int argc, char* argv[])
{
  // Parse input arguments
  if ( argc != 2 )
  {
    std::cout << "Required parameters: Filename" << std::endl;
    return EXIT_FAILURE;
  }
  std::string inputFilename = argv[1];
 
  // Read the image
  vtkSmartPointer<vtkJPEGReader> reader = 
      vtkSmartPointer<vtkJPEGReader>::New();
  reader->SetFileName(inputFilename.c_str());
  reader->Update();
 
  // Create an actor
  vtkSmartPointer<vtkImageActor> actor = 
    vtkSmartPointer<vtkImageActor>::New();
  actor->GetMapper()->SetInputConnection(reader->GetOutputPort());
 
  // Setup renderer
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderer->ResetCamera();
 
  // Setup render window
  vtkSmartPointer<vtkRenderWindow> window = 
    vtkSmartPointer<vtkRenderWindow>::New();
  window->AddRenderer(renderer);
 
  // Setup render window interactor
  vtkSmartPointer<vtkRenderWindowInteractor> interactor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(window);
 
  // Setup interactor style (this is what implements the zooming, panning and brightness adjustment functionality)
  vtkSmartPointer<vtkInteractorStyleImage> style = 
    vtkSmartPointer<vtkInteractorStyleImage>::New();
  interactor->SetInteractorStyle(style);
 
  // Render and start interaction
  interactor->Start();
 
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(InteractWithImage)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
add_executable(InteractWithImage MACOSX_BUNDLE InteractWithImage.cxx)
 
if(VTK_LIBRARIES)
  target_link_libraries(InteractWithImage ${VTK_LIBRARIES})
else()
  target_link_libraries(InteractWithImage vtkHybrid vtkWidgets)
endif()

Download and Build InteractWithImage

Click here to download InteractWithImage. and its CMakeLists.txt file.

Once the tarball InteractWithImage.tar has been downloaded and extracted,

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

./InteractWithImage

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