三维重建学习记录2-将CT数据集转化为三维模型
软件:VS2017+VTK8.2
先上代码:
1 #define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2) 2 #define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2) 3 #include "vtkDICOMImageReader.h" 4 #include "vtkRenderWindowInteractor.h" 5 #include "vtkRenderer.h" 6 #include "vtkRenderWindow.h" 7 #include "vtkMarchingCubes.h" 8 #include "vtkStripper.h" 9 #include "vtkActor.h" 10 #include "vtkPolyDataMapper.h" 11 #include "vtkProperty.h" 12 #include "vtkCamera.h" 13 #include "vtkBoxWidget.h" 14 #include "vtkSmartPointer.h" 15 #include "vtkTriangleFilter.h" 16 #include "vtkMassProperties.h" 17 #include "vtkSmoothPolyDataFilter.h" 18 #include "vtkPolyDataNormals.h" 19 #include "vtkContourFilter.h" 20 #include "vtkRecursiveDividingCubes.h" 21 #include "vtkSTLWriter.h" 22 23 int main(int argc, char* argv[]) 24 { 25 std::string filename = "out_Smooth.stl";//设置输出文件路径 26 27 //读取二维切片数据序列 28 vtkSmartPointer< vtkDICOMImageReader >reader = 29 vtkSmartPointer< vtkDICOMImageReader >::New(); 30 reader->SetDataByteOrderToLittleEndian(); 31 reader->SetDirectoryName("E://dicomimage");//设置读取路径 32 33 reader->SetDataSpacing(1.0, 1.0, 1.0);//设置每个体素的大小 34 reader->Update(); 35 36 37 //抽取等值面为骨头的信息 38 //MC算法 39 vtkSmartPointer< vtkMarchingCubes > boneExtractor = 40 vtkSmartPointer< vtkMarchingCubes >::New(); 41 boneExtractor->SetInputConnection(reader->GetOutputPort()); 42 boneExtractor->SetValue(0, 400); //设置提取的等值信息 43 boneExtractor->Update(); 44 45 46 //利用ContourFilter提取等值面 47 /*vtkSmartPointer< vtkContourFilter > boneExtractor = 48 vtkSmartPointer< vtkContourFilter >::New(); 49 boneExtractor->SetInputConnection(reader->GetOutputPort()); 50 boneExtractor->SetValue(0, 200); //设置提取的等值信息 51 boneExtractor->Update();*/ 52 53 //DC算法 耗时长,模型有明显缝隙 54 /*vtkSmartPointer< vtkRecursiveDividingCubes > boneExtractor = 55 vtkSmartPointer< vtkRecursiveDividingCubes >::New(); 56 boneExtractor->SetInputConnection(reader->GetOutputPort()); 57 boneExtractor->SetValue(500); 58 boneExtractor->SetDistance(1); 59 boneExtractor->SetIncrement(2); 60 boneExtractor->Update();*/ 61 62 63 64 //剔除旧的或废除的数据单元,提高绘制速度(可略去这一步) 65 vtkSmartPointer< vtkStripper > boneStripper = 66 vtkSmartPointer< vtkStripper >::New(); //三角带连接 67 boneStripper->SetInputConnection(boneExtractor->GetOutputPort()); 68 boneStripper->Update(); 69 70 //平滑滤波 71 vtkSmartPointer<vtkSmoothPolyDataFilter> pSmoothPolyDataFilter = vtkSmartPointer<vtkSmoothPolyDataFilter>::New(); 72 pSmoothPolyDataFilter->SetInputConnection(boneStripper->GetOutputPort()); 73 //pSmoothPolyDataFilter->SetNumberOfIterations(m_nNumberOfIterations); 74 pSmoothPolyDataFilter->SetRelaxationFactor(0.05); 75 76 vtkSmartPointer<vtkPolyDataNormals> pPolyDataNormals = vtkSmartPointer<vtkPolyDataNormals>::New(); 77 pPolyDataNormals->SetInputConnection(pSmoothPolyDataFilter->GetOutputPort()); 78 //pPolyDataNormals->SetFeatureAngle(m_nFeatureAngle); 79 80 81 //将模型输出到入STL文件 82 vtkSmartPointer<vtkSTLWriter> stlWriter = 83 vtkSmartPointer<vtkSTLWriter>::New(); 84 stlWriter->SetFileName(filename.c_str()); 85 stlWriter->SetInputConnection(pPolyDataNormals->GetOutputPort()); 86 stlWriter->Write(); 87 88 89 //建立映射 90 vtkSmartPointer< vtkPolyDataMapper > boneMapper = 91 vtkSmartPointer< vtkPolyDataMapper >::New(); 92 boneMapper->SetInputData(pPolyDataNormals->GetOutput()); 93 boneMapper->ScalarVisibilityOff(); 94 //建立角色 95 vtkSmartPointer< vtkActor > bone = 96 vtkSmartPointer< vtkActor >::New(); 97 bone->SetMapper(boneMapper); 98 99 bone->GetProperty()->SetDiffuseColor(1.0, 1.0, 1.0); 100 bone->GetProperty()->SetSpecular(.3); 101 bone->GetProperty()->SetSpecularPower(20); 102 103 //定义绘制器 104 vtkSmartPointer< vtkRenderer > aRenderer = 105 vtkSmartPointer< vtkRenderer >::New(); 106 //定义绘制窗口 107 vtkSmartPointer< vtkRenderWindow > renWin = 108 vtkSmartPointer< vtkRenderWindow >::New(); 109 renWin->AddRenderer(aRenderer); 110 //定义窗口交互器 111 vtkSmartPointer< vtkRenderWindowInteractor > iren = 112 vtkSmartPointer< vtkRenderWindowInteractor >::New(); 113 iren->SetRenderWindow(renWin); 114 115 //创建一个camera 116 vtkSmartPointer< vtkCamera > aCamera = 117 vtkSmartPointer< vtkCamera >::New(); 118 aCamera->SetViewUp(0, 0, -1); 119 aCamera->SetPosition(0, 1, 0); 120 aCamera->SetFocalPoint(0, 0, 0); 121 122 aRenderer->AddActor(bone); 123 aRenderer->SetActiveCamera(aCamera); 124 aRenderer->ResetCamera(); 125 aCamera->Dolly(1.5); 126 aRenderer->SetBackground(0, 0, 0); 127 aRenderer->ResetCameraClippingRange(); 128 129 //将3D模型渲染到绘制窗口 130 iren->Initialize(); 131 iren->Start(); 132 133 return 0; 134 }
注:运行后笔记本要等几分钟,等待渲染完成。运行时内存和CPU实时曲线一直在变。而且路径里的文件要全部是满足要求的CT图,不然会报错。

效果图:(显示的是断骨CT图重建效果)


浙公网安备 33010602011771号