VoxelsOnBoundary
VTKEx/Cxx/ImageData/VoxelsOnBoundary
Description¶
This example uses vtkMultiThreshold to extract voxels that are inside an isosurface and on the boundary of the isosurface. The result is a vtkUnstructuredGrid for each set of voxels. Before processing, vtkImageShrink3D reduces the resolution by a factor of 4.
Compare these results with
usage
VoxelsOnBoundary FullHead.mhd
Info
Compare these results with MedicalDemo1 that extracts the surface using vtkMarchingCubes to extract an interpolated isosurface.
Info
The example uses FullHead.mhd which references FullHead.raw.gz.
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¶
VoxelsOnBoundary.cxx
#include <vtkSmartPointer.h>
#include <vtkMultiThreshold.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkUnstructuredGrid.h>
#include <vtkMetaImageReader.h>
#include <vtkImageDataToPointSet.h>
#include <vtkPlane.h>
#include <vtkImageShrink3D.h>
#include <vtkImageData.h>
#include <vtkPointSet.h>
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <string>
#include <vector>
#include <iostream>
int main (int argc, char *argv[])
{
auto reader =
vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName (argv[1]);
reader->Update();
auto shrink =
vtkSmartPointer<vtkImageShrink3D>::New();
shrink->SetShrinkFactors(4, 4, 4);
shrink->SetInputConnection(reader->GetOutputPort());
shrink->Update();
// Create a PointSet from the ImageData
auto imageDataToPointSet =
vtkSmartPointer<vtkImageDataToPointSet>::New();
imageDataToPointSet->SetInputConnection(reader->GetOutputPort());
imageDataToPointSet->SetInputConnection(shrink->GetOutputPort());
imageDataToPointSet->Update();
// Extract voxels on the border between the inside and outside.
auto threshold =
vtkSmartPointer<vtkMultiThreshold>::New();
// Inside points have one or more points above the isosurface
int insideId = threshold->AddIntervalSet(
501, 20000,
vtkMultiThreshold::CLOSED, vtkMultiThreshold::CLOSED,
vtkDataObject::FIELD_ASSOCIATION_POINTS, "ImageScalars",
0, 0);
// Border points have points that straddle the boundary
int borderId = threshold->AddIntervalSet(
499.9999, 501.0000,
vtkMultiThreshold::OPEN, vtkMultiThreshold::OPEN,
vtkDataObject::FIELD_ASSOCIATION_POINTS, "ImageScalars",
0, 0);
threshold->SetInputData(imageDataToPointSet->GetOutput());
// Select the intervals to be output
threshold->OutputSet(insideId);
threshold->OutputSet(borderId);
threshold->Update();
// Visualize
auto colors =
vtkSmartPointer<vtkNamedColors>::New();
//vtkColor3d outsideColor = colors->GetColor3d("Crimson");
vtkColor3d insideColor = colors->GetColor3d("Banana");
vtkColor3d borderColor = colors->GetColor3d("Mint");
//vtkColor3d surfaceColor = colors->GetColor3d("Peacock");
vtkColor3d backgroundColor = colors->GetColor3d("Silver");
auto plane =
vtkSmartPointer<vtkPlane>::New();
plane->SetOrigin(dynamic_cast<vtkUnstructuredGrid*>(vtkMultiBlockDataSet::SafeDownCast(threshold->GetOutput()->GetBlock(insideId))->GetBlock(0))->GetCenter());
plane->SetNormal(1.0, -1.0, -1.0);
// Inside
auto insideMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
insideMapper->SetInputData(
dynamic_cast<vtkUnstructuredGrid*>(vtkMultiBlockDataSet::SafeDownCast(threshold->GetOutput()->GetBlock(insideId))->GetBlock(0)));
insideMapper->ScalarVisibilityOff();
auto insideActor =
vtkSmartPointer<vtkActor>::New();
insideActor->SetMapper(insideMapper);
insideActor->GetProperty()->SetDiffuseColor(insideColor.GetData());
insideActor->GetProperty()->SetSpecular(.6);
insideActor->GetProperty()->SetSpecularPower(30);
insideActor->GetProperty()->EdgeVisibilityOn();
// Border
auto borderMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
borderMapper->SetInputData(
dynamic_cast<vtkUnstructuredGrid*>(vtkMultiBlockDataSet::SafeDownCast(threshold->GetOutput()->GetBlock(borderId))->GetBlock(0)));
borderMapper->ScalarVisibilityOff();
auto borderActor =
vtkSmartPointer<vtkActor>::New();
borderActor->SetMapper(borderMapper);
borderActor->GetProperty()->SetDiffuseColor(borderColor.GetData());
borderActor->GetProperty()->SetSpecular(.6);
borderActor->GetProperty()->SetSpecularPower(30);
borderActor->GetProperty()->EdgeVisibilityOn();
auto renderer =
vtkSmartPointer<vtkRenderer>::New();
auto renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
auto renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->SetBackground(backgroundColor.GetData());
renderer->UseHiddenLineRemovalOn();
renderer->AddActor(insideActor);
renderer->AddActor(borderActor);
renderWindow->SetWindowName("CellsOnBoundary");
renderWindow->Render();
// Setup a good view
renderer->GetActiveCamera()->SetViewUp (0, 0, -1);
renderer->GetActiveCamera()->SetPosition (0, -1, 0);
renderer->GetActiveCamera()->SetFocalPoint (0, 0, 0);
renderer->GetActiveCamera()->Azimuth(30.0);
renderer->GetActiveCamera()->Elevation(30.0);
renderer->ResetCamera ();
renderer->GetActiveCamera()->Dolly(1.5);
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(VoxelsOnBoundary)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersGeneral
vtkvtkIOImage
vtkvtkImagingCore
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping VoxelsOnBoundary: ${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(VoxelsOnBoundary MACOSX_BUNDLE VoxelsOnBoundary.cxx )
target_link_libraries(VoxelsOnBoundary PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(VoxelsOnBoundary MACOSX_BUNDLE VoxelsOnBoundary.cxx )
target_link_libraries(VoxelsOnBoundary PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS VoxelsOnBoundary
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build VoxelsOnBoundary¶
Click here to download VoxelsOnBoundary and its CMakeLists.txt file. Once the tarball VoxelsOnBoundary.tar has been downloaded and extracted,
cd VoxelsOnBoundary/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:
./VoxelsOnBoundary
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.