GenerateCubesFromLabels
Repository source: GenerateCubesFromLabels
Description¶
Sometimes it is helpful to view the results of a segmentation without any post processing. This example converts the point data from a labeled volume into cell data. The surfaces are displayed as vtkPolydata. If you want to created smoothed polydata models from your segmented volumes, see the example GenerateModelsFromLabels. The input volume must be in MetaIO format.
Usage: GenerateCubesFromLabels InputVolume.mhd StartLabel EndLabel
where
InputVolume is a meta file containing a 3 volume of discrete labels.
StartLabel is the first label to be processed
EndLabel is the last label to be processed
NOTE: There can be gaps in the labeling. If a label does not exist in the volume, it will be skipped.
Note
This original source code for this example is here.
Other languages
See (Python), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
GenerateCubesFromLabels.cxx
//
// GenerateCubesFromLabels
// Usage: GenerateCubesFromLabels InputVolume Startlabel Endlabel
// where
// InputVolume is a meta file containing a 3 volume of
// discrete labels.
// StartLabel is the first label to be processed
// EndLabel is the last label to be processed
// NOTE: There can be gaps in the labeling. If a label does
// not exist in the volume, it will be skipped.
//
//
#include <vtkCamera.h>
#include <vtkCellData.h>
#include <vtkGeometryFilter.h>
#include <vtkImageData.h>
#include <vtkImageWrapPad.h>
#include <vtkMetaImageReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkThreshold.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <iostream>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
if (argc < 4)
{
std::cout
<< "Usage: " << argv[0]
<< " InputVolume StartLabel EndLabel e.g. Frog/frogtissue.mhd 1 29"
<< std::endl;
return EXIT_FAILURE;
}
// Define all of the variables
unsigned int startLabel = atoi(argv[2]);
unsigned int endLabel = atoi(argv[3]);
// Generate cubes from labels.
// 1) Read the meta file
// 2) Convert point data to cell data
// 3) Convert to geometry and display
vtkNew<vtkMetaImageReader> reader;
reader->SetFileName(argv[1]);
reader->Update();
// Pad the volume so that we can change the point data into cell.
// data.
int* extent = reader->GetOutput()->GetExtent();
vtkNew<vtkImageWrapPad> pad;
pad->SetInputConnection(reader->GetOutputPort());
pad->SetOutputWholeExtent(extent[0], extent[1] + 1, extent[2], extent[3] + 1,
extent[4], extent[5] + 1);
pad->Update();
// Copy the scalar point data of the volume into the scalar cell data.
pad->GetOutput()->GetCellData()->SetScalars(
reader->GetOutput()->GetPointData()->GetScalars());
vtkNew<vtkThreshold> selector;
selector->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_CELLS,
vtkDataSetAttributes::SCALARS);
selector->SetInputConnection(pad->GetOutputPort());
selector->SetLowerThreshold(startLabel);
selector->SetUpperThreshold(endLabel);
selector->Update();
// Shift the geometry by 1/2.
vtkNew<vtkTransform> transform;
transform->Translate(-0.5, -0.5, -0.5);
vtkNew<vtkTransformFilter> transformModel;
transformModel->SetTransform(transform);
transformModel->SetInputConnection(selector->GetOutputPort());
vtkNew<vtkGeometryFilter> geometry;
geometry->SetInputConnection(transformModel->GetOutputPort());
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(geometry->GetOutputPort());
mapper->SetScalarRange(startLabel, endLabel);
mapper->SetScalarModeToUseCellData();
mapper->SetColorModeToMapScalars();
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("GenerateCubesFromLabels");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("DarkSlateBlue").GetData());
renderWindow->Render();
auto camera = renderer->GetActiveCamera();
camera->SetPosition(42.301174, 939.893457, -124.005030);
camera->SetFocalPoint(224.697134, 221.301653, 146.823706);
camera->SetViewUp(0.262286, -0.281321, -0.923073);
camera->SetDistance(789.297581);
camera->SetClippingRange(168.744328, 1509.660206);
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(GenerateCubesFromLabels)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
CommonTransforms
FiltersCore
FiltersGeneral
FiltersGeometry
IOImage
ImagingCore
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "GenerateCubesFromLabels: 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(GenerateCubesFromLabels MACOSX_BUNDLE GenerateCubesFromLabels.cxx )
target_link_libraries(GenerateCubesFromLabels PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS GenerateCubesFromLabels
MODULES ${VTK_LIBRARIES}
)
Download and Build GenerateCubesFromLabels¶
Click here to download GenerateCubesFromLabels and its CMakeLists.txt file. Once the tarball GenerateCubesFromLabels.tar has been downloaded and extracted,
cd GenerateCubesFromLabels/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:
./GenerateCubesFromLabels
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.