开发工具 vs2017 qt 5.2.1 VTK 7.1.1
#include "vtkDICOMImageReader.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageCast.h"
#include "vtkImageShiftScale.h"
#include "vtkImageShrink3D.h"
#include "vtkImageThreshold.h"
#include "vtkImageIslandRemoval2D.h"
#include "vtkImageMathematics.h"
#include "vtkImageShrink3D.h"
#include <vtk.h>
void main()
{
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkDICOMImageReader *reader = vtkDICOMImageReader::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetDirectoryName("E://CT");
reader->SetDataOrigin(0.0, 0.0, 0.0);
vtkImageShrink3D *shrink = vtkImageShrink3D::New();
shrink->SetInputData((vtkDataObject *)reader->GetOutput());
shrink->SetShrinkFactors(2, 2, 1);//你要缩得更小
vtkImageShiftScale *ShiftScale1 = vtkImageShiftScale::New();
ShiftScale1->SetInputData((vtkDataObject *)shrink->GetOutput());
ShiftScale1->SetOutputScalarTypeToShort();
ShiftScale1->SetShift(1024);
ShiftScale1->ClampOverflowOn();
vtkImageCast *readerImageCast1 = vtkImageCast::New();
readerImageCast1->SetInputData((vtkDataObject *)ShiftScale1->GetOutput());
readerImageCast1->SetOutputScalarTypeToUnsignedShort();
readerImageCast1->ClampOverflowOn();
//////////////////////
vtkImageThreshold *threshold1 = vtkImageThreshold::New();
threshold1->SetInputConnection(shrink->GetOutputPort());
threshold1->ThresholdBetween(-500, 200);
threshold1->SetInValue(1);
threshold1->SetOutValue(0);
threshold1->ReplaceOutOn();
threshold1->Update();
vtkImageIslandRemoval2D *island = vtkImageIslandRemoval2D::New();
island->SetInputConnection(threshold1->GetOutputPort());
island->SetIslandValue(1);
island->SetReplaceValue(0);
island->SetAreaThreshold(7500);
island->SquareNeighborhoodOn();
vtkImageShiftScale *ShiftScale = vtkImageShiftScale::New();
ShiftScale->SetInputData((vtkDataObject *)island->GetOutput());
ShiftScale->SetOutputScalarTypeToUnsignedShort();
ShiftScale->ClampOverflowOn();
////////////////////////
vtkImageMathematics *mathematic = vtkImageMathematics::New();
mathematic->SetInputData((vtkDataObject *)readerImageCast1->GetOutput());
mathematic->SetInputData((vtkDataObject *)ShiftScale->GetOutput());
mathematic->SetOperationToMultiply();
reader->Delete();
vtkPiecewiseFunction *opacityTransferFunction = vtkPiecewiseFunction::New();
opacityTransferFunction->AddPoint(490, 0.0);
opacityTransferFunction->AddPoint(500, 0.0);
opacityTransferFunction->AddPoint(900, 0.0);
opacityTransferFunction->AddPoint(1024 - 100, 0.07);
opacityTransferFunction->AddPoint(1024, 0.01);
opacityTransferFunction->AddPoint(1024 + 100, 0.06);
opacityTransferFunction->AddPoint(1024 + 160, 0.3);
opacityTransferFunction->AddPoint(1024 + 200, 0.5);
opacityTransferFunction->ClampingOff();
vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New();
colorTransferFunction->AddRGBPoint(500.0, 1.0, 0.5, 0.2);
colorTransferFunction->AddRGBPoint(900.0, 0.80, 0.5, 0.3);
colorTransferFunction->AddRGBPoint(1000.0, 1.0, 0.9, 0.5);
colorTransferFunction->AddRGBPoint(1024 + 0.0, 0.0, 0.5, 0.0);
colorTransferFunction->AddRGBPoint(1024 + 60.0, 1.0, 0.0, 0.0);
// colorTransferFunction->AddRGBPoint(1024+60.0, 0.6, 0.1, 0.1);
colorTransferFunction->AddRGBPoint(1024 + 128.0, 0.2, 0.1, 0.9);
colorTransferFunction->AddRGBPoint(1024 + 196.0, 0.27, 0.21, 0.1);
colorTransferFunction->AddRGBPoint(1024 + 255.0, 0.8, 0.8, 0.8);
vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
//设定一个体绘容器的属性
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->ShadeOn();
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->SetAmbient(0.2);
volumeProperty->SetDiffuse(0.9);
volumeProperty->SetSpecular(0.2);
volumeProperty->SetSpecularPower(10);
/**/
vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New();
//运行沿着光线合成
vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New();
//体绘制器,特点:slow but accurate
volumeMapper->SetVolumeRayCastFunction(compositeFunction); //体绘光线投射函数
volumeMapper->SetInputConnection(mathematic->GetOutputPort());//图像数据输入
vtkVolume *volume = vtkVolume::New();
//表示透示图中的一组三维数据
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
ren->AddVolume(volume);
ren->SetBackground(1, 1, 1);
renWin->SetSize(600, 600);
renWin->Render();
// iren-> SetStillUpdateRate(0.5);
iren->SetDesiredUpdateRate(99);
iren->Initialize();
iren->Start();
ren->ResetCameraClippingRange();
volumeMapper->Delete();
iren->Delete();
ren->Delete();
renWin->Delete();
opacityTransferFunction->Delete();
volumeProperty->Delete();
compositeFunction->Delete();
volume->Delete();
colorTransferFunction->Delete();
}