Skip to content

ImageContinuousErode3D

Repository source: ImageContinuousErode3D


Question

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

Code

ImageContinuousErode3D.cxx

#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageContinuousErode3D.h>
#include <vtkImageData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPNGReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

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

  vtkNew<vtkImageData> image;

  if (argc < 2)
  {
    std::array<double, 3> drawColor1{0, 0, 0};
    std::array<double, 3> drawColor2{0, 0, 0};
    auto color1 = colors->GetColor3ub("Black").GetData();
    auto color2 = colors->GetColor3ub("Red").GetData();
    for (auto i = 0; i < 3; ++i)
    {
      drawColor1[i] = color1[i];
      drawColor2[i] = color2[i];
    }

    // Create an image
    vtkNew<vtkImageCanvasSource2D> source;
    source->SetScalarTypeToUnsignedChar();
    source->SetExtent(0, 200, 0, 200, 0, 0);
    source->SetDrawColor(drawColor1.data());
    source->FillBox(0, 200, 0, 200);
    source->SetDrawColor(drawColor2.data());
    source->FillBox(100, 150, 100, 150);
    source->Update();
    image->ShallowCopy(source->GetOutput());
  }
  else
  {
    vtkNew<vtkPNGReader> reader;
    reader->SetFileName(argv[1]);
    reader->Update();
    image->ShallowCopy(reader->GetOutput());
  }

  vtkNew<vtkImageContinuousErode3D> erodeFilter;
  erodeFilter->SetInputData(image);
  erodeFilter->SetKernelSize(10, 10, 1);
  erodeFilter->Update();

  vtkNew<vtkDataSetMapper> originalMapper;
  originalMapper->SetInputData(image);
  originalMapper->Update();

  vtkNew<vtkActor> originalActor;
  originalActor->SetMapper(originalMapper);

  vtkNew<vtkDataSetMapper> erodedMapper;
  erodedMapper->SetInputConnection(erodeFilter->GetOutputPort());
  erodedMapper->Update();

  vtkNew<vtkActor> erodedActor;
  erodedActor->SetMapper(erodedMapper);

  // Visualize
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->SetWindowName("ImageContinuousErode3D");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  vtkNew<vtkRenderer> leftRenderer;
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(colors->GetColor3d("Sienna").GetData());

  vtkNew<vtkRenderer> rightRenderer;
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(colors->GetColor3d("RoyalBlue").GetData());

  leftRenderer->AddActor(originalActor);
  rightRenderer->AddActor(erodedActor);

  leftRenderer->ResetCamera();
  rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ImageContinuousErode3D)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  IOImage
  ImagingMorphological
  ImagingSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

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

Download and Build ImageContinuousErode3D

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

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

./ImageContinuousErode3D

WINDOWS USERS

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