日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

VTK修炼之道26:图像基本操作_三维图像切片提取

發(fā)布時(shí)間:2025/3/15 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VTK修炼之道26:图像基本操作_三维图像切片提取 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 coronalElements[16] = {1, 0, 0, 0,0, 0, 1, 0,0,-1, 0, 0,0, 0, 0, 1 }; 提取平行于YZ平面的切片:
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)該是:


然而,在實(shí)際運(yùn)行中,卻碰到了vtkMetaImageReader不能讀取文件的問(wèn)題,在之前32bit系統(tǒng)上,這都是正常的,暫時(shí)還不能肯定問(wèn)題出現(xiàn)在哪里,需要進(jìn)一步研究!!問(wèn)題如下:

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。