1. 程式人生 > >VTK讀取DICOM醫學圖片進行體繪製的幾種方法

VTK讀取DICOM醫學圖片進行體繪製的幾種方法

注意的是:VTK中不同的vtkVolumeMapper支援不同的資料型別。比如vtkVolumeRayCastMapper和vtkVolumeTextureMapper2D只能支援單分組VTK_UNSIGNED_CHAR和VTK_UNSIGNED_SHORT型別資料,因此當讀入其他型別的影象資料時,需要對資料進行轉換,比如採用vtkImageCast或者vtkImageScale:而vtkVolumeTextMapper3D則支援任意資料型別,但是必須是單分組資料或者多元獨立資料。vtkFixedPointVolumeRayCastMapper靈活性最高,可以支援所有型別資料,最高四元資料。

使用vtkFixedPointVolumeRayCastMapper類

#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" #include "vtkAutoInit.h" #include "vtkFixedPointVolumeRayCastMapper.h" VTK_MODULE_INIT(vtkRenderingOpenGL); VTK_MODULE_INIT(vtkInteractionStyle); VTK_MODULE_INIT(vtkRenderingFreeType); VTK_MODULE_INIT(vtkRenderingVolumeOpenGL); int main() { std::string
str = "F:\\CT3"; vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New(); reader->SetDirectoryName(str.c_str()); reader->Update(); vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkSmartPointer<vtkPiecewiseFunction>::New(); opacityFun->AddPoint(120,0.0); opacityFun->AddPoint(250,1.0); opacityFun->AddPoint(520,1.0); opacityFun->AddPoint(650,0.0); vtkSmartPointer<vtkColorTransferFunction> TransferFun = vtkSmartPointer<vtkColorTransferFunction>::New(); TransferFun->AddRGBPoint(120, 255 / 255.0, 98 / 255.0, 98 / 255.0); TransferFun->AddRGBPoint(250, 255 / 255.0, 255 / 255.0, 180 / 255.0); TransferFun->AddRGBPoint(520, 1.0, 1.0, 1.0); TransferFun->AddRGBPoint(650, 1.0, 1.0, 1.0); vtkSmartPointer<vtkPiecewiseFunction> grideFun = vtkSmartPointer<vtkPiecewiseFunction>::New(); grideFun->AddPoint(120, 2.0); grideFun->AddPoint(250, 2.0); grideFun->AddPoint(520, 0.1); grideFun->AddPoint(650, 0.1); vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New(); volumeProperty->SetColor(TransferFun); volumeProperty->SetScalarOpacity(opacityFun); volumeProperty->SetGradientOpacity(grideFun); volumeProperty->ShadeOn(); volumeProperty->SetAmbient(0.2); volumeProperty->SetDiffuse(0.9); volumeProperty->SetSpecular(0.2); volumeProperty->SetSpecularPower(10); vtkSmartPointer<vtkVolumeRayCastCompositeFunction> rayCastFun = vtkSmartPointer<vtkVolumeRayCastCompositeFunction>::New(); vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper1 = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New(); mapper1->SetInputConnection(reader->GetOutputPort()); mapper1->SetAutoAdjustSampleDistances(0); vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New(); volume->SetMapper(mapper1); volume->SetProperty(volumeProperty); vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New(); ren->AddVolume(volume); ren->SetBackground(1.0,1.0,1.0); vtkSmartPointer<vtkRenderWindow> renwin = vtkSmartPointer<vtkRenderWindow>::New(); renwin->AddRenderer(ren); renwin->SetSize(740, 480); vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); iren->SetRenderWindow(renwin); iren->Initialize(); renwin->Render(); iren->Start(); return 0; }

使用tkVolumeRayCastCompositeFunction類

#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" 
#include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);

int main()
{
    std::string str = "F:\\CT3";
    vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
    reader->SetDirectoryName(str.c_str());
    reader->SetDataByteOrderToLittleEndian();// 小端位元組序
    reader->Update();

    vtkSmartPointer<vtkImageCast> imageCast = vtkSmartPointer<vtkImageCast>::New();
    imageCast->SetInputConnection(reader->GetOutputPort());
    imageCast->SetOutputScalarTypeToUnsignedShort();
    imageCast->Update();

    vtkSmartPointer<vtkPiecewiseFunction> opacityFun = vtkSmartPointer<vtkPiecewiseFunction>::New();
    opacityFun->AddPoint(120,0.0);
    opacityFun->AddPoint(250,1.0);
    opacityFun->AddPoint(520,1.0);
    opacityFun->AddPoint(650,0.0);


    vtkSmartPointer<vtkColorTransferFunction> TransferFun = vtkSmartPointer<vtkColorTransferFunction>::New();
    TransferFun->AddRGBPoint(120, 255 / 255.0, 98 / 255.0, 98 / 255.0);
    TransferFun->AddRGBPoint(250, 255 / 255.0, 255 / 255.0, 180 / 255.0);
    TransferFun->AddRGBPoint(520, 1.0, 1.0, 1.0);
    TransferFun->AddRGBPoint(650, 1.0, 1.0, 1.0);

    vtkSmartPointer<vtkPiecewiseFunction> grideFun = vtkSmartPointer<vtkPiecewiseFunction>::New();
    grideFun->AddPoint(120, 2.0);
    grideFun->AddPoint(250, 2.0);
    grideFun->AddPoint(520, 0.1);
    grideFun->AddPoint(650, 0.1);

    vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
    volumeProperty->SetColor(TransferFun);
    volumeProperty->SetScalarOpacity(opacityFun);
    volumeProperty->SetGradientOpacity(grideFun);
    volumeProperty->ShadeOn();

    volumeProperty->SetAmbient(0.2);
    volumeProperty->SetDiffuse(0.9);
    volumeProperty->SetSpecular(0.2);
    volumeProperty->SetSpecularPower(10);

    vtkSmartPointer<vtkVolumeRayCastCompositeFunction> rayCastFun = vtkSmartPointer<vtkVolumeRayCastCompositeFunction>::New();

    vtkSmartPointer<vtkVolumeRayCastMapper> mapper1 = vtkSmartPointer<vtkVolumeRayCastMapper>::New();
    mapper1->SetVolumeRayCastFunction(rayCastFun);
    mapper1->SetInputConnection(imageCast->GetOutputPort());
    mapper1->SetAutoAdjustSampleDistances(0);





    vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
    volume->SetMapper(mapper1);
    volume->SetProperty(volumeProperty);
    vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
    ren->AddVolume(volume);
    ren->SetBackground(1.0,1.0,1.0);


    vtkSmartPointer<vtkRenderWindow> renwin = vtkSmartPointer<vtkRenderWindow>::New();
    renwin->AddRenderer(ren);
    renwin->SetSize(740, 480);

    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renwin);

    iren->Initialize();
    renwin->Render();
    iren->Start();
    return 0;
}

使用vtkPiecewiseFunction 設定不透明度和梯度不透明vtkTransferFunctiony設定顏色傳輸。

這裡寫圖片描述

這裡寫圖片描述