Advertisement
Guest User

ICP code

a guest
Jul 2nd, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | Software | 0 0
  1. from pickle import TRUE
  2. import sys
  3. sys.coinit_flags = 2
  4. import open3d
  5. import pythoncom
  6. import numpy
  7. import copy
  8. import pyvista as pv
  9. import pcl
  10. import tkinter
  11. from tkinter import filedialog
  12. import json
  13. import os
  14.  
  15. def showPCD(source, target, transformation, reference_frame):
  16.     temp_source = copy.deepcopy(source)
  17.     temp_target = copy.deepcopy(target)
  18.     if reference_frame == 'camera':
  19.         temp_coordinate = open3d.geometry.TriangleMesh.create_coordinate_frame(size=100)
  20.     else:
  21.         temp_coordinate = copy.deepcopy(reference_frame)
  22.     temp_source.transform(transformation)
  23.     open3d.visualization.draw_geometries([temp_source, temp_target, temp_coordinate],'risultati',2160,1440)
  24.     return None
  25.  
  26. def setFirstTransform(source, target):
  27.  
  28.     while True:
  29.         # l'utente inserisce i parametri per la trasformazione successiva
  30.         #x = float(input('inserire x'))
  31.         #y = float(input('inserire y'))
  32.         #z = float(input('inserire z'))
  33.         #tetax = float(input('inserire tetax'))
  34.         #tetay = float(input('inserire tetay'))
  35.         #tetaz = float(input('inserire tetaz'))
  36.         #tetax_rad = tetax * (numpy.pi / 180)
  37.         #tetay_rad = tetay * (numpy.pi / 180)
  38.         #tetaz_rad = tetaz * (numpy.pi / 180)
  39.         #teta_array = numpy.array([tetax_rad, tetay_rad, tetaz_rad])
  40.  
  41.         # calcolo la matrice di rotazione in base agli angoli dati dall'utente (in terna mobile)
  42.         #rot_matrix = open3d.geometry.get_rotation_matrix_from_xyz(teta_array)
  43.  
  44.         actual_transform = numpy.array([[0.96,0.04,0.00, -167.54],
  45.                             [0.02,-0.55,-0.80,71.67],
  46.                             [-0.03,0.79,-0.55,-520.23],
  47.                             [0, 0, 0, 1]])
  48.  
  49.  
  50.         #answer = input('Press n to finish the process')
  51.  
  52.         if TRUE: #answer == 'n':
  53.             print(actual_transform)
  54.             return actual_transform
  55.             break
  56. #1
  57. root = tkinter.Tk()
  58. file_path = filedialog.askopenfilename()
  59. root.withdraw()
  60. root.destroy()
  61. stl_source = open3d.io.read_triangle_mesh(file_path)
  62. source = open3d.geometry.PointCloud()
  63. source.points = stl_source.vertices
  64. source.paint_uniform_color([0.945, 0.547, 0.156])
  65. #2
  66. root = tkinter.Tk()
  67. file_path2 = filedialog.askopenfilename()
  68. root.withdraw()
  69. root.destroy()
  70. #pyvista
  71. point_cloud = pv.read(file_path2)
  72. point_cloud.plot()
  73. plotter = pv.Plotter()
  74. plotter.add_points(point_cloud)
  75. points = numpy.array(point_cloud.points)
  76. o3d_point_cloud = open3d.geometry.PointCloud()
  77. o3d_point_cloud.points = open3d.utility.Vector3dVector(points)
  78. o3d_point_cloud.scale(scale=1000, center=[0, 0, 0])
  79. open3d.visualization.draw_geometries([o3d_point_cloud])
  80.  
  81. showPCD(source, o3d_point_cloud, numpy.identity(4), 'camera')
  82.  
  83. #FIRST TRANSFORM
  84.  
  85. first_transform = setFirstTransform(source, o3d_point_cloud)
  86.  
  87. o3d_point_cloud.estimate_normals(open3d.geometry.KDTreeSearchParamHybrid(radius=10, max_nn=10))
  88. actual_result = open3d.pipelines.registration.registration_icp(source, o3d_point_cloud, 0.1, first_transform,
  89.                                                                        open3d.pipelines.registration.TransformationEstimationPointToPoint(),
  90.                                                                        open3d.pipelines.registration.ICPConvergenceCriteria(relative_fitness=1e-10,
  91.                                                                                                                             relative_rmse=1e-10,
  92.                                                                                                                             max_iteration=500))
  93. reference_frame = open3d.geometry.TriangleMesh.create_coordinate_frame(size=100)
  94. matrix_result = actual_result.transformation
  95. print(actual_result)
  96. showPCD(source, o3d_point_cloud, matrix_result, 'camera')
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement