├── readme.md └── vtk_3d.cpp /readme.md: -------------------------------------------------------------------------------- 1 | # 3D reconstruction based on MarchingCubes of VTK 2 | 3 | # Environment 4 | - Visual studio 2015 or later version 5 | - VTK 9.0.1, or other version 6 | 7 | 8 | # depends libs of VTK 9 | vtkIOLegacy-9.0d.lib 10 | vtkIOExport-9.0d.lib 11 | vtkCommonCore-9.0d.lib 12 | vtkRenderingVolumeOpenGL2-9.0d.lib 13 | vtkRenderingVolume-9.0d.lib 14 | vtkCommonColor-9.0d.lib 15 | vtkRenderingVolume-9.0d.lib 16 | vtkInteractionWidgets-9.0d.lib 17 | vtkFiltersGeneral-9.0d.lib 18 | vtkCommonTransforms-9.0d.lib 19 | vtkRenderingFreeType-9.0d.lib 20 | vtkCommonDataModel-9.0d.lib 21 | vtkInteractionStyle-9.0d.lib 22 | vtkFiltersCore-9.0d.lib 23 | vtkIOImage-9.0d.lib 24 | vtkDICOMParser-9.0d.lib 25 | vtkCommonExecutionModel-9.0d.lib 26 | vtkCommonCore-9.0d.lib 27 | vtkFiltersModeling-9.0d.lib 28 | vtkDICOMParser-9.0d.lib 29 | vtkRenderingCore-9.0d.lib 30 | 31 | # Misc 32 | + The example uses MarchingCubes for 3D reconstuction. 33 | + Be sure to set build INSTALL when you build VTK. 34 | + Be sure to set CMAKE_INSTALL_PREFIX on cmake to make sure all headers and libs could be collected to the set folder. 35 | + If you 3D figure doesn't look good, try to change the parameter 100 on line 62 to some other value. 36 | ```C 37 | skinExtractor->SetValue(0, 100); 38 | ``` 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /vtk_3d.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Zhiqiang Liang 3 | * Email: dpstill@126.com 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "vtkRenderer.h" 14 | #include "vtkRenderWindow.h" 15 | #include "vtkRenderWindowInteractor.h" 16 | #include "vtkDICOMImageReader.h" 17 | #include "vtkPolyDataMapper.h" 18 | #include "vtkActor.h" 19 | #include "vtkOutlineFilter.h" 20 | #include "vtkCamera.h" 21 | #include "vtkProperty.h" 22 | #include "vtkMarchingCubes.h" 23 | #include "vtkStripper.h" 24 | #include "vtkSmartPointer.h" 25 | #include "vtkImageData.h" 26 | #include "vtkNamedColors.h" 27 | #include "vtkAutoInit.h" 28 | 29 | VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2) 30 | VTK_MODULE_INIT(vtkRenderingFreeType) 31 | VTK_MODULE_INIT(vtkInteractionStyle) 32 | 33 | int main(int argc, char *argv[]) 34 | { 35 | 36 | //const char *dirname = "C:/__2"; 37 | //const char * dirname = "D:\\d___3"; 38 | const char *dirname = "D:\\dataset\\SE1"; 39 | int clip = 0; 40 | 41 | //------------------------------- Read DICOM images 42 | vtkSmartPointer reader = vtkSmartPointer::New(); 43 | vtkSmartPointer input = vtkSmartPointer::New(); 44 | vtkSmartPointer dicomReader = vtkSmartPointer::New(); 45 | dicomReader->SetDirectoryName(dirname); 46 | dicomReader->Update(); 47 | input = dicomReader->GetOutput(); 48 | reader = dicomReader; 49 | 50 | //------------------------------- Set the colors. 51 | vtkSmartPointer colors = vtkSmartPointer::New(); 52 | std::array skinColor{ { 255, 125, 64 } }; 53 | colors->SetColor("SkinColor", skinColor.data()); 54 | std::array bkg{ { 51, 77, 102, 255 } }; 55 | colors->SetColor("BkgColor", bkg.data()); 56 | 57 | //------------------------------- Skin 58 | // An isosurface, or contour value of 500 is known to correspond to the skin of the patient. 59 | // The triangle stripper is used to create triangle strips from the isosurface; these render much faster on many systems. 60 | vtkSmartPointer skinExtractor = vtkSmartPointer::New(); 61 | skinExtractor->SetInputConnection(reader->GetOutputPort()); 62 | skinExtractor->SetValue(0, 100); 63 | 64 | vtkSmartPointer skinStripper = vtkSmartPointer::New(); 65 | skinStripper->SetInputConnection(skinExtractor->GetOutputPort()); 66 | 67 | vtkSmartPointer skinMapper = vtkSmartPointer::New(); 68 | skinMapper->SetInputConnection(skinStripper->GetOutputPort()); 69 | skinMapper->ScalarVisibilityOff(); 70 | 71 | vtkSmartPointer skin = vtkSmartPointer::New(); 72 | skin->SetMapper(skinMapper); 73 | skin->GetProperty()->SetDiffuseColor(colors->GetColor3d("SkinColor").GetData()); 74 | skin->GetProperty()->SetSpecular(.3); 75 | skin->GetProperty()->SetSpecularPower(20); 76 | skin->GetProperty()->SetOpacity(.5); 77 | 78 | #if 1 79 | //------------------------------- Bone 80 | // An isosurface, or contour value of 1150 is known to correspond to the bone of the patient. 81 | // The triangle stripper is used to create triangle strips from the isosurface; these render much faster on may systems. 82 | vtkSmartPointer boneExtractor = vtkSmartPointer::New(); 83 | boneExtractor->SetInputConnection(reader->GetOutputPort()); 84 | boneExtractor->SetValue(0, 300); 85 | 86 | vtkSmartPointer boneStripper = vtkSmartPointer::New(); 87 | boneStripper->SetInputConnection(boneExtractor->GetOutputPort()); 88 | 89 | vtkSmartPointer boneMapper = vtkSmartPointer::New(); 90 | boneMapper->SetInputConnection(boneStripper->GetOutputPort()); 91 | boneMapper->ScalarVisibilityOff(); 92 | 93 | vtkSmartPointer bone = vtkSmartPointer::New(); 94 | bone->SetMapper(boneMapper); 95 | bone->GetProperty()->SetDiffuseColor(colors->GetColor3d("Ivory").GetData()); 96 | #endif 97 | 98 | //------------------------------- Outline // An outline provides context around the data. 99 | vtkSmartPointer outlineData = vtkSmartPointer::New(); 100 | outlineData->SetInputConnection(reader->GetOutputPort()); 101 | 102 | vtkSmartPointer mapOutline = vtkSmartPointer::New(); 103 | mapOutline->SetInputConnection(outlineData->GetOutputPort()); 104 | 105 | vtkSmartPointer outline = vtkSmartPointer::New(); 106 | outline->SetMapper(mapOutline); 107 | outline->GetProperty()->SetColor(colors->GetColor3d("Black").GetData()); 108 | 109 | //------------------------------- camera 110 | // It is convenient to create an initial view of the data. The FocalPoint and Position form a vector direction. Later on (ResetCamera() method) 111 | // this vector is used to position the camera to look at the data in this direction. 112 | vtkSmartPointer aCamera = vtkSmartPointer::New(); 113 | aCamera->SetViewUp(0, 0, -1); 114 | aCamera->SetPosition(0, -1, 0); 115 | aCamera->SetFocalPoint(0, 0, 0); 116 | aCamera->ComputeViewPlaneNormal(); 117 | aCamera->Azimuth(30.0); 118 | aCamera->Elevation(30.0); 119 | 120 | //------------------------------- create winwow 121 | // Create the renderer, the render window, and the interactor. 122 | // The renderer draws into the render window, the interactor enables mouse- and keyboard-based interaction with the data within the render window. 123 | vtkSmartPointer aRenderer = vtkSmartPointer::New(); 124 | vtkSmartPointer renWin = vtkSmartPointer::New(); 125 | renWin->AddRenderer(aRenderer); 126 | vtkSmartPointer iren = vtkSmartPointer::New(); 127 | iren->SetRenderWindow(renWin); 128 | 129 | 130 | //------------------------------- display 131 | // Actors are added to the renderer. An initial camera view is created. The Dolly() method moves the camera towards the FocalPoint, thereby enlarging the image. 132 | //aRenderer->AddActor(outline); 133 | aRenderer->AddActor(skin); 134 | //aRenderer->AddActor(bone); 135 | aRenderer->SetActiveCamera(aCamera); 136 | aRenderer->ResetCamera(); 137 | aCamera->Dolly(1.5); 138 | 139 | // Set a background color for the renderer and set the size of the render window (expressed in pixels). 140 | aRenderer->SetBackground(colors->GetColor3d("BkgColor").GetData()); 141 | renWin->SetSize(640, 480); 142 | 143 | // Note that when camera movement occurs (as it does in the Dolly() method), the clipping planes often need adjusting. Clipping planes consist of two planes: near and far along the view direction. 144 | // The near plane clips out objects in front of the plane; the far plane clips out objects behind the plane. This way only what is drawn between the planes is actually rendered. 145 | aRenderer->ResetCameraClippingRange(); 146 | 147 | // Initialize the event loop and then start it. 148 | renWin->Render(); 149 | iren->Initialize(); 150 | iren->Start(); 151 | 152 | return EXIT_SUCCESS; 153 | } 154 | 155 | 156 | 157 | 158 | 159 | 160 | --------------------------------------------------------------------------------