通常处理医学图像,使用VTK库,VTK库在官网下载,并经过Cmake,编译并配置好环境变量后使用,下文提供使用VTK读取三维图像并显示的代码,经过调试可运行。
// RAWREAD.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkDICOMImageReader.h>
#include<vtkMarchingCubes.h>
#include<vtkPolyDataMapper.h>
#include<vtkStripper.h>
#include<vtkActor.h>
#include<vtkProperty.h>
#include<vtkCamera.h>
#include<vtkOutlineFilter.h>
#include<vtkOBJExporter.h>
#include<vtkRenderer.h>
#include<vtkMetaImageReader.h>
#include<vtkInteractorStyleTrackballCamera.h>
#include<iostream>
#include<string.h>
//需要进行初始化,否则会报错
#include <vtkAutoInit.h>
#include<vtkRenderingVolumeOpenGL2ObjectFactory.h>
#include<vtkRenderingOpenGL2ObjectFactory.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
int main(void)
{
vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
vtkObjectFactory::RegisterFactory(vtkRenderingVolumeOpenGL2ObjectFactory::New());
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();//WINDOW;
renWin->AddRenderer(ren);
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();//wininteratcor;
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName("D:/CT_Brain");
reader->SetDataByteOrderToLittleEndian();
reader->Update();
cout << "读取数据完毕" << endl;
cout << "The width is" << reader->GetWidth() << endl;
cout << "The height is" << reader->GetHeight() << endl;
cout << "The depth is" << reader->GetPixelSpacing() << endl;
cout << "The Output port is" << reader->GetOutputPort() << endl;
vtkSmartPointer<vtkMarchingCubes> marchingcube = vtkSmartPointer<vtkMarchingCubes>::New();
marchingcube->SetInputConnection(reader->GetOutputPort());//获得读取的数据的点集;
marchingcube->SetValue(0, 200);//Setting the threshold;
marchingcube->ComputeNormalsOn();//计算表面法向量;
vtkSmartPointer<vtkStripper> Stripper = vtkSmartPointer<vtkStripper>::New();
Stripper->SetInputConnection(marchingcube->GetOutputPort());//获取三角片
vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();//将三角片映射为几何数据;
Mapper->SetInputConnection(Stripper->GetOutputPort());
Mapper->ScalarVisibilityOff();//
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();//Created a actor;
actor->SetMapper(Mapper);//获得皮肤几何数据
actor->GetProperty()->SetDiffuseColor(1, .49, .25);//设置皮肤颜色;
actor->GetProperty()->SetSpecular(0.3);//反射率;
actor->GetProperty()->SetOpacity(1.0);//透明度;
actor->GetProperty()->SetSpecularPower(20);//反射光强度;
actor->GetProperty()->SetColor(1, 0, 0);//设置角的颜色;
actor->GetProperty()->SetRepresentationToWireframe();//线框;
//vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();//Setting the Camera;
//camera->SetViewUp(0, 0, -1);//设置相机向上方向;
//camera->SetPosition(0, 1, 0);//位置:世界坐标系,相机位置;
//camera->SetFocalPoint(0, 0, 0);//焦点,世界坐标系,控制相机方向;
//camera->ComputeViewPlaneNormal();//重置视平面方向,基于当前的位置和焦点;
vtkSmartPointer<vtkOutlineFilter> outfilterline = vtkSmartPointer<vtkOutlineFilter>::New();
outfilterline->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> outmapper = vtkSmartPointer<vtkPolyDataMapper>::New();
outmapper->SetInputConnection(outfilterline->GetOutputPort());
vtkSmartPointer<vtkActor> OutlineActor = vtkSmartPointer<vtkActor>::New();
OutlineActor->SetMapper(outmapper);
OutlineActor->GetProperty()->SetColor(0, 0, 0);//线框颜色
ren->AddActor(actor);
ren->AddActor(OutlineActor);
//ren->SetActiveCamera(camera);//设置渲染器的相机;
ren->ResetCamera();
ren->ResetCameraClippingRange();
//camera->Dolly(1.5);//使用Dolly()方法延伸着视平面法向移动相机;
ren->SetBackground(1, 1, 1);//设置背景颜色;
renWin->SetSize(1000, 600);
vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
renWin->Render();
iren->Initialize();
iren->Start();
vtkSmartPointer<vtkOBJExporter> porter = vtkSmartPointer<vtkOBJExporter>::New();
porter->SetFilePrefix("D:/polywrite.obj");//重建图像输出
porter->SetInput(renWin);
porter->Write();
return EXIT_SUCCESS;
}
运行结果:
序列医学图像: