vtk读取序列医学Dicom图片进行体绘制(vtkVolumeRayCastCompositeFunction类)
我看大家现在问得多说两个类的文件没有,我现在就着重说明下,当时这个代码是在vtk5.10下编写的,至于现在的vtk版本迭代这么快,希望大家还是先看看官网的体绘制的代码(https://lorensen.github.io/VTKExamples/site/Cxx/#volume-rendering),此代码仅供参考吧。
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkActor.h"
#include "vtkSmartPointer.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkDICOMImageReader.h"
#include "vtkImageCast.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
void main()
{
vtkSmartPointer<vtkDICOMImageReader>dicomImagereader=vtkSmartPointer<vtkDICOMImageReader>::New();
dicomImagereader->SetDirectoryName("E:\\tougu");
dicomImagereader->SetDataByteOrderToLittleEndian();
vtkSmartPointer<vtkImageCast>readerImageCast=vtkSmartPointer<vtkImageCast>::New();
readerImageCast->SetInputConnection(dicomImagereader->GetOutputPort());
readerImageCast->SetOutputScalarTypeToUnsignedShort();
readerImageCast->Update();
vtkSmartPointer<vtkPiecewiseFunction>opactiyTransferFunction=vtkSmartPointer<vtkPiecewiseFunction>::New();
opactiyTransferFunction->AddPoint(120,0.0);
opactiyTransferFunction->AddPoint(250,1.0);
opactiyTransferFunction->AddPoint(520,1.0);
opactiyTransferFunction->AddPoint(650,0.0);
vtkSmartPointer<vtkColorTransferFunction>colorTransferFunction=vtkSmartPointer<vtkColorTransferFunction>::New();
colorTransferFunction->AddRGBPoint(120, 255/255.0, 98/255.0, 98/255.0);
colorTransferFunction->AddRGBPoint(250, 255/255.0, 255/255.0, 180/255.0);
colorTransferFunction->AddRGBPoint(520, 1.0, 1.0, 1.0);
colorTransferFunction->AddRGBPoint(650, 1.0, 1.0, 1.0);
vtkSmartPointer<vtkPiecewiseFunction>gradientTransferFunction=vtkSmartPointer<vtkPiecewiseFunction>::New();
gradientTransferFunction->AddPoint(120, 2.0);
gradientTransferFunction->AddPoint(250, 2.0);
gradientTransferFunction->AddPoint(520, 0.1);
gradientTransferFunction->AddPoint(650, 0.1);
vtkSmartPointer<vtkVolumeProperty>volumeProperty=vtkSmartPointer<vtkVolumeProperty>::New();
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opactiyTransferFunction);
volumeProperty->SetGradientOpacity(gradientTransferFunction);
volumeProperty->ShadeOn();//阴影
//volumeProperty->SetInterpolationTypeToLinear();//直线与样条插值之间逐发函数
volumeProperty->SetAmbient(0.2);//环境光系数
volumeProperty->SetDiffuse(0.9);//漫反射
volumeProperty->SetSpecular(0.2);//高光系数
volumeProperty->SetSpecularPower(10);//高光强度
vtkSmartPointer<vtkVolumeRayCastCompositeFunction>compositeRaycastFunction=vtkSmartPointer<vtkVolumeRayCastCompositeFunction>::New();
vtkSmartPointer<vtkVolumeRayCastMapper>volumeMapper=vtkSmartPointer<vtkVolumeRayCastMapper>::New();
volumeMapper->SetVolumeRayCastFunction(compositeRaycastFunction);//载入体绘制方法
volumeMapper->SetInputConnection(readerImageCast->GetOutputPort());
/*fixedPointVolumeMapper=vtkFixedPointVolumeRayCastMapper::New();
fixedPointVolumeMapper->SetInput(dicomImagereader->GetOutput());*/
vtkSmartPointer<vtkVolume>volume=vtkSmartPointer<vtkVolume>::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);//设置体属性
vtkSmartPointer<vtkRenderer>ren1=vtkSmartPointer<vtkRenderer>::New();
ren1->AddVolume(volume);
ren1->SetBackground(1,1,1);
vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
renWin->SetSize(800,800);
vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
renWin->Render();
iren->Start();
}
读取序列的Dicom图片并用vtkVolumeRayCastCompositeFunction进行体绘制,其关键就在于不透明函数参数,以及颜色参数的设置

如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com,更多博文干货等着您。