1.点云配准
在计算机逆向工程中,通过三维扫描等实物数字化技术可以获取各种点云数据。但是受到测量环境和设备的影响,再一次测量的情况下,难以获取实物整体的点云数据,因此需要多次从不同角度进行测量。但不同的测量数据之间可能会存在平移错误或旋转错位等问题。这就需要使用点云配准技术来对测量点云数据进行局部配准和整合,以得到完整的模型数据。
另外,在外科手术导航技术中,图像标记点技术与人体表面标记点的配准是一个关键步骤,对于手术定位的精度有着重要的影响。通常这需要使用基于标记点的配准技术。因此,点云配准及时对一组源点云数据应用一个空间变换,使得变换后的数据与目标点云数据能够一一映射,使两组数据之间的平均距离误差最小。
2.LandMark配准实验
VTKLandMarkTransform实现了片几点配准算法,使得两个点集在配准后的平均距离最小。通过SetSourceLandmarks()和SetTargetLandmarks()函数分别设置源标记点集和目标标记点集。需要注意的是,源标记点集和目标标记点集序号要对应。基于标记点的配准算法如下所示:1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 VTK_MODULE_INIT(vtkRenderingFreeType); 4 VTK_MODULE_INIT(vtkInteractionStyle); 5 6 #include <vtkSmartPointer.h> 7 #include <vtkPoints.h> 8 #include <vtkLandmarkTransform.h> 9 #include <vtkPolyData.h> 10 #include <vtkVertexGlyphFilter.h> 11 #include <vtkTransformPolyDataFilter.h> 12 #include <vtkPolyDataMapper.h> 13 #include <vtkActor.h> 14 #include <vtkProperty.h> 15 #include <vtkRenderer.h> 16 #include <vtkRenderWindow.h> 17 #include <vtkRenderWindowInteractor.h> 18 #include <vtkAxesActor.h> 19 //研究一下坐标系显示位置 20 #include <vtkOrientationMarkerWidget.h> 21 int main() 22 { 23 vtkSmartPointer<vtkPoints> sourcePoints = 24 vtkSmartPointer<vtkPoints>::New(); 25 double sourcePoint1[3] = { 0.5, 0, 0 }; 26 sourcePoints->InsertNextPoint(sourcePoint1); 27 double sourcePoint2[3] = { 0, 0.5, 0 }; 28 sourcePoints->InsertNextPoint(sourcePoint2); 29 double sourcePoint3[3] = { 0, 0, 0.5 }; 30 sourcePoints->InsertNextPoint(sourcePoint3); 31 32 vtkSmartPointer<vtkPoints> targetPoints = 33 vtkSmartPointer<vtkPoints>::New(); 34 double targetPoint1[3] = { 0.0, 0.0, 0.55 }; 35 targetPoints->InsertNextPoint(targetPoint1); 36 double targetPoint2[3] = { 0.0, 0.55, 0.0 }; 37 targetPoints->InsertNextPoint(targetPoint2); 38 double targetPoint3[3] = { -0.55, 0.0, 0.0 }; 39 targetPoints->InsertNextPoint(targetPoint3); 40 //利用Landmark算法求变换矩阵 41 vtkSmartPointer<vtkLandmarkTransform> landmarkTransform = 42 vtkSmartPointer<vtkLandmarkTransform>::New(); 43 landmarkTransform->SetSourceLandmarks(sourcePoints); 44 landmarkTransform->SetTargetLandmarks(targetPoints); 45 landmarkTransform->SetModeToRigidBody(); //执行刚体配准 46 landmarkTransform->Update(); 47 构造PolyData类型 进行图形可视化 48 vtkSmartPointer<vtkPolyData> source = 49 vtkSmartPointer<vtkPolyData>::New(); 50 source->SetPoints(sourcePoints); 51 vtkSmartPointer<vtkPolyData> target = 52 vtkSmartPointer<vtkPolyData>::New(); 53 target->SetPoints(targetPoints); 54 / 55 vtkSmartPointer<vtkVertexGlyphFilter> sourceGlyphFilter = 56 vtkSmartPointer<vtkVertexGlyphFilter>::New(); 57 sourceGlyphFilter->SetInputData(source); 58 sourceGlyphFilter->Update(); 59 60 vtkSmartPointer<vtkVertexGlyphFilter> targetGlyphFilter = 61 vtkSmartPointer<vtkVertexGlyphFilter>::New(); 62 targetGlyphFilter->SetInputData(target); 63 targetGlyphFilter->Update(); 64 源数据施加配准变换矩阵 65 vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = 66 vtkSmartPointer<vtkTransformPolyDataFilter>::New(); 67 transformFilter->SetInputData(sourceGlyphFilter->GetOutput()); 68 transformFilter->SetTransform(landmarkTransform); 69 transformFilter->Update(); 70 / 71 vtkSmartPointer<vtkPolyDataMapper> sourceMapper = 72 vtkSmartPointer<vtkPolyDataMapper>::New(); 73 sourceMapper->SetInputConnection(sourceGlyphFilter->GetOutputPort()); 74 75 vtkSmartPointer<vtkActor> sourceActor = 76 vtkSmartPointer<vtkActor>::New(); 77 sourceActor->SetMapper(sourceMapper); 78 sourceActor->GetProperty()->SetColor(1, 1, 0); 79 sourceActor->GetProperty()->SetPointSize(10); 80 81 vtkSmartPointer<vtkPolyDataMapper> targetMapper = 82 vtkSmartPointer<vtkPolyDataMapper>::New(); 83 targetMapper->SetInputConnection(targetGlyphFilter->GetOutputPort()); 84 85 vtkSmartPointer<vtkActor> targetActor = 86 vtkSmartPointer<vtkActor>::New(); 87 targetActor->SetMapper(targetMapper); 88 targetActor->GetProperty()->SetColor(0, 1, 0); 89 targetActor->GetProperty()->SetPointSize(10); 90 91 vtkSmartPointer<vtkPolyDataMapper> solutionMapper = 92 vtkSmartPointer<vtkPolyDataMapper>::New(); 93 solutionMapper->SetInputConnection(transformFilter->GetOutputPort()); 94 95 vtkSmartPointer<vtkActor> solutionActor = 96 vtkSmartPointer<vtkActor>::New(); 97 solutionActor->SetMapper(solutionMapper); 98 solutionActor->GetProperty()->SetColor(1, 0, 0); 99 solutionActor->GetProperty()->SetPointSize(10); 100 101 vtkSmartPointer<vtkRenderer> render = 102 vtkSmartPointer<vtkRenderer>::New(); 103 render->AddActor(sourceActor); 104 render->AddActor(targetActor); 105 render->AddActor(solutionActor); 106 render->SetBackground(0, 0, 0); 107 108 vtkSmartPointer<vtkRenderWindow> rw = 109 vtkSmartPointer<vtkRenderWindow>::New(); 110 rw->AddRenderer(render); 111 rw->SetSize(480, 480); 112 rw->SetWindowName("Regisration by Landmark"); 113 //设置坐标系显示功能 114 vtkSmartPointer<vtkAxesActor> axes = 115 vtkSmartPointer<vtkAxesActor>::New(); 116 axes->SetScale(10); 117 render->AddActor(axes); 118 119 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 120 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 121 rwi->SetRenderWindow(rw); 122 /************************************************************/ 123 vtkSmartPointer<vtkOrientationMarkerWidget> widget = 124 vtkSmartPointer<vtkOrientationMarkerWidget>::New(); 125 widget->SetOutlineColor(0.9300, 0.5700, 0.1300); 126 widget->SetOrientationMarker(axes); 127 widget->SetInteractor(rwi); //加入鼠标交互 128 widget->SetViewport(0.0, 0.0, 0.3, 0.3); //设置显示位置 129 widget->SetEnabled(1); 130 widget->InteractiveOn();//开启鼠标交互 131 /************************************************************/ 132 render->ResetCamera(); 133 rw->Render(); 134 rwi->Initialize(); 135 rwi->Start(); 136 137 return 0; 138 }
输出结果:
黄色散点代表待配准点集;绿色散点代表金标准;红色散点代表施加变换矩阵后的源数据点集。vtkVertexGliphFilter类显示点集!!!VTKTransformPolyDataFilter用来对源标记点进行变换来显示配准后的点集,SetTransform()直接设置为vtkLandmarkTransform的变换结果。
2.VTKLandmarkTransform类
VTKLandmarkTransform类的使用比较简单,只需要设定源标记点和目标标记点。SetModeToRigidBody()函数用于设置其配准变换类型为刚体变换,仅包括简单的平移和旋转(六个自由度)。此外还有一个更为广泛应用的函数——SetModeToSimilarity,设置为相似变换,包括平移、旋转和放缩变换(七个自由度)。以及SetModeToAffine()函数设置仿射变换。默认情况,就采用相似变换进行配准。
3.vtkAxesActor类
有的时候,在显示三维物体时,我们希望知道当前场景对应的坐标系位置或者方向,这样在旋转物体的时候,就能够很清楚地看到当前正对这视野的是什么面xy平面,还是y轴等信息了。
在vtk库中有一个vtkAxesActor负责显示坐标系,在查阅了vtk的wiki之后,找到了两个示例,在这里将两者结合起来,放在同一个例子中显示,并用java代码重写。其中,第一个示例:http://vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Axes 是直接将vtkAxesActor加入到renderer中进行显示:1 //设置坐标系显示功能 2 vtkSmartPointer<vtkAxesActor> axes = 3 vtkSmartPointer<vtkAxesActor>::New(); 4 axes->SetScale(10); 5 render->AddActor(axes);
而第二个示例:
http://vtk.org/Wiki/VTK/Examples/Cxx/Visualization/DisplayCoordinateAxes 是以Widget的方式在一个独立的视口中显示,比较适合用来放在左下角指示当前的坐标系位置:1 vtkSmartPointer<vtkOrientationMarkerWidget> widget = 2 vtkSmartPointer<vtkOrientationMarkerWidget>::New(); 3 widget->SetOutlineColor(0.9300, 0.5700, 0.1300); 4 widget->SetOrientationMarker(axes); 5 widget->SetInteractor(rwi); //加入鼠标交互 6 widget->SetViewport(0.0, 0.0, 0.3, 0.3); //设置显示位置 7 widget->SetEnabled(1); 8 widget->InteractiveOn();//开启鼠标交互