HeadSlice
VTKExamples/Cxx/VisualizationAlgorithms/HeadSlice
Description¶
This example generates contours on one CT sice through the ehad,
Info
See Figure 6-11a in Chapter 6 the VTK Textbook.
Other Languages
See (Python)
Code¶
HeadSlice.cxx
#include <vtkActor.h> #include <vtkCamera.h> #include <vtkContourFilter.h> #include <vtkExtractVOI.h> #include <vtkMetaImageReader.h> #include <vtkNamedColors.h> #include <vtkOutlineFilter.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> int main (int argc, char *argv[]) { if (argc < 2) { std::cout << "Usage: " << argv[0] << " FullHead.mhd" << std::endl; return EXIT_FAILURE; } // Create the RenderWindow, Renderer and both Actors // vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New(); vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); renWin->AddRenderer(ren1); vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); iren->SetRenderWindow(renWin); // create pipeline // vtkSmartPointer<vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New(); reader->SetFileName(argv[1]); reader->Update(); vtkSmartPointer<vtkExtractVOI> extractVOI = vtkSmartPointer<vtkExtractVOI>::New(); extractVOI->SetInputConnection(reader->GetOutputPort()); extractVOI->SetVOI(0, 255, 0, 255, 45, 45); vtkSmartPointer<vtkContourFilter> iso = vtkSmartPointer<vtkContourFilter>::New(); iso->SetInputConnection(extractVOI->GetOutputPort()); iso->GenerateValues(12, 500, 1150); vtkSmartPointer<vtkPolyDataMapper> isoMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); isoMapper->SetInputConnection(iso->GetOutputPort()); isoMapper->ScalarVisibilityOff(); vtkSmartPointer<vtkActor> isoActor = vtkSmartPointer<vtkActor>::New(); isoActor->SetMapper(isoMapper); isoActor->GetProperty()->SetColor(colors->GetColor3d("Wheat").GetData()); vtkSmartPointer<vtkOutlineFilter> outline = vtkSmartPointer<vtkOutlineFilter>::New(); outline->SetInputConnection(extractVOI->GetOutputPort()); vtkSmartPointer<vtkPolyDataMapper> outlineMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); outlineMapper->SetInputConnection(outline->GetOutputPort()); vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New(); outlineActor->SetMapper(outlineMapper); // Add the actors to the renderer, set the background and size // ren1->AddActor(outlineActor); ren1->AddActor(isoActor); ren1->SetBackground(colors->GetColor3d("SlateGray").GetData()); ren1->ResetCamera(); ren1->GetActiveCamera()->Dolly(1.5); ren1->ResetCameraClippingRange(); renWin->SetSize(640, 480); renWin->Render(); iren->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 2.8) PROJECT(HeadSlice) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) add_executable(HeadSlice MACOSX_BUNDLE HeadSlice.cxx ) target_link_libraries(HeadSlice ${VTK_LIBRARIES})
Download and Build HeadSlice¶
Click here to download HeadSlice and its CMakeLists.txt file. Once the tarball HeadSlice.tar has been downloaded and extracted,
cd HeadSlice/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:
./HeadSlice
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.