VTK修炼之道26:图像基本操作_三维图像切片提取
1.三維圖像切片提取
切片是指三維圖像中的一個(gè)切面對(duì)應(yīng)的圖像。切面可以是過(guò)圖像內(nèi)部一點(diǎn)且平行于XY、YZ、XZ平面的平面,也可以是任意的過(guò)三維圖像內(nèi)部一點(diǎn)任意方向的平面。通過(guò)提取切片可以方便的瀏覽和分析圖像內(nèi)部組織結(jié)構(gòu),是醫(yī)學(xué)圖像瀏覽軟件中的一個(gè)重要的功能。在VTK中vtkImageReslice類實(shí)現(xiàn)圖像切片提取功能。
下面是切片提取的代碼:
#include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL);#include <vtkSmartPointer.h> #include <vtkImageData.h> #include <vtkMetaImageReader.h> #include <vtkMatrix4x4.h> // #include <vtkImageReslice.h> #include <vtkLookupTable.h> #include <vtkImageMapToColors.h> #include <vtkImageActor.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleImage.h>int main(int argc, char* argv[]) {vtkSmartPointer<vtkMetaImageReader> reader =vtkSmartPointer<vtkMetaImageReader>::New();reader->SetFileName("brain.mhd");reader->Update();int extent[6];double spacing[3];double origin[3];reader->GetOutput()->GetExtent(extent);reader->GetOutput()->GetSpacing(spacing);reader->GetOutput()->GetOrigin(origin);double center[3];center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);//*****************************************************************//static double axialElements[16] = {1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,0, 0, 0, 1};vtkSmartPointer<vtkMatrix4x4> resliceAxes =vtkSmartPointer<vtkMatrix4x4>::New();resliceAxes->DeepCopy(axialElements);resliceAxes->SetElement(0, 3, center[0]);resliceAxes->SetElement(1, 3, center[1]);resliceAxes->SetElement(2, 3, center[2]);vtkSmartPointer<vtkImageReslice> reslice =vtkSmartPointer<vtkImageReslice>::New();reslice->SetInputConnection(reader->GetOutputPort());reslice->SetOutputDimensionality(2);reslice->SetResliceAxes(resliceAxes);reslice->SetInterpolationModeToLinear();//*****************************************************************//vtkSmartPointer<vtkLookupTable> colorTable =vtkSmartPointer<vtkLookupTable>::New();colorTable->SetRange(0, 1000);colorTable->SetValueRange(0.0, 1.0);colorTable->SetSaturationRange(0.0, 0.0);colorTable->SetRampToLinear();colorTable->Build();vtkSmartPointer<vtkImageMapToColors> colorMap =vtkSmartPointer<vtkImageMapToColors>::New();colorMap->SetLookupTable(colorTable);colorMap->SetInputConnection(reslice->GetOutputPort());//*****************************************************************//vtkSmartPointer<vtkImageActor> imgActor =vtkSmartPointer<vtkImageActor>::New();imgActor->SetInputData(colorMap->GetOutput());vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(imgActor);renderer->SetBackground(1.0, 1.0, 1.0);vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);renderWindow->Render();renderWindow->SetSize(640, 480);renderWindow->SetWindowName("Extract3Dslice");vtkSmartPointer<vtkRenderWindowInteractor> rwi =vtkSmartPointer<vtkRenderWindowInteractor>::New();vtkSmartPointer<vtkInteractorStyleImage> imagestyle =vtkSmartPointer<vtkInteractorStyleImage>::New();rwi->SetInteractorStyle(imagestyle);rwi->SetRenderWindow(renderWindow);rwi->Initialize();rwi->Start();return 0; }
首先通過(guò)vtkMetaImageReader讀取一張醫(yī)學(xué)三維圖像,并獲取得到圖像范圍(extent),原點(diǎn)和像素間隔;由這三個(gè)參數(shù)可以計(jì)算圖像的中心位置center;接下來(lái)定義了切面的變換矩陣axialElements,該矩陣的前三列分別表示x、y和z方向向量,第四列為中心點(diǎn)坐標(biāo);
代碼中的axialElements表示切面變換矩陣與當(dāng)前坐標(biāo)系一致,且切面為過(guò)中心點(diǎn)center,并平行于XY平面的平面???當(dāng)前,定義該切面時(shí),也可以是其他平面,甚至是任意平面,但是必須要過(guò)圖像內(nèi)部點(diǎn)。
下面給出了一個(gè)常用的變換矩陣。
提取平行于XZ平面的切片:
static double sagittalElements[16] = { 0, 0,-1, 0, 1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 1 }; 提取斜切切片:static double obliqueElements[16] = { 1, 0, 0, 0, 0, 0.866025, -0.5, 0, 0, 0.5, 0.866025, 0, 0, 0, 0, 1 }; 注意使用這些變換矩陣的時(shí)候,需要將第四列替換為切片經(jīng)過(guò)圖像的一個(gè)點(diǎn)坐標(biāo),上例中將圖像的中心添加到axialElements矩陣,并通過(guò)函數(shù)SetResliceAxes設(shè)置變換矩陣,SetOutputDimensionality(2)指定輸出的圖像為一個(gè)二維圖像;而函數(shù)SetInterpolationModeToLinear()則指定了切面提取中的差值方式為線性差值,另外該類中還提供了其他的插值方式:
SetInterpolationModeToNearestNeighbor():最近鄰方式
SetInterpolationModeToCubic():三次線性差值
設(shè)置完畢后,執(zhí)行Update()即可完成切面計(jì)算。
東靈提供的預(yù)想結(jié)果應(yīng)該是:
2.參看資料
1.《C++ primer》
2.《The VTK User’s Guide – 11thEdition》
3.《The Visualization Toolkit – AnObject-Oriented Approach To 3D Graphics (4th Edition)》
4. ?張曉東, 羅火靈. VTK圖形圖像開(kāi)發(fā)進(jìn)階[M]. 機(jī)械工業(yè)出版社, 2015.
總結(jié)
以上是生活随笔為你收集整理的VTK修炼之道26:图像基本操作_三维图像切片提取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS选择器的权重详解
- 下一篇: VTK修炼之道27:图像基本操作_三维图