VTK學習筆記-2-TIFF影象資料的重切片
阿新 • • 發佈:2019-02-03
玩了半天,終於可以實現對於TIFF影象的三維重切片。首先需要注意的是vtkTIFFReader並不是支援所有的TIFF壓縮格式,比如LZ壓縮的就不支援。個人覺得,VTK在DICOM格式上具有更強的操作性,貌似其存在便是針對了DICOM格式而進行的,至於其他格式的資料,有點是後期為了迎合相容性而做出的各種的新增。再者,VTK的資料是有限有限的,除了那些Examples。。。那個使用者手冊是要錢的,79美金。。。。。。
#include "vtkImageReader2.h"
#include "vtkImageReslice.h" #include "vtkWindowLevelLookupTable.h" #include "vtkImageMapToColors.h" #include "vtkImageActor.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkImageData.h" #include<vtkTIFFReader.h> int main () { vtkTIFFReader *reader= vtkTIFFReader::New();//用tiff進行讀取 reader->SetFilePrefix("H:\\Crop_test4_4X4\\");//嘗試更大的資料 reader->SetFilePattern("%saaaa_%05d%_bbb.tif");//這個具體的圖片的命名,就要看自己的實際操作了 reader->SetDataExtent(0, 63, 0, 63, 188, 230); reader->SetDataSpacing(3.2, 3.2, 1.5); reader->SetDataOrigin(0.0, 0.0, 0.0); reader->SetDataScalarTypeToUnsignedShort(); reader->UpdateWholeExtent(); // 更新並獲取影象引數。 reader->GetOutput()->UpdateInformation(); int extent[6]; double spacing[3]; double origin[3]; reader->GetOutput()->GetWholeExtent(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]); //使用下面三個軸重切後,ZSlice將對應原影象橫斷面(預設) /*static double axialX[3]={ 1, 0, 0}; static double axialY[3]={ 0, 1, 0}; static double axialZ[3]={ 0, 0, 1};*/ // ZSlice對應原影象冠狀面(即重切的z軸方向為原y軸方向) // static double coronalX[3]={ 1, 0, 0}; // static double coronalY[3]={ 0, 0,-1}; // static double coronalZ[3]={ 0, 1, 0}; // ZSlice對應原影象矢狀面(即重切的z軸方向為原x軸方向) static double sagittalX[3]={ 0, 1, 0}; static double sagittalY[3]={ 0, 0,-1}; static double sagittalZ[3]={-1, 0, 0}; // ZSlice對應原影象斜面 static double obliqueX[3]={ 1, 0, 0}; static double obliqueY[3]={ 0, 0.866025,0.5}; static double obliqueZ[3]={ 0,-0.5, 0.866025}; // 按指定的方向抽取片。 vtkImageReslice *reslice = vtkImageReslice::New(); reslice->SetInputConnection(reader->GetOutputPort()); reslice->SetOutputDimensionality(2); //reslice->SetResliceAxesDirectionCosines(sagittalX, sagittalY, sagittalZ); //reslice->SetResliceAxesDirectionCosines(axialX, axialY, axialZ); reslice->SetResliceAxesDirectionCosines(obliqueX, obliqueY, obliqueZ); reslice->SetResliceAxesOrigin(center); reslice->SetInterpolationModeToLinear(); vtkWindowLevelLookupTable *table=vtkWindowLevelLookupTable::New(); /*table->SetWindow(2000); table->SetLevel(1000);*/ table->SetWindow(200); table->SetLevel(100); vtkImageMapToColors *color = vtkImageMapToColors::New(); color->SetLookupTable(table); color->SetInputConnection(reslice->GetOutputPort()); vtkImageActor *actor = vtkImageActor::New(); actor->SetInput(color->GetOutput()); vtkRenderer *renderer = vtkRenderer::New(); renderer->AddActor(actor); vtkRenderWindow *window = vtkRenderWindow::New(); window->AddRenderer(renderer); vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New(); window->SetInteractor(interactor); window->Render(); interactor->Start(); return 0; }