玩了半天,终于可以实现对于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;
}