赞
踩
VTK中鼠标消息是在交互类型对象(interactorstyle)中响应,因此通过为交互类型对象(interactorstyle)添加观察者(observer)来监听相应的消息,当消息触发时,由命令模式执行相应的回调函数。
vtkImageInteractionCallback继承自vtkCommand类,并覆盖父类函数Execute()。
该类提供了两个接口:SetImageReslice和SetInteractor。
SetImageReslice用以设置vtkImageSlice对象,vtkImageSlice根据设置的变换矩阵提取三维图像切片。SetInteractor用以设置vtkRenderWindowInteractor,vtkRenderWindowInteractor类对象负责每次提取切片后刷新视图。
- class vtkImageInteractionCallback : public vtkCommand
- {
- public:
- static vtkImageInteractionCallback *New() //回调函数初始化函数
- {
- return new vtkImageInteractionCallback;
- }
- vtkImageInteractionCallback()
- {
- this->Slicing = 0;//切片提取标志位,为1时提取切片。用来检验鼠标左键是否已经按下
- this->ImageReslice = 0;//vtkImageSlice对象的变换矩阵,用来确定切面在三维图像中的位置
- 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 *)
- {
- //具体内容放下面
- }
- private:
- int Slicing;
- vtkImageReslice *ImageReslice;
- vtkRenderWindowInteractor *Interactor;
- };

需要设置四个参数,窗位(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()即可不断的根据鼠标移动刷新图像。
- 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; //标志位。当鼠标左键按下时,就设置切片提取标志为1
- }
- else if (event == vtkCommand::LeftButtonReleaseEvent)/如果当前事件是鼠标右键按下
- {
- this->Slicing = 0; //标志位
- }
- else if (event == vtkCommand::MouseMoveEvent)/如果当前事件是鼠标移动
- {
- if (this->Slicing)//检验鼠标左键已经按下
- {
- vtkImageReslice *reslice = this->ImageReslice;//设置当前切片图像位置的矩阵为this->ImageReslice;但在更新之前,不会将这个矩阵数据填充到reslice中去
- //记下鼠标Y向变化的幅值大小
- int deltaY = lastPos[1] - currPos[1];//计算鼠标滑动的前后两次点的位置lastPos和currPos。然后计算两点的Y坐标的差deltaY
-
- reslice->Update();//使用更新操作,为reslice获取当前切片图像位置的矩阵
- double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];获取reslice矩阵中的参数
- vtkMatrix4x4 *matrix = reslice->GetResliceAxes();//定义需要使用的新的中心店center转换至vtkImageSlice当前变换矩阵中的转换矩阵,但这里没有更新,就没有填充
-
- //重新定位切片需要经过的中心点
- 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();
- }
- }
- }
- }

Command对象定义完毕后,即可为交互对象InteractorStyle添加观察者,响应鼠标消息。
这里主要是定义了vtkImageInteractionCallback对象,并设置vtkImageSlice对象和vtkRenderWindowInteractor对象。然后为交互对象vtkInteractorStyle添加观察者来监控相应的消息。
- vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();//定义交互对象
-
- vtkSmartPointer<vtkInteractorStyleImage> imagestyle =
- vtkSmartPointer<vtkInteractorStyleImage>::New();//定义交互模式
-
- renderWindowInteractor->SetInteractorStyle(imagestyle);//设置交互模式
- renderWindowInteractor->SetRenderWindow(renderWindow);//连接交互器与显示窗口
- renderWindowInteractor->Initialize();//交互器初始化
-
- //****************建立 观察者-命令 模式****************//
- vtkSmartPointer<vtkImageInteractionCallback> callback =
- vtkSmartPointer<vtkImageInteractionCallback>::New();//设置回调函数
- callback->SetImageReslice(reslice);//为vtkImageSlice对象设置变换矩阵,确定矩阵对应的切片位置
- callback->SetInteractor(renderWindowInteractor);
-
- imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);//鼠标移动对应的回调函数
- imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);//按下鼠标左键对应的回调函数
- imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);//按下鼠标右键对应的回调函数
-
- renderWindowInteractor->Start();
-

这里主要是三个消息:
vtkCommand::LeftButtonPressEvent,
vtkCommand::LeftButtonReleaseEvent,
vtkCommand::MouseMoveEvent,
当响应到这三个消息时,立即执行vtkImageInteractionCallback的Execute函数,以便实现切片的实时提取和更新。完成以后,运行程序,当鼠标在图像上移动时,会发现图像会跟着鼠标的移动而变化。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。