Files

112 lines
3.5 KiB
Python
Raw Permalink Normal View History

2024-06-19 16:32:25 -06:00
from threading import Thread
import cv2
2024-05-29 10:11:05 -06:00
import numpy as np
2024-06-19 16:32:25 -06:00
import ktb
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
import open3d as o3d
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
voxel_size = 0.1
fitness_minimum = 0.9
2024-05-29 10:11:05 -06:00
2024-06-03 12:42:47 -06:00
vis = o3d.visualization.Visualizer()
2024-06-19 16:32:25 -06:00
vis.create_window()
2024-05-29 10:11:05 -06:00
2024-06-03 12:42:47 -06:00
reconstruction = o3d.geometry.PointCloud()
2024-06-19 16:32:25 -06:00
reconstruction.points = o3d.utility.Vector3dVector(np.random.rand(2, 3))
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
# vis.add_geometry(pcd)
vis.add_geometry(reconstruction)
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
def get_pcd(k):
points, colors = k.get_ptcld(colorized=True, scale=1000)
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
points = points.reshape((-1, 3))
colors = colors.reshape((-1, 3))
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
colors[:, [0, 2]] = colors[:, [2, 0]]
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
points = o3d.utility.Vector3dVector(points)
colors = o3d.utility.Vector3dVector(colors)
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
pcd = o3d.geometry.PointCloud()
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
pcd.points = points
pcd.colors = colors
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
pcd.points = pcd.voxel_down_sample(voxel_size=voxel_size).points
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
return pcd
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
def calc_transformation(cur_pcd, prev_pcd, cur_fpfh, prev_fpfh):
result = o3d.pipelines.registration.registration_ransac_based_on_feature_matching(
cur_pcd, prev_pcd, cur_fpfh, prev_fpfh,
mutual_filter=True,
max_correspondence_distance=voxel_size,
estimation_method=o3d.pipelines.registration.TransformationEstimationPointToPoint(False),
ransac_n=4, checkers=[],
criteria=o3d.pipelines.registration.RANSACConvergenceCriteria(max_iteration=100000, confidence=0.999)
)
print(result.fitness)
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
running = True
def run_loop():
k = ktb.Kinect()
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
prev_pcd = None
prev_fpfh = None
prev_normals = None
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
while running:
cur_pcd = get_pcd(k)
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
cur_pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
2024-06-03 12:42:47 -06:00
2024-06-19 16:32:25 -06:00
cur_fpfh = o3d.pipelines.registration.compute_fpfh_feature(cur_pcd,
o3d.geometry.KDTreeSearchParamHybrid(radius=0.25,
max_nn=100))
if prev_pcd is None:
reconstruction.points = cur_pcd.points
reconstruction.paint_uniform_color([0.8, 0.8, 0])
vis.update_geometry(reconstruction)
else:
# thread = Thread(target=calc_transformation, args=(cur_pcd, prev_pcd, cur_fpfh, prev_fpfh))
# thread.start()
result = o3d.pipelines.registration.registration_ransac_based_on_feature_matching(
cur_pcd, prev_pcd, cur_fpfh, prev_fpfh,
mutual_filter=True,
max_correspondence_distance=voxel_size,
estimation_method=o3d.pipelines.registration.TransformationEstimationPointToPoint(False),
ransac_n=4, checkers=[],
criteria=o3d.pipelines.registration.RANSACConvergenceCriteria(max_iteration=100000, confidence=0.999)
)
# print(result.transformation)
# eg_p2p = o3d.pipelines.registration.registration_icp(
# reconstruction, pcd, threshold, trans_init,
# o3d.pipelines.registration.TransformationEstimationPointToPoint())
# if result.fitness > fitness_minimum:
reconstruction.points.extend(cur_pcd.transform(-result.transformation).points)
2024-06-03 12:42:47 -06:00
vis.update_geometry(reconstruction)
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
prev_pcd = cur_pcd
prev_fpfh = cur_fpfh
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
print("update")
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
t = Thread(target=run_loop)
t.start()
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
while running:
# vis.update_geometry(reconstruction)
# print("E")
if cv2.waitKey(1) & 0xFF == ord('q'):
running = False
running = vis.poll_events()
vis.update_renderer()
2024-05-29 10:11:05 -06:00
2024-06-19 16:32:25 -06:00
t.join()