Skip to content

ImageToPolyDataFilter

vtk-examples/Cxx/Images/ImageToPolyDataFilter


Question

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

Code

ImageToPolyDataFilter.cxx

#include <vtkActor.h>
#include <vtkImageQuantizeRGBToIndex.h>
#include <vtkImageToPolyDataFilter.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPNGReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTriangleFilter.h>

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

  if (argc < 2)
  {
    std::cerr << "Required arguments: filename.png e.g. Gourds.png"
              << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkPNGReader> reader;
  reader->SetFileName(argv[1]);

  vtkNew<vtkImageQuantizeRGBToIndex> quant;
  quant->SetInputConnection(reader->GetOutputPort());
  quant->SetNumberOfColors(16);

  vtkNew<vtkImageToPolyDataFilter> i2pd;
  i2pd->SetInputConnection(quant->GetOutputPort());
  i2pd->SetLookupTable(quant->GetLookupTable());
  i2pd->SetColorModeToLUT();
  i2pd->SetOutputStyleToPolygonalize();
  i2pd->SetError(0);
  i2pd->DecimationOn();
  i2pd->SetDecimationError(0.0);
  i2pd->SetSubImageSize(25);

  // Need a triangle filter because the polygons are complex and concave
  vtkNew<vtkTriangleFilter> tf;
  tf->SetInputConnection(i2pd->GetOutputPort());

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(tf->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetRepresentationToWireframe();

  // Visualize
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());

  renderWindow->SetSize(300, 250);
  renderWindow->SetWindowName("ImageToPolyDataFilter");

  renderWindow->Render();
  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ImageToPolyDataFilter)

find_package(VTK COMPONENTS 
)

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

Download and Build ImageToPolyDataFilter

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

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

./ImageToPolyDataFilter

WINDOWS USERS

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