Skip to content


# ImageRange3D

VTKEx/Cxx/Images/ImageRange3D

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

ImageRange3D.cxx

    #include <vtkSmartPointer.h>
    #include <vtkImageRange3D.h>

    #include <vtkImageReader2Factory.h>
    #include <vtkImageReader2.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkInteractorStyleImage.h>
    #include <vtkRenderer.h>
    #include <vtkImageMapper3D.h>
    #include <vtkImageActor.h>
    #include <vtkImageCast.h>

    int main(int argc, char* argv[])
    {
      // Parse input arguments
      if ( argc != 2 )
      {
        std::cout << "Required parameters: Filename.jpg" << std::endl;
        return EXIT_FAILURE;
      }

      // Read the image
      vtkSmartPointer<vtkImageReader2Factory> readerFactory =
        vtkSmartPointer<vtkImageReader2Factory>::New();
      vtkSmartPointer<vtkImageReader2> reader;
      reader.TakeReference(
        readerFactory->CreateImageReader2(argv[1]));
      reader->SetFileName(argv[1]);

      // Create actors
      vtkSmartPointer<vtkImageActor> originalActor =
        vtkSmartPointer<vtkImageActor>::New();
      originalActor->GetMapper()->SetInputConnection(
        reader->GetOutputPort());

      vtkSmartPointer<vtkImageRange3D> rangeFilter =
        vtkSmartPointer<vtkImageRange3D>::New();
      rangeFilter->SetInputConnection(reader->GetOutputPort());
      rangeFilter->SetKernelSize(5,5,5);
      rangeFilter->Update();

      vtkSmartPointer<vtkImageCast> rangeCastFilter =
        vtkSmartPointer<vtkImageCast>::New();
      rangeCastFilter->SetInputConnection(rangeFilter->GetOutputPort());
      rangeCastFilter->SetOutputScalarTypeToUnsignedChar();
      rangeCastFilter->Update();

      vtkSmartPointer<vtkImageActor> rangeActor =
        vtkSmartPointer<vtkImageActor>::New();
      rangeActor->GetMapper()->SetInputConnection(
        rangeCastFilter->GetOutputPort());

       // Define viewport ranges
      // (xmin, ymin, xmax, ymax)
      double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
      double rangeViewport[4] = {0.5, 0.0, 1.0, 1.0};

      // Setup renderers
      vtkSmartPointer<vtkRenderer> originalRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      originalRenderer->SetViewport(originalViewport);
      originalRenderer->AddActor(originalActor);
      originalRenderer->ResetCamera();
      originalRenderer->SetBackground(.4, .5, .6);

      vtkSmartPointer<vtkRenderer> rangeRenderer =
        vtkSmartPointer<vtkRenderer>::New();
      rangeRenderer->SetViewport(rangeViewport);
      rangeRenderer->AddActor(rangeActor);
      rangeRenderer->ResetCamera();
      rangeRenderer->SetBackground(.4, .5, .7);

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetSize(600, 300);
      renderWindow->AddRenderer(originalRenderer);
      renderWindow->AddRenderer(rangeRenderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

      renderWindowInteractor->SetInteractorStyle(style);

      renderWindowInteractor->SetRenderWindow(renderWindow);
      renderWindow->Render();
      renderWindowInteractor->Initialize();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ImageRange3D)

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

### Download and Build ImageRange3D

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

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

    ./ImageRange3D

WINDOWS USERS

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