因为OpenCV不能使用,只能使用VTK库的图像处理库,暂时尚未找到其余能够全面替代的库;html
CSDN东灵工做室:http://blog.csdn.net/www_doling_net/article/details/8763686git
复制其第一篇:算法
注:之后将陆续分享一些对经典图像、图形算法的介绍,编程
新浪微博: @东灵工做室数据结构
邮箱:www_doling_net@163.comapp
ITK、VTK研究群:209215671ide
系列一 《VTK基础及应用开发教程》函数
基础入门篇:工具
1. 从零开始学习VTKpost
2. VTK编译安装
3. VTK基础概念
1) VTK基础概念(1)
2) VTK基础概念(2)
4. VTK可视化管线
1) VTK可视化管线(1)
2) VTK可视化管线(2)
5. VTK在图像处理中的应用
6. VTK基本数据结构
1) VTK基本数据结构(1)
2) VTK基本数据结构(2)
3) VTK基本数据结构(3)
4) VTK基本数据结构(4)
5) VTK基本数据结构(5)
高阶应用篇:
9. 基于VTK的MFC应用程序开发
咱们已经知道了VTK工程的管理是用CMake的,而Qt自身有qmake工具,若是对于一些小工程而言,单纯的Qt程序用qmake来构建工程,确实很方便,但若是随着工程复杂度的增长以及工程依赖其余的函数库时,使用CMake来管理工程或许是一个明智的选择。并且随着你对CMake语法的了解,你会发现用CMake来管理工程是一件很是棒的事情。
咱们先看看对于单纯的Qt工程,怎么来写CMakeLists.txt脚本文件。
官方对于这个话题给出的解释在这里。咱们引用一下这篇博文的图,而后给出每句CMakeLists.txt脚本的注释,结合这个图以及脚本的注释,相信你应该能明白了。
咱们在上面的基础上添加VTK相关的CMake脚本文件,以下:
以上的脚本除了红色字体标注的跟1.1注释的不太像以外,其余的都同样,再也不解释。
不少非计算机专业的用户在使用VTK进行编程时,常常会碰到相似下图所示的一些错误。
碰到这样的错误之后,可能不少用户就不知道怎么处理了,其实上面的提示信息已经写得很是清楚了,就是缺乏“vtkCommon.dll”文件。可是又有人会说:个人电脑里明明有这个文件存在啊,为何会找不到呢?
通常的解决方法多是:
方法一:将缺乏的dll文件所有拷贝的工程的Debug或者Release目录下(拷贝的时候要注意你编译的VTK是Debug版本的仍是Release版本的,若是拷错的话,又会出现其余不可预知的错误了)。可是这个方法是你每建一个工程,运行工程以前得把缺乏的动态库文件又要拷贝过去,若是你不嫌麻烦的话,能够采用。
方法二:将缺乏的dll文件所有拷贝到Windows系统的目录下,即C:\Windows\system32或者C:\Windows\system目录下,这个方法是你拷贝一次,之后再基于你拷贝的VTK动态库的工程运行的时候问题都解决了。但它一样有一个问题,假如你电脑里的VTK升级成别的版本,从新编译了一份动态库,或者是同时在你电脑里编译了好几个版本的VTK,这个时候就有点凌乱了。
为何这两种方法均可以解决问题?原来动态编译的程序在启动的时候,会搜索程序所在的目录以及系统环境变量PATH所列的目录,若是这些目录有该程序须要的动态库时,就加载它们,若是没有,就提示没法加载相应动态库的错误。
能够在工程的CMakeLists.txt文件里添加一些脚本,把系统的PATH环境变量做一些更改,在工程启动以前加载这些环境变量。也就是(在工程的CMakeLists.txt最后添加):
以上的脚本也不是特别复杂,但提到了两个文件:CreateWindowsBatchScript.cmake以及StartVS.bat.in。这两个文件的内容分别是:
CreateWindowsBatchScript.cmake:
将工程经过CMake的configure->generate之后,便可生成StartVS_debug.bat和StartVS_release.bat两个脚本文件。若是你要编译、运行Debug版本的工程,即双击StartVS_debug.bat文件打开对应的工程,同理,Release版本的也同样。一旦按这种方式打开相应的工程,就不用再担忧相似“没法加载***.dll文件”的错误了。若是你的程序还增长了ITK等函数库,也能够照着上面的脚本做相应的修改。
注意:使用时将CreateWindowsBatchScript.cmake和StartVS.bat.in两个文件与工程的CMakeLists.txt放在同一级目录里。即相似下图的目录结构:
Qt与VTK的整合可使用VTK提供的类QVTKWidget,看这个类名就知道这个类其实就是一个Qt里的Widget (QVTKWidget派生自QWidget),因此能够把它看成普通的Qt里的Widget来使用,甚至能够在Qt Designer里像Qt的其余标准控件同样拖来拖去。
要实现QVTKWidget在Qt Designer里像Qt的其余标准控件同样拖来拖去,须要把编译生成的QVTKWidgetPlugin.dll/QVTKWidgetPlugin.lib(Release版本)复制到Qt的安装目录里的plugins\designer目录下。完了之后,你会在Qt Designer里面看到以下的控件:
接下来,咱们来完成一个小功能,就是读入一幅JPG图像,而后在Qt界面上,用VTK来显示。功能很是简单,程序也很是简单。上代码:
ProjectMainWindow.h:
ProjectMainWindow.cpp:
程序运行结果:
类vtkEventQtSlotConnect能够实现VTK的事件与Qt的槽函数的链接,VTK的事件主要在vtkCommand.h文件里定义,包括鼠标单击、鼠标双击、鼠标移动等等,如:
vtkCommand::ProgressEvent
vtkCommand::ErrorEvent
vtkCommand::WarningEvent
vtkCommand::PickEvent
vtkCommand::StartPickEvent
vtkCommand::EndPickEvent
vtkCommand::CharEvent
vtkCommand::KeyPressEvent
vtkCommand::KeyReleaseEvent
vtkCommand::LeftButtonPressEvent
vtkCommand::LeftButtonReleaseEvent
vtkCommand::MouseMoveEvent
……
具体的代码实现:
源文件:
示例代码及该博文文档下载地址:http://download.csdn.net/detail/www_doling_net/5137375
(3)二维图像处理:05-VTK在图像处理中的应用(4)
感兴趣区域(Volum of Interest)是指图像内部的一个子区域。在VTK中vtkExtractVOI类实现由用户指定的区域范围提取图像的子图像。该Filter的输入和输出都是一个vtkImageData,所以其结果能够直接做为图像保存。
1: vtkSmartPointer<vtkBMPReader> reader =
2: vtkSmartPointer<vtkBMPReader>::New();
3: reader->SetFileName ( "lena.bmp" );
4: reader->Update();
5:
6: int dims[3];
7: reader->GetOutput()->GetDimensions(dims);
8:
9: vtkSmartPointer<vtkExtractVOI> extractVOI =
10: vtkSmartPointer<vtkExtractVOI>::New();
11: extractVOI->SetInputConnection(reader->GetOutputPort());
12: extractVOI->SetVOI(dims[0]/4.,3.*dims[0]/4.,dims[1]/4.,3.*dims[1]/4., 0, 0);
13: extractVOI->Update();
上例代码实现了提取一副图像的子区域。首先读取一个图像,并获取图像的维数。而后定义vtkExtractVOI对象,该对象接收两个输入一个是图像数据,第二个是区域大小。设置区域大小的函数原型:
void SetVOI(int _arg1, int _arg2, int _arg3, int _arg4, int _arg5, int _arg6)
void SetVOI(int _arg[])
其参数是提取的区域各个方向的大小,共6个参数,依次表示x方向最小值,x方向最大值,y方向最小值,y方向最大值,z方向最小值和z方向最大值。上例中因为读取的是二维图像,所以z方向的区域为[0,0],而在x方向范围为[ dims[0]/4 , 3*dims[0]/4 ],y方向范围为[ dims[1]/4 , 3*dims[1]/4 ],即提取图像原图中间1/4图像。执行结果以下:
图5.18 提取感兴趣区域
切片是指三维图像中的一个切面对应的图像。切面能够是过图像内部一点且平行于XY、YZ、XZ平面的平面,也能够是任意的过三维图像内部一点任意方向的平面。经过提取切片能够方便的浏览和分析图像内部组织结构,是医学图像浏览软件中的一个重要的功能。在VTK中vtkImageReslice类实现图像切片提取功能。下面首先看一段切片提取的代码。
1: vtkSmartPointer<vtkMetaImageReader> reader =
2: vtkSmartPointer<vtkMetaImageReader>::New();
3: reader->SetFileName ( " brain.mhd" );
4: reader->Update();
5:
6: int extent[6];
7: double spacing[3];
8: double origin[3];
9:
10: reader->GetOutput()->GetExtent(extent);
11: reader->GetOutput()->GetSpacing(spacing);
12: reader->GetOutput()->GetOrigin(origin);
13:
14: double center[3];
15: center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
16: center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
17: center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
18:
19: static double axialElements[16] = {
20: 1, 0, 0, 0,
21: 0, 1, 0, 0,
22: 0, 0, 1, 0,
23: 0, 0, 0, 1 };
24:
25: vtkSmartPointer<vtkMatrix4x4> resliceAxes =
26: vtkSmartPointer<vtkMatrix4x4>::New();
27: resliceAxes->DeepCopy(axialElements);
28:
29: resliceAxes->SetElement(0, 3, center[0]);
30: resliceAxes->SetElement(1, 3, center[1]);
31: resliceAxes->SetElement(2, 3, center[2]);
32:
33:
34: vtkSmartPointer<vtkImageReslice> reslice =
35: vtkSmartPointer<vtkImageReslice>::New();
36: reslice->SetInputConnection(reader->GetOutputPort());
37: reslice->SetOutputDimensionality(2);
38: reslice->SetResliceAxes(resliceAxes);
39: reslice->SetInterpolationModeToLinear();
40:
41: vtkSmartPointer<vtkLookupTable> colorTable =
42: vtkSmartPointer<vtkLookupTable>::New();
43: colorTable->SetRange(0, 1000);
44: colorTable->SetValueRange(0.0, 1.0);
45: colorTable->SetSaturationRange(0.0, 0.0);
46: colorTable->SetRampToLinear();
47: colorTable->Build();
48:
49: vtkSmartPointer<vtkImageMapToColors> colorMap =
50: vtkSmartPointer<vtkImageMapToColors>::New();
51: colorMap->SetLookupTable(colorTable);
52: colorMap->SetInputConnection(reslice->GetOutputPort());
53:
54: vtkSmartPointer<vtkImageActor> imgActor =
55: vtkSmartPointer<vtkImageActor>::New();
56: imgActor->SetInput(colorMap->GetOutput());
57:
58: vtkSmartPointer<vtkRenderer> renderer =
59: vtkSmartPointer<vtkRenderer>::New();
60: renderer->AddActor(imgActor);
61: renderer->SetBackground(.4, .5, .6);
62:
63: vtkSmartPointer<vtkRenderWindow> renderWindow =
64: vtkSmartPointer<vtkRenderWindow>::New();
65: renderWindow->SetSize(500, 500);
66: renderWindow->AddRenderer(renderer);
67:
68: vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
69: vtkSmartPointer<vtkRenderWindowInteractor>::New();
70: vtkSmartPointer<vtkInteractorStyleImage> imagestyle =
71: vtkSmartPointer<vtkInteractorStyleImage>::New();
72:
73: renderWindowInteractor->SetInteractorStyle(imagestyle);
74: renderWindowInteractor->SetRenderWindow(renderWindow);
75: renderWindowInteractor->Initialize();
76:
77: renderWindowInteractor->Start();
首先经过vtkMetaImageReader读取一副医学三维图像,并获取获得图像范围(extent),原点和像素间隔;由这三个参数能够计算图像的中心位置center;接下来定义了切面的变换矩阵axialElements,该矩阵的前三列分别表示x、y和z方向向量,第四列为中心点坐标;代码中的axialElements表示切面变换矩阵与当前坐标系一致,且切面为过中心点center,并平行于XY平面的平面。当前,定义该切面时,也能够是其余平面,甚至是任意平面,可是必需要过图像内部点。下面给出了一个经常使用的变换矩阵:
static double coronalElements[16] = {
1, 0, 0, 0,
0, 0, 1, 0,
0,-1, 0, 0,
0, 0, 0, 1 }; 提取平行于XZ平面的切片
static double sagittalElements[16] = {
0, 0,-1, 0,
1, 0, 0, 0,
0,-1, 0, 0,
0, 0, 0, 1 }; 提取平行于YZ平面的切片
static double obliqueElements[16] = {
1, 0, 0, 0,
0, 0.866025, -0.5, 0,
0, 0.5, 0.866025, 0,
0, 0, 0, 1 }; 提取斜切切片
注意使用这些变换矩阵的时候,须要将第四列替换为切片通过图像的一个点坐标,上例中将图像的中心添加到axialElements矩阵,并经过函数SetResliceAxes设置变换矩阵,SetOutputDimensionality(2)指定输出的图像为一个二维图像;而函数SetInterpolationModeToLinear()则指定了切面提取中的差值方式为线性差值,另外该类中还提供了其余的差值方式:
SetInterpolationModeToNearestNeighbor():最近邻方式
SetInterpolationModeToCubic():三次线性差值
设置完毕后,执行Update()便可完成切面计算。运行结果以下图:
图5.19 切片提取
学习三维图像切面的提取后,咱们在上节的程序上作一个扩展,实现一个稍微复杂的程序——经过滑动鼠标来切换三维图像切片,这也是医学图像处理软件中一个很基本的功能。实现该功能难点是怎样在VTK中控制鼠标来实时提取图像切片。在前面的章节中已经介绍观察者/命令(Observer/Command)模式,咱们也采用这种机制来实现。VTK中鼠标消息是在交互类型对象(interactorstyle)中响应,所以经过为交互类型对象(interactorstyle)添加观察者(observer)来监听相应的消息,当消息触发时,由命令模式执行相应的回调函数。闲话少说,放代码。
1: class vtkImageInteractionCallback : public vtkCommand
2: {
3: public:
4:
5: static vtkImageInteractionCallback *New()
6: {
7: return new vtkImageInteractionCallback;
8: }
9:
10: vtkImageInteractionCallback()
11: {
12: this->Slicing = 0;
13: this->ImageReslice = 0;
14: this->Interactor = 0;
15: }
16:
17: void SetImageReslice(vtkImageReslice *reslice)
18: {
19: this->ImageReslice = reslice;
20: }
21:
22: vtkImageReslice *GetImageReslice()
23: {
24: return this->ImageReslice;
25: }
26:
27: void SetInteractor(vtkRenderWindowInteractor *interactor)
28: {
29: this->Interactor = interactor;
30: }
31:
32: vtkRenderWindowInteractor *GetInteractor()
33: {
34: return this->Interactor;
35: }
36:
37: virtual void Execute(vtkObject *, unsigned long event, void *)
38: {
39: vtkRenderWindowInteractor *interactor = this->GetInteractor();
40:
41: int lastPos[2];
42: interactor->GetLastEventPosition(lastPos);
43: int currPos[2];
44: interactor->GetEventPosition(currPos);
45:
46: if (event == vtkCommand::LeftButtonPressEvent)
47: {
48: this->Slicing = 1;
49: }
50: else if (event == vtkCommand::LeftButtonReleaseEvent)
51: {
52: this->Slicing = 0;
53: }
54: else if (event == vtkCommand::MouseMoveEvent)
55: {
56: if (this->Slicing)
57: {
58: vtkImageReslice *reslice = this->ImageReslice;
59:
60: // Increment slice position by deltaY of mouse
61: int deltaY = lastPos[1] - currPos[1];
62:
63: reslice->Update();
64: double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
65: vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
66: // move the center point that we are slicing through
67: double point[4];
68: double center[4];
69: point[0] = 0.0;
70: point[1] = 0.0;
71: point[2] = sliceSpacing * deltaY;
72: point[3] = 1.0;
73: matrix->MultiplyPoint(point, center);
74: matrix->SetElement(0, 3, center[0]);
75: matrix->SetElement(1, 3, center[1]);
76: matrix->SetElement(2, 3, center[2]);
77: interactor->Render();
78: }
79: else
80: {
81: vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
82: interactor->GetInteractorStyle());
83: if (style)
84: {
85: style->OnMouseMove();
86: }
87: }
88: }
89: }
90:
91: private:
92: int Slicing;
93: vtkImageReslice *ImageReslice;
94: vtkRenderWindowInteractor *Interactor;
95: };
vtkImageInteractionCallback继承自vtkCommand类,并覆盖父类函数Execute()。该类提供了两个接口:SetImageReslice和SetInteractor。SetImageReslice用以设置vtkImageSlice对象,vtkImageSlice根据设置的变换矩阵提取三维图像切片。SetInteractor用以设置vtkRenderWindowInteractor,vtkRenderWindowInteractor类对象负责每次提取切片后刷新视图。
下面咱们重点来看一下Execute函数,该函数提供了具体的切片提取功能。在该函数里面,主要监听了三个消息:
vtkCommand::LeftButtonPressEvent,
vtkCommand::LeftButtonReleaseEvent,
vtkCommand::MouseMoveEvent,
前两个消息分别是鼠标左键的按下和弹起消息。当鼠标左键按下时,就设置切片提取标志为1,而当弹起时,将标志置为0。这样在鼠标移动时,只有在肯定切片提取标志为1时,执行切片提取功能。
vtkCommand::MouseMoveEvent即为鼠标移动消息。当检测到该消息时,首先检查切片提取标志,当为1时提取切片。提取切片时,须要为vtkImageSlice对象设置变换矩阵。这里在函数开始时,首先获取了鼠标滑动的先后两次点的位置lastPos和currPos。而后根据两点的Y坐标差deltaY,计算新的中心点center并变换至vtkImageSlice当前变换矩阵中,获得变换中心点,将其设置到原来的变换矩阵matrix中,并设置到vtkImageSlice中,最后执行interactor->Render()便可不断的根据鼠标移动刷新图像。
Command对象定义完毕后,便可为交互对象InteractorStyle添加观察者,响应鼠标消息。这里能够在上节的程序上进行修改,前面代码一致,只须要在最后添加以下代码:
1: vtkSmartPointer<vtkImageInteractionCallback> callback =
2: vtkSmartPointer<vtkImageInteractionCallback>::New();
3: callback->SetImageReslice(reslice);
4: callback->SetInteractor(renderWindowInteractor);
5:
6: imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
7: imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
8: imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);
9:
10: renderWindowInteractor->Start();
这里主要是定义了vtkImageInteractionCallback对象,并设置vtkImageSlice对象和vtkRenderWindowInteractor对象。而后为交互对象vtkInteractorStyle添加观察者来监控相应的消息,这里主要是三个消息:
vtkCommand::LeftButtonPressEvent,
vtkCommand::LeftButtonReleaseEvent,
vtkCommand::MouseMoveEvent,
当响应到这三个消息时,当即执行vtkImageInteractionCallback的Execute函数,以便实现切片的实时提取和更新。完成之后,运行程序,当鼠标在图像上移动时,会发现图像会跟着鼠标的移动而变化,神奇吧?有兴趣的话,还能够实现YZ平面、XZ平面切片提取,甚至是任意方向的切面提取。