原程序基于《VTK图形图像开发进阶》第五章,用于鼠标实时进行三维图像的切面提取。由于我使用的版本是VS2022+VTK9,所以我对提供的程序进行了一定的修改,加入了Update()函数,代码为:
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //建立“观察者/命令”模式监听鼠标消息 完成交互
#include
#include
#include
class vtkImageInteractionCallback : public vtkCommand
{
public:
static vtkImageInteractionCallback* New() //回调函数初始化函数
{
return new vtkImageInteractionCallback;
}
vtkImageInteractionCallback()
{
this->Slicing = 0;
this->ImageReslice = 0;
this->Interactor = 0;
}
void SetImageReslice(vtkImageReslice* reslice)
{
this->ImageReslice = reslice;
}
vtkImageReslice* GetImageReslice()
{
return this->ImageReslice;
}
void SetInteractor(vtkRenderWindowInteractor* interactor)
{
this->Interactor = interactor;
}
vtkRenderWindowInteractor* GetInteractor()
{
return this->Interactor;
}
virtual void Execute(vtkObject*, unsigned long event, void*)
{
vtkRenderWindowInteractor* interactor = GetInteractor();
int lastPos[2];
interactor->GetLastEventPosition(lastPos);
int currPos[2];
interactor->GetEventPosition(currPos);
if (event == vtkCommand::LeftButtonPressEvent)
{
this->Slicing = 1; //标志位
}
else if (event == vtkCommand::LeftButtonReleaseEvent)
{
this->Slicing = 0; //标志位
}
else if (event == vtkCommand::MouseMoveEvent)
{
if (this->Slicing)//检验鼠标左键已经按下 正在执行操作
{
vtkImageReslice* reslice = this->ImageReslice;
//记下鼠标Y向变化的幅值大小
int deltaY = lastPos[1] - currPos[1];
reslice->Update();
double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
vtkMatrix4x4* matrix = reslice->GetResliceAxes();
//重新定位切片需要经过的中心点
double point[4];
double center[4];
point[0] = 0;
point[1] = 0;
point[2] = sliceSpacing * deltaY;
point[3] = 1.0;
matrix->MultiplyPoint(point, center);
matrix->SetElement(0, 3, center[0]);
matrix->SetElement(1, 3, center[1]);
matrix->SetElement(2, 3, center[2]);
interactor->Render();
}
else
{
vtkInteractorStyle* style = vtkInteractorStyle::SafeDownCast(
interactor->GetInteractorStyle());
if (style)
{
style->OnMouseMove();
}
}
}
}
private:
int Slicing;
vtkImageReslice* ImageReslice;
vtkRenderWindowInteractor* Interactor;
};
//**********************************************************************************//
int main()
{
vtkSmartPointer reader =
vtkSmartPointer::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 resliceAxes =
vtkSmartPointer::New();
resliceAxes->DeepCopy(axialElements);
resliceAxes->SetElement(0, 3, center[0]);
resliceAxes->SetElement(1, 3, center[1]);
resliceAxes->SetElement(2, 3, center[2]);
vtkSmartPointer reslice =
vtkSmartPointer::New();
reslice->SetInputConnection(reader->GetOutputPort());
reslice->SetOutputDimensionality(2);
reslice->SetResliceAxes(resliceAxes);
reslice->SetInterpolationModeToLinear();
reslice->Update();
vtkSmartPointer colorTable =
vtkSmartPointer::New();
colorTable->SetRange(0, 1000);
colorTable->SetValueRange(0.0, 1.0);
colorTable->SetSaturationRange(0.0, 0.0);
colorTable->SetRampToLinear();
colorTable->Build();
vtkSmartPointer colorMap =
vtkSmartPointer::New();
colorMap->SetLookupTable(colorTable);
colorMap->SetInputConnection(reslice->GetOutputPort());
colorMap->Update();
vtkSmartPointer imgActor =
vtkSmartPointer::New();
imgActor->SetInputData(colorMap->GetOutput());
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(imgActor);
renderer->SetBackground(.4, .5, .6);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->SetSize(500, 500);
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
vtkSmartPointer imagestyle =
vtkSmartPointer::New();
renderWindowInteractor->SetInteractorStyle(imagestyle);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
//****************建立 观察者-命令 模式****************//
vtkSmartPointer callback =
vtkSmartPointer::New();
callback->SetImageReslice(reslice);
callback->SetInteractor(renderWindowInteractor);
imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);
renderWindowInteractor->Start();
return 0;
}
运行结果如下,只能显示一张切面图,无法使用鼠标进行交互。
出现这种情况的原因是:新版vtk需要在vtkImageInteractionCallback类的定义里增加vtkImageMapToColors的更新的相关函数才可以正常切换切片。
所以我们需要在原来的代码里增加以下部分:
void SetImageResliceColorMap(vtkImageMapToColors* colorMap)
{
this->colorMap = colorMap;
}
vtkImageMapToColors* GetImageResliceColorMap()
{
return this->colorMap;
callback->SetImageReslice(reslice);
callback->SetInteractor(renderWindowInteractor);
callback->SetImageResliceColorMap(colorMap);
这样就可以实现鼠标滑动改变切面了。
三维图像切片交互提取(回调函数、观察者-命令模式)
如果需要这段代码更加详细的解析,可以参考我的另一篇博文:6.VTK二维三维图像显示以及二维三维图像切面交互提取(回调函数、观察者-命令模式)_vtk怎么提取切面-CSDN博客另外如果想给这段代码用Qt界面展示出来,我的这篇博文可以用来参考:VS2022联合Qt5开发学习5(QT5.12.3联合VTK在VS2022上开发医学图像项目)-CSDN博客