#define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)
#include "vtkDICOMImageReader.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkMarchingCubes.h"
#include "vtkStripper.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkBoxWidget.h"
#include "vtkSmartPointer.h"
#include "vtkTriangleFilter.h"
#include "vtkMassProperties.h"
#include "vtkSmoothPolyDataFilter.h"
#include "vtkPolyDataNormals.h"
#include "vtkContourFilter.h"
#include "vtkRecursiveDividingCubes.h"
#include "vtkSTLWriter.h"
int main(int argc, char* argv[])
{
std::string filename = "out_Smooth.stl";//设置输出文件路径
//读取二维切片数据序列
vtkSmartPointer< vtkDICOMImageReader >reader =
vtkSmartPointer< vtkDICOMImageReader >::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDirectoryName("COU IV");//设置读取路径
reader->SetDataSpacing(1.0, 1.0, 1.0);//设置每个体素的大小
reader->Update();
//抽取等值面为骨头的信息
//MC算法
vtkSmartPointer< vtkMarchingCubes > boneExtractor =
vtkSmartPointer< vtkMarchingCubes >::New();
boneExtractor->SetInputConnection(reader->GetOutputPort());
boneExtractor->SetValue(0, 400); //设置提取的等值信息
boneExtractor->Update();
//利用ContourFilter提取等值面
/*vtkSmartPointer< vtkContourFilter > boneExtractor =
vtkSmartPointer< vtkContourFilter >::New();
boneExtractor->SetInputConnection(reader->GetOutputPort());
boneExtractor->SetValue(0, 200); //设置提取的等值信息
boneExtractor->Update();*/
//DC算法 耗时长,模型有明显缝隙
/*vtkSmartPointer< vtkRecursiveDividingCubes > boneExtractor =
vtkSmartPointer< vtkRecursiveDividingCubes >::New();
boneExtractor->SetInputConnection(reader->GetOutputPort());
boneExtractor->SetValue(500);
boneExtractor->SetDistance(1);
boneExtractor->SetIncrement(2);
boneExtractor->Update();*/
//剔除旧的或废除的数据单元,提高绘制速度(可略去这一步)
vtkSmartPointer< vtkStripper > boneStripper =
vtkSmartPointer< vtkStripper >::New(); //三角带连接
boneStripper->SetInputConnection(boneExtractor->GetOutputPort());
boneStripper->Update();
//平滑滤波
vtkSmartPointer<vtkSmoothPolyDataFilter> pSmoothPolyDataFilter = vtkSmartPointer<vtkSmoothPolyDataFilter>::New();
pSmoothPolyDataFilter->SetInputConnection(boneStripper->GetOutputPort());
//pSmoothPolyDataFilter->SetNumberOfIterations(m_nNumberOfIterations);
pSmoothPolyDataFilter->SetRelaxationFactor(0.05);
vtkSmartPointer<vtkPolyDataNormals> pPolyDataNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
pPolyDataNormals->SetInputConnection(pSmoothPolyDataFilter->GetOutputPort());
//pPolyDataNormals->SetFeatureAngle(m_nFeatureAngle);
//将模型输出到入STL文件
vtkSmartPointer<vtkSTLWriter> stlWriter =
vtkSmartPointer<vtkSTLWriter>::New();
stlWriter->SetFileName(filename.c_str());
stlWriter->SetInputConnection(pPolyDataNormals->GetOutputPort());
stlWriter->Write();
//建立映射
vtkSmartPointer< vtkPolyDataMapper > boneMapper =
vtkSmartPointer< vtkPolyDataMapper >::New();
boneMapper->SetInputData(pPolyDataNormals->GetOutput());
boneMapper->ScalarVisibilityOff();
//建立角色
vtkSmartPointer< vtkActor > bone =
vtkSmartPointer< vtkActor >::New();
bone->SetMapper(boneMapper);
bone->GetProperty()->SetDiffuseColor(1.0, 1.0, 1.0);
bone->GetProperty()->SetSpecular(.3);
bone->GetProperty()->SetSpecularPower(20);
//定义绘制器
vtkSmartPointer< vtkRenderer > aRenderer =
vtkSmartPointer< vtkRenderer >::New();
//定义绘制窗口
vtkSmartPointer< vtkRenderWindow > renWin =
vtkSmartPointer< vtkRenderWindow >::New();
renWin->AddRenderer(aRenderer);
//定义窗口交互器
vtkSmartPointer< vtkRenderWindowInteractor > iren =
vtkSmartPointer< vtkRenderWindowInteractor >::New();
iren->SetRenderWindow(renWin);
//创建一个camera
vtkSmartPointer< vtkCamera > aCamera =
vtkSmartPointer< vtkCamera >::New();
aCamera->SetViewUp(0, 0, -1);
aCamera->SetPosition(0, 1, 0);
aCamera->SetFocalPoint(0, 0, 0);
aRenderer->AddActor(bone);
aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
aCamera->Dolly(1.5);
aRenderer->SetBackground(0, 0, 0);
aRenderer->ResetCameraClippingRange();
//将3D模型渲染到绘制窗口
iren->Initialize();
iren->Start();
return 0;
}
01-31
1291
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-10