当前位置:   article > 正文

VTK使用交互器来从三维体数据中提取二维切片_vtk getresliceaxes

vtk getresliceaxes

VTK中鼠标消息是在交互类型对象(interactorstyle)中响应,因此通过为交互类型对象(interactorstyle)添加观察者(observer)来监听相应的消息,当消息触发时,由命令模式执行相应的回调函数

vtkImageInteractionCallback继承自vtkCommand类,并覆盖父类函数Execute()。
该类提供了两个接口:SetImageReslice和SetInteractor。
SetImageReslice用以设置vtkImageSlice对象,vtkImageSlice根据设置的变换矩阵提取三维图像切片。SetInteractor用以设置vtkRenderWindowInteractor,vtkRenderWindowInteractor类对象负责每次提取切片后刷新视图

  1. class vtkImageInteractionCallback : public vtkCommand
  2. {
  3. public:
  4. static vtkImageInteractionCallback *New() //回调函数初始化函数
  5. {
  6. return new vtkImageInteractionCallback;
  7. }
  8. vtkImageInteractionCallback()
  9. {
  10. this->Slicing = 0;//切片提取标志位,为1时提取切片。用来检验鼠标左键是否已经按下
  11. this->ImageReslice = 0;//vtkImageSlice对象的变换矩阵,用来确定切面在三维图像中的位置
  12. this->Interactor = 0;
  13. }
  14. void SetImageReslice(vtkImageReslice *reslice)
  15. {
  16. this->ImageReslice = reslice;
  17. }
  18. vtkImageReslice *GetImageReslice()
  19. {
  20. return this->ImageReslice;
  21. }
  22. void SetInteractor(vtkRenderWindowInteractor *interactor)
  23. {
  24. this->Interactor = interactor;
  25. }
  26. vtkRenderWindowInteractor *GetInteractor()
  27. {
  28. return this->Interactor;
  29. }
  30. virtual void Execute(vtkObject * ,unsigned long event,void *)
  31. {
  32. //具体内容放下面
  33. }
  34. private:
  35. int Slicing;
  36. vtkImageReslice *ImageReslice;
  37. vtkRenderWindowInteractor *Interactor;
  38. };

   需要设置四个参数,窗位(ColorLevel)、窗宽(ColorWindow)、切片(Slice)和切片方向(Orientation)。按下鼠标左键拖动鼠标,可以调节图像的窗宽窗位,从而显示不同灰度范围内容;按下鼠标右键拖动鼠标可以放缩图像。当然这些交互操作可以由用户根据需要自己定义vtkInteractorStyle子类,并响应相应的操作。

( 窗宽是图像显示的灰度范围。一般显示器的灰度范围为256级,医学图像的灰度范围远远大于这个范围,所以显示器不足以显示所有的灰度级,需要使用窗宽来定义想要显示的灰度范围;
  一般情况下,可以将灰度值高于窗宽范围的最大值时,将像素值显示为白色值(255);将灰度值低于窗宽范围的最小值时,将像素值显示为黑色值(0);增大窗宽范围,可以显示更多不同灰度组织结构,但会减低组织之间的对比度;减小窗宽范围,可以减少所显示不同灰度组织结构,也就增大了组织结构之间的对比度;
  窗位是窗宽的中心位置;窗宽只是确定了像素上的可视化的范围,是一个具体值,单独依靠窗宽不能确定这个范围的上限是多少和下限是多少,需要配合使用窗位来确定;比如上一代码中:窗宽为2000,只能说明窗宽的范围是2000,不能说明是从0到2000,还是-1000到1000,设置窗位为500时,窗宽2000,就能表示可视灰度范围为-500到1500;)

  而显示三维图像时,还需要确定当前显示切片和方向。vtkImageViewer2提供了SetSlice()函数设置切片号,SetSliceOrientationToXY()则将切片的方向设置为垂直XY平面方向。此外还可以设置为垂直YZ或者XZ平面方向,其对应函数分别为SetSliceOrientationToYZ()和SetSliceOrientationToXZ()。默认情况下切片方向为垂直于XY平面即沿着Z轴方向,根据设置的切片号获取Z轴方向的具体切片进行显示。

下面重点看Execute函数,该函数提供了具体的切片提取功能。在该函数里面,主要监听了三个消息:
vtkCommand::LeftButtonPressEvent,
vtkCommand::LeftButtonReleaseEvent,
vtkCommand::MouseMoveEvent,
前两个消息分别是鼠标左键的按下和弹起消息。当鼠标左键按下时,就设置切片提取标志为1,而当弹起时,将标志置为0。这样在鼠标移动时,只有在确定切片提取标志为1时,执行切片提取功能。
vtkCommand::MouseMoveEvent即为鼠标移动消息。当检测到该消息时,首先检查切片提取标志,当为1时提取切片。提取切片时,需要为vtkImageSlice对象设置变换矩阵。这里在函数开始时,首先获取了鼠标滑动的前后两次点的位置lastPos和currPos。然后根据两点的Y坐标差deltaY,计算新的中心点center并变换至vtkImageSlice当前变换矩阵中,得到变换中心点,将其设置到原来的变换矩阵matrix中,并设置到vtkImageSlice中,最后执行interactor->Render()即可不断的根据鼠标移动刷新图像。

  1. virtual void Execute(vtkObject * ,unsigned long event,void *)
  2. {
  3. vtkRenderWindowInteractor *interactor = GetInteractor();
  4. int lastPos[2];
  5. interactor->GetLastEventPosition(lastPos);
  6. int currPos[2];
  7. interactor->GetEventPosition(currPos);
  8. if (event == vtkCommand::LeftButtonPressEvent)//如果当前事件是鼠标左键按下
  9. {
  10. this->Slicing = 1; //标志位。当鼠标左键按下时,就设置切片提取标志为1
  11. }
  12. else if (event == vtkCommand::LeftButtonReleaseEvent)/如果当前事件是鼠标右键按下
  13. {
  14. this->Slicing = 0; //标志位
  15. }
  16. else if (event == vtkCommand::MouseMoveEvent)/如果当前事件是鼠标移动
  17. {
  18. if (this->Slicing)//检验鼠标左键已经按下
  19. {
  20. vtkImageReslice *reslice = this->ImageReslice;//设置当前切片图像位置的矩阵为this->ImageReslice;但在更新之前,不会将这个矩阵数据填充到reslice中去
  21. //记下鼠标Y向变化的幅值大小
  22. int deltaY = lastPos[1] - currPos[1];//计算鼠标滑动的前后两次点的位置lastPos和currPos。然后计算两点的Y坐标的差deltaY
  23. reslice->Update();//使用更新操作,为reslice获取当前切片图像位置的矩阵
  24. double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];获取reslice矩阵中的参数
  25. vtkMatrix4x4 *matrix = reslice->GetResliceAxes();//定义需要使用的新的中心店center转换至vtkImageSlice当前变换矩阵中的转换矩阵,但这里没有更新,就没有填充
  26. //重新定位切片需要经过的中心点
  27. double point[4];
  28. double center[4];
  29. point[0] = 0;
  30. point[1] = 0;
  31. point[2] = sliceSpacing*deltaY;
  32. point[3] = 1.0;
  33. matrix->MultiplyPoint(point, center);//填充矩阵
  34. matrix->SetElement(0, 3, center[0]);//填充矩阵
  35. matrix->SetElement(1, 3, center[1]);//填充矩阵
  36. matrix->SetElement(2, 3, center[2]);//填充矩阵
  37. interactor->Render();
  38. }
  39. else//没有触发上述三个事件
  40. {
  41. vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
  42. interactor->GetInteractorStyle());
  43. if (style)
  44. {
  45. style->OnMouseMove();
  46. }
  47. }
  48. }
  49. }

Command对象定义完毕后,即可为交互对象InteractorStyle添加观察者,响应鼠标消息。
这里主要是定义了vtkImageInteractionCallback对象,并设置vtkImageSlice对象和vtkRenderWindowInteractor对象。然后为交互对象vtkInteractorStyle添加观察者来监控相应的消息。

  1. vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
  2. vtkSmartPointer<vtkRenderWindowInteractor>::New();//定义交互对象
  3. vtkSmartPointer<vtkInteractorStyleImage> imagestyle =
  4. vtkSmartPointer<vtkInteractorStyleImage>::New();//定义交互模式
  5. renderWindowInteractor->SetInteractorStyle(imagestyle);//设置交互模式
  6. renderWindowInteractor->SetRenderWindow(renderWindow);//连接交互器与显示窗口
  7. renderWindowInteractor->Initialize();//交互器初始化
  8. //****************建立 观察者-命令 模式****************//
  9. vtkSmartPointer<vtkImageInteractionCallback> callback =
  10. vtkSmartPointer<vtkImageInteractionCallback>::New();//设置回调函数
  11. callback->SetImageReslice(reslice);//为vtkImageSlice对象设置变换矩阵,确定矩阵对应的切片位置
  12. callback->SetInteractor(renderWindowInteractor);
  13. imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);//鼠标移动对应的回调函数
  14. imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);//按下鼠标左键对应的回调函数
  15. imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);//按下鼠标右键对应的回调函数
  16. renderWindowInteractor->Start();

这里主要是三个消息:
vtkCommand::LeftButtonPressEvent,
vtkCommand::LeftButtonReleaseEvent,
vtkCommand::MouseMoveEvent,
当响应到这三个消息时,立即执行vtkImageInteractionCallback的Execute函数,以便实现切片的实时提取和更新。完成以后,运行程序,当鼠标在图像上移动时,会发现图像会跟着鼠标的移动而变化。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/732780?site
推荐阅读
相关标签
  

闽ICP备14008679号