ImageContinuousDilate3D
Repository source: ImageContinuousDilate3D
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImageContinuousDilate3D.cxx
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageContinuousDilate3D.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<vtkImageContinuousDilate3D> dilateFilter;
dilateFilter->SetInputData(image);
dilateFilter->SetKernelSize(10, 10, 1);
dilateFilter->Update();
vtkNew<vtkDataSetMapper> originalMapper;
originalMapper->SetInputData(image);
originalMapper->Update();
vtkNew<vtkActor> originalActor;
originalActor->SetMapper(originalMapper);
vtkNew<vtkDataSetMapper> dilatedMapper;
dilatedMapper->SetInputConnection(dilateFilter->GetOutputPort());
dilatedMapper->Update();
vtkNew<vtkActor> dilatedActor;
dilatedActor->SetMapper(dilatedMapper);
// 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("ImageContinuousDilate3D");
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(dilatedActor);
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImageContinuousDilate3D)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
IOImage
ImagingMorphological
ImagingSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImageContinuousDilate3D: 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(ImageContinuousDilate3D MACOSX_BUNDLE ImageContinuousDilate3D.cxx )
target_link_libraries(ImageContinuousDilate3D PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImageContinuousDilate3D
MODULES ${VTK_LIBRARIES}
)
Download and Build ImageContinuousDilate3D¶
Click here to download ImageContinuousDilate3D and its CMakeLists.txt file. Once the tarball ImageContinuousDilate3D.tar has been downloaded and extracted,
cd ImageContinuousDilate3D/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:
./ImageContinuousDilate3D
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.