The code is commented in detail, so I won’t go over the process.

`#include "vtkAutoInit.h" VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2 VTK_MODULE_INIT(vtkInteractionStyle); #include "vtkDICOMImageReader.h" #include <vtkSmartPointer.h> #include <vtkMetaImageReader.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 <vtkInteractorStyleTrackballCamera.h> #include <vtkImageData.h> VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2) void main() { VtkRenderer *ren = vtkRenderer::New(); VtkRenderWindow *renWin = vtkRenderWindow::New(); // AddRenderer to render window renWin->AddRenderer(ren); / / set the interactive window vtkRenderWindowInteractor * iren = vtkRenderWindowInteractor: : New (); Iren ->SetRenderWindow(renWin); / / camera vtkInteractorStyleTrackballCamera * style for interaction = vtkInteractorStyleTrackballCamera: : New (); Iren ->SetInteractorStyle(style); VtkSmartPointer <vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New(); reader->SetFileName("mhd_head/manix_small.mhd"); reader->Update(); vtkImageCast *readerImageCast = vtkImageCast::New(); readerImageCast->SetInputConnection(reader->GetOutputPort()); readerImageCast->SetOutputScalarTypeToUnsignedShort(); readerImageCast->ClampOverflowOn(); / / set the threshold / / set the opacity transfer function, this is a one-dimensional piecewise transfer function vtkPiecewiseFunction * opacityTransferFunction = vtkPiecewiseFunction: : New (); OpacityTransferFunction - > AddPoint (20, 0.0); OpacityTransferFunction - > AddPoint (255, 0.1); OpacityTransferFunction - > AddPoint (1005, 0.6); / / set the color transfer function vtkColorTransferFunction * colorTransferFunction = vtkColorTransferFunction: : New (); ColorTransferFunction - > AddRGBPoint (0.0, 0.0, 0.5, 0.0); ColorTransferFunction - > AddRGBPoint (60.0, 1.0, 0.0, 0.0); ColorTransferFunction - > AddRGBPoint (128.0, 0.2, 0.1, 0.9); ColorTransferFunction - > AddRGBPoint (196.0, 0.27, 0.21, 0.1); ColorTransferFunction - > AddRGBPoint (255.0, 0.8, 0.8, 0.8); vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New(); VolumeProperty ->SetColor(colorTransferFunction); VolumeProperty ->SetScalarOpacity(opacityTransferFunction); VolumeProperty ->ShadeOn(); / / shadow volumeProperty - > SetInterpolationTypeToLinear (); VolumeProperty ->SetAmbient(0.2); VolumeProperty ->SetDiffuse(0.4); // diffuse reflection volumeProperty->SetSpecular(0.6); // volumeProperty->SetSpecularPower(10); / / / / highlights strength definition light projection method for RayCasting vtkVolumeRayCastCompositeFunction * compositeFunction = volume rendering method vtkVolumeRayCastCompositeFunction::New(); / / define rendering Mapper vtkVolumeRayCastMapper * volumeMapper = vtkVolumeRayCastMapper: : New (); volumeMapper->SetVolumeRayCastFunction(compositeFunction); volumeMapper->SetInputConnection(readerImageCast->GetOutputPort()); volumeMapper->SetNumberOfThreads(3); 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->Initialize(); iren->Start(); Ren ->ResetCameraClippingRange(); VolumeMapper ->Delete(); readerImageCast->Delete(); iren->Delete(); ren->Delete(); renWin->Delete(); opacityTransferFunction->Delete(); volumeProperty->Delete(); compositeFunction->Delete(); volume->Delete(); colorTransferFunction->Delete(); } `Copy the code

Results obtained:

VTK body rendering isn’t that good anyway…