VTK 医学图像处理---放大镜/区域放大功能
本博文主要内容为:实现放大镜的源代码;实现思路;具体代码说明。
目录
VTK 医学图像处理---放大镜/区域放大功能
简介:
1 放大镜源代码
1 wxInteractorStyleImage 类源代码
2 wxMagnifierAcotor类源代码
3 Magnifier.cpp 源代码
2 放大镜实现思路
总结:
简介:
放大镜(局部放大)在医学图像处理软件中是一个常用的功能,本博文基于VTK实现放大镜功能,该功能主要涉及到交互和放大镜功能实现,具体实现过程中(源代码中),wxInteractorStyleImage类主要实现左键按下、移动到抬起这个过程中局部图像放大的交互;wxMagnifierAcotor类实现局部图像方法功能的实现和显示。
1 放大镜源代码
Main函数入口和主体在 <021_Magnifier.cpp>文件中,wxInteractorStyleImage类主要实现鼠标的交互功能,wxMagnifierAcotor类实现局部图像方法功能的实现和显示。项目工程组织如下图:
在自己的项目中添加 wxInteractorStyleImage 和 wxMagnifierAcotor 两个类的源码(可以自己新建类,把下面代码拷贝进来,然后修改下文件名称即可),然后将Magnifier.cpp文件中的内容拷贝到自己项目main函数所在文件,就可以直接运行。
1 wxInteractorStyleImage 类源代码
头文件如下:
#ifndef wxInteractorStyleImage_h
#define wxInteractorStyleImage_h#include "vtkInteractorStyleTrackballCamera.h"// Motion flags#define VTKIS_WINDOW_LEVEL 1024
#define VTKIS_SLICE 1025
#define VTKIS_MAGNIFIER 1026 /* 添加放大镜事件 ID */// Style flags#define VTKIS_IMAGE2D 2
#define VTKIS_IMAGE3D 3
#define VTKIS_IMAGE_SLICING 4class vtkImageProperty;
class wxMagnifierAcotor;class wxInteractorStyleImage : public vtkInteractorStyleTrackballCamera
{
public:static wxInteractorStyleImage *New();vtkTypeMacro(wxInteractorStyleImage, vtkInteractorStyleTrackballCamera);void PrintSelf(ostream& os, vtkIndent indent) override;virtual void SetMagnifier(wxMagnifierAcotor* pActor);//@{/*** Some useful information for handling window level*/vtkGetVector2Macro(WindowLevelStartPosition, int);vtkGetVector2Macro(WindowLevelCurrentPosition, int);//@}//@{/*** Event bindings controlling the effects of pressing mouse buttons* or moving the mouse.*/void OnMouseMove() override;void OnLeftButtonDown() override;void OnLeftButtonUp() override;void OnMiddleButtonDown() override;void OnMiddleButtonUp() override;void OnRightButtonDown() override;void OnRightButtonUp() override;//@}/*** Override the "fly-to" (f keypress) for images.*/void OnChar() override;// These methods for the different interactions in different modes// are overridden in subclasses to perform the correct motion. Since// they might be called from OnTimer, they do not have mouse coord parameters// (use interactor's GetEventPosition and GetLastEventPosition)virtual void WindowLevel();virtual void Pick();virtual void Slice();// Interaction mode entry points used internally.virtual void StartWindowLevel();virtual void EndWindowLevel();virtual void StartPick();virtual void EndPick();virtual void StartSlice();virtual void EndSlice();virtual void StartMagnifier();virtual void EndMagnifier();virtual void Magnifier();//@{/*** Set/Get current mode to 2D or 3D. The default is 2D. In 3D mode,* it is possible to rotate the camera to view oblique slices. In Slicing* mode, it is possible to slice through the data, but not to generate oblique* views by rotating the camera.*/vtkSetClampMacro(InteractionMode, int, VTKIS_IMAGE2D, VTKIS_IMAGE_SLICING);vtkGetMacro(InteractionMode, int);void SetInteractionModeToImage2D() {this->SetInteractionMode(VTKIS_IMAGE2D);}void SetInteractionModeToImage3D() {this->SetInteractionMode(VTKIS_IMAGE3D);}void SetInteractionModeToImageSlicing() {this->SetInteractionMode(VTKIS_IMAGE_SLICING);}//@}//@{/*** Set the orientations that will be used when the X, Y, or Z* keys are pressed. See SetImageOrientation for more information.*/vtkSetVector3Macro(XViewRightVector, double);vtkGetVector3Macro(XViewRightVector, double);vtkSetVector3Macro(XViewUpVector, double);vtkGetVector3Macro(XViewUpVector, double);vtkSetVector3Macro(YViewRightVector, double);vtkGetVector3Macro(YViewRightVector, double);vtkSetVector3Macro(YViewUpVector, double);vtkGetVector3Macro(YViewUpVector, double);vtkSetVector3Macro(ZViewRightVector, double);vtkGetVector3Macro(ZViewRightVector, double);vtkSetVector3Macro(ZViewUpVector, double);vtkGetVector3Macro(ZViewUpVector, double);//@}/*** Set the view orientation, in terms of the horizontal and* vertical directions of the computer screen. The first* vector gives the direction that will correspond to moving* horizontally left-to-right across the screen, and the* second vector gives the direction that will correspond to* moving bottom-to-top up the screen. This method changes* the position of the camera to provide the desired view.*/void SetImageOrientation(const double leftToRight[3],const double bottomToTop[3]);/*** Set the image to use for WindowLevel interaction.* Any images for which the Pickable flag is off are ignored.* Images are counted back-to-front, so 0 is the rearmost image.* Negative values can be used to count front-to-back, so -1 is* the frontmost image, -2 is the image behind that one, etc.* The default is to use the frontmost image for interaction.* If the specified image does not exist, then no WindowLevel* interaction will take place.*/virtual void SetCurrentImageNumber(int i);int GetCurrentImageNumber() { return this->CurrentImageNumber; }/*** Get the current image property, which is set when StartWindowLevel* is called immediately before StartWindowLevelEvent is generated.* This is the image property of the topmost vtkImageSlice in the* renderer or nullptr if no image actors are present.*/vtkImageProperty *GetCurrentImageProperty() {return this->CurrentImageProperty;}protected:wxInteractorStyleImage();~wxInteractorStyleImage() override;wxMagnifierAcotor* MagnifierAcotor;int WindowLevelStartPosition[2];int WindowLevelCurrentPosition[2];double WindowLevelInitial[2];vtkImageProperty *CurrentImageProperty;int CurrentImageNumber;int InteractionMode;double XViewRightVector[3];double XViewUpVector[3];double YViewRightVector[3];double YViewUpVector[3];double ZViewRightVector[3];double ZViewUpVector[3];private:wxInteractorStyleImage(const wxInteractorStyleImage&) = delete;void operator=(const wxInteractorStyleImage&) = delete;
};#endif
wxInteractorStyleImage类源代码 具体实现.cpp文件源码如下:
#include "wxInteractorStyleImage.h"#include "vtkAbstractPropPicker.h"
#include "vtkAssemblyPath.h"
#include "vtkPropCollection.h"#include "vtkCallbackCommand.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageSlice.h"
#include "vtkImageMapper3D.h"
#include "vtkImageProperty.h"
#include "wxMagnifierAcotor.h"vtkStandardNewMacro(wxInteractorStyleImage);//----------------------------------------------------------------------------
wxInteractorStyleImage::wxInteractorStyleImage()
{this->WindowLevelStartPosition[0] = 0;this->WindowLevelStartPosition[1] = 0;this->WindowLevelCurrentPosition[0] = 0;this->WindowLevelCurrentPosition[1] = 0;this->WindowLevelInitial[0] = 1.0; // Windowthis->WindowLevelInitial[1] = 0.5; // Levelthis->CurrentImageProperty = nullptr;this->CurrentImageNumber = -1;this->MagnifierAcotor = nullptr;this->InteractionMode = VTKIS_IMAGE2D;this->XViewRightVector[0] = 0;this->XViewRightVector[1] = 1;this->XViewRightVector[2] = 0;this->XViewUpVector[0] = 0;this->XViewUpVector[1] = 0;this->XViewUpVector[2] = -1;this->YViewRightVector[0] = 1;this->YViewRightVector[1] = 0;this->YViewRightVector[2] = 0;this->YViewUpVector[0] = 0;this->YViewUpVector[1] = 0;this->YViewUpVector[2] = -1;this->ZViewRig