IterativeClosestPoints icp配准矩阵

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①ICP配算法的运用,②运用ICP的配准矩阵

二:代码及注释

python 复制代码
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkCellArray, vtkPolyData, vtkIterativeClosestPointTransform
from vtkmodules.vtkFiltersGeneral import vtkTransformPolyDataFilter

# from vtkmodules.vtkFiltersSources import

def main():
    """
    原文写法
    sourcePoints = vtkPoints()
    sourceVertices = vtkCellArray()

    sp_id = sourcePoints.InsertNextPoint(1.0, 0.1, 0.0) # 返回为0
    sourceVertices.InsertNextCell(1)
    sourceVertices.InsertCellPoint(sp_id)

    sp_id = sourcePoints.InsertNextPoint(0.1, 1.1, 0.0) # 返回为1
    sourceVertices.InsertNextCell(1)
    sourceVertices.InsertCellPoint(sp_id)

    sp_id = sourcePoints.InsertNextPoint(0.0, 0.1, 1.0)  #  返回为2
    sourceVertices.InsertNextCell(1)
    sourceVertices.InsertCellPoint(sp_id)

    source = vtkPolyData()
    source.SetPoints(sourcePoints)
    source.SetVerts(sourceVertices)

    我觉得这种写法与一般示例的写法出入较大,在这里重写
    """
    sourcePoints = vtkPoints()
    sourcePoints.InsertNextPoint(1.0, 0.1, 0.0)
    sourcePoints.InsertNextPoint(0.1, 1.1, 0.0)
    sourcePoints.InsertNextPoint(0.0, 0.1, 1.0)

    sourceCells = vtkCellArray()
    sourceCells.InsertNextCell(1, [0])
    sourceCells.InsertNextCell(1, [1])
    sourceCells.InsertNextCell(1, [2])

    source = vtkPolyData()
    source.SetPoints(sourcePoints)
    source.SetVerts(sourceCells)

    pointCount = 3
    for index in range(pointCount):
        point = [0, 0, 0]
        sourcePoints.GetPoint(index, point)
        print("source point[%s]=%s" % (index, point))

    # target_points。目标点位
    targetPoints = vtkPoints()
    targetPoints.InsertNextPoint(1, 0, 0)
    targetPoints.InsertNextPoint(0, 1, 0)
    targetPoints.InsertNextPoint(0, 0, 1)

    targetCells = vtkCellArray()
    targetCells.InsertNextCell(1, [0])
    targetCells.InsertNextCell(1, [1])
    targetCells.InsertNextCell(1, [2])
    target = vtkPolyData()
    target.SetPoints(targetPoints)
    target.SetVerts(targetCells)

    pointCount = 3
    for index in range(pointCount):
        point = [0, 0, 0]
        targetPoints.GetPoint(index, point)
        print("target point[%s]=%s" % (index, point))

    # 构建icp
    icp = vtkIterativeClosestPointTransform()
    icp.SetSource(source)
    icp.SetTarget(target)
    icp.GetLandmarkTransform().SetModeToRigidBody()  # 设为刚体,只能旋转和平移,不能缩放
    icp.SetMaximumNumberOfIterations(20)  # 设置最大迭代次数为20
    icp.StartByMatchingCentroidsOn()  # 开启ICP算法的质心预匹配步骤,它会在第一次迭代时,先应用一个平移变换,将源点云的质心移动到目标点云的质心位置
    icp.Modified() # 通知 VTK 管道,这个对象(icp 变换对象)的参数已被修改
    icp.Update()

    """
    vtkTransformPolyDataFilter  将一个 vtkPolyData 数据集应用几何变换,然后生成一个新的、已变换的 vtkPolyData 对象
    下面一段代码的含义是指将source乘以ipc得到的4x4配准矩阵
    """
    icpTransformFilter = vtkTransformPolyDataFilter()
    icpTransformFilter.SetInputData(source)
    icpTransformFilter.SetTransform(icp)
    icpTransformFilter.Update()

    transformedSource = icpTransformFilter.GetOutput()

    # ============ display transformed points ==============
    pointCount = 3
    for index in range(pointCount):
        point = [0, 0, 0]
        transformedSource.GetPoint(index, point)
        print("transformed source point[%s]=%s" % (index, point))

if __name__ == '__main__':
    main()
相关推荐
ZC跨境爬虫11 小时前
Scrapy实战爬取5sing网站:Pipeline优化+全流程踩坑复盘,从报错到数据落地
前端·爬虫·python·scrapy
ZhuNian的学习乐园11 小时前
LLM智能体调度:从ReAct到多智能体调度
人工智能·python·深度学习
喵叔哟11 小时前
6.【.NET10 实战--孢子记账--产品智能化】--认证与安全包
python·安全·flask
小鱼~~11 小时前
Jupyter Notebook 最常用快捷键
python·jupyter
wgzrmlrm7412 小时前
如何从SQL中提取年份或月份:EXTRACT与日期函数用法
jvm·数据库·python
web3.088899912 小时前
淘宝、京东、1688 拍立淘图搜 API 均返回 JSON 格式
python·json
IT莫染12 小时前
用脚本解放双手!我写了个WorkBuddy自动签到工具
python
d1z88812 小时前
(十八)32天GPU测试从入门到精通-TensorRT-LLM 部署与优化day16
人工智能·python·深度学习·gpu·tensorrt
qq_2837200512 小时前
Python 面向对象编程(OOP)从入门到精通
python·oop·面对对象
linux_map12 小时前
大模型微调实战指南
人工智能·python·ai·策略模式