VTK/Examples/Cxx/Broken/IO/ImageReader2

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search

ImageReader2.cxx

#include <vtkSmartPointer.h>
#include <vtkImageReader2.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
 
int main(int argc, char *argv[])
{
  // Verify command line arguments
  if(argc < 2)
    {
    std::cout << "Usage: " << argv[0]
              << " InputFilename" << std::endl;
    return EXIT_FAILURE;
    }
 
  // Parse command line arguments
  std::string inputFilename = argv[1];
 
  // Read file
  vtkSmartPointer<vtkImageReader2> reader =
    vtkSmartPointer<vtkImageReader2>::New();
  reader->SetFileName(inputFilename.c_str());
  reader->SetDataScalarTypeToUnsignedChar();
  reader->Update();
 
  // Create an actor
  vtkSmartPointer<vtkImageActor> actor =
    vtkSmartPointer<vtkImageActor>::New();
  actor->SetInput(reader->GetOutput());
 
  // Setup renderer
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderer->ResetCamera();
 
  // Setup render window
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
 
  // Setup render window interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
 
  renderWindowInteractor->SetInteractorStyle(style);
 
  // Render and start interaction
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
 
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

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

Download and Build ImageReader2

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

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

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

./ImageReader2

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.