File size: 23,150 Bytes
37ee115 cac424e 37ee115 3b20b02 37ee115 cac424e 3b20b02 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 3b20b02 37ee115 3b20b02 37ee115 3b20b02 37ee115 3b20b02 37ee115 cac424e 37ee115 3b20b02 37ee115 3b20b02 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 cac424e 37ee115 6826d64 37ee115 cac424e 37ee115 3b20b02 37ee115 3b20b02 cac424e 37ee115 cac424e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | #!/usr/bin/env python3
"""
Point Cloud Registration Demo using Open3D and 3DMatch RedKitchen
Interactive Gradio app for pairwise point cloud registration
"""
import json
import os
import copy
import numpy as np
import gradio as gr
from pathlib import Path
import tempfile
from typing import Tuple, Dict, List
import plotly.graph_objects as go
try:
import open3d as o3d
except ImportError:
raise ImportError("Please install open3d: pip install open3d")
def get_file_path(file_obj):
"""Convert a Gradio file output or string into a filesystem path."""
if file_obj is None:
return None
if isinstance(file_obj, str):
return file_obj
if hasattr(file_obj, "name"):
return file_obj.name
if isinstance(file_obj, dict):
return file_obj.get("path") or file_obj.get("name")
return str(file_obj)
def point_cloud_to_arrays(pcd, max_points=30000):
"""Convert Open3D point cloud to numpy array, optionally downsampled."""
points = np.asarray(pcd.points)
if len(points) == 0:
return points
if len(points) > max_points:
idx = np.random.choice(len(points), max_points, replace=False)
points = points[idx]
return points
class RegistrationDemo:
def __init__(self, examples_dir: str = "examples"):
self.examples_dir = Path(examples_dir)
self.metadata = self._load_metadata()
self.current_source = None
self.current_target = None
self.current_source_down = None
self.current_target_down = None
def _load_metadata(self) -> Dict:
"""Load pair metadata"""
metadata_file = self.examples_dir / "pair_metadata.json"
if metadata_file.exists():
with open(metadata_file) as f:
return json.load(f)
return {}
def get_pair_choices(self) -> List[str]:
"""Get list of available demo pairs"""
if not self.metadata:
return []
return sorted(self.metadata.keys())
def load_demo_pair(self, pair_key: str) -> Tuple[str, str]:
"""Load source and target from demo pair"""
if pair_key not in self.metadata:
return None, None
pair_info = self.metadata[pair_key]
source_file = self.examples_dir / pair_info["source_file"]
target_file = self.examples_dir / pair_info["target_file"]
if source_file.exists() and target_file.exists():
return str(source_file), str(target_file)
return None, None
def load_point_clouds(self, source_path: str, target_path: str) -> Tuple[bool, str]:
"""Load point clouds from files"""
try:
self.current_source = o3d.io.read_point_cloud(source_path)
self.current_target = o3d.io.read_point_cloud(target_path)
n_source = len(self.current_source.points)
n_target = len(self.current_target.points)
return True, f"β Loaded: {n_source} source points, {n_target} target points"
except Exception as e:
return False, f"β Error loading point clouds: {e}"
def preprocess_point_clouds(self, voxel_size: float) -> Tuple[bool, str]:
"""Preprocess loaded point clouds"""
if self.current_source is None or self.current_target is None:
return False, "β No point clouds loaded"
try:
# Remove non-finite points
def remove_non_finite(pcd):
pcd_clean = o3d.geometry.PointCloud()
mask = ~np.any(~np.isfinite(np.asarray(pcd.points)), axis=1)
pcd_clean.points = o3d.utility.Vector3dVector(np.asarray(pcd.points)[mask])
if pcd.has_normals():
pcd_clean.normals = o3d.utility.Vector3dVector(np.asarray(pcd.normals)[mask])
pcd_clean.remove_duplicated_points()
return pcd_clean
source_clean = remove_non_finite(self.current_source)
target_clean = remove_non_finite(self.current_target)
# Downsample
self.current_source_down = source_clean.voxel_down_sample(voxel_size)
self.current_target_down = target_clean.voxel_down_sample(voxel_size)
n_source = len(self.current_source_down.points)
n_target = len(self.current_target_down.points)
if n_source < 10 or n_target < 10:
return False, f"β Too few points after downsampling: {n_source}, {n_target}"
return True, f"β Preprocessed: {n_source} source, {n_target} target points"
except Exception as e:
return False, f"β Preprocessing error: {e}"
def estimate_normals_and_features(
self,
voxel_size: float,
normal_radius_mult: float,
fpfh_radius_mult: float,
) -> Tuple[bool, str, object, object]:
"""Estimate normals and compute FPFH features"""
if self.current_source_down is None or self.current_target_down is None:
return False, "β No preprocessed point clouds", None, None
try:
normal_radius = voxel_size * normal_radius_mult
fpfh_radius = voxel_size * fpfh_radius_mult
# Estimate normals
self.current_source_down.estimate_normals(
o3d.geometry.KDTreeSearchParamRadius(radius=normal_radius)
)
self.current_target_down.estimate_normals(
o3d.geometry.KDTreeSearchParamRadius(radius=normal_radius)
)
# Compute FPFH
source_fpfh = o3d.pipelines.registration.compute_fpfh_feature(
self.current_source_down,
o3d.geometry.KDTreeSearchParamRadius(radius=fpfh_radius)
)
target_fpfh = o3d.pipelines.registration.compute_fpfh_feature(
self.current_target_down,
o3d.geometry.KDTreeSearchParamRadius(radius=fpfh_radius)
)
return True, "β Features computed", source_fpfh, target_fpfh
except Exception as e:
return False, f"β Feature computation error: {e}", None, None
def ransac_registration(
self,
voxel_size: float,
normal_radius_mult: float = 2.0,
fpfh_radius_mult: float = 5.0,
distance_mult: float = 1.5,
max_iterations: int = 50000,
) -> Tuple[bool, str, np.ndarray]:
"""Global registration using RANSAC"""
if self.current_source_down is None or self.current_target_down is None:
return False, "β No preprocessed point clouds", None
try:
success, msg, source_fpfh, target_fpfh = self.estimate_normals_and_features(
voxel_size,
normal_radius_mult,
fpfh_radius_mult,
)
if not success:
return False, msg, None
distance_threshold = voxel_size * distance_mult
result = o3d.pipelines.registration.registration_ransac_based_on_feature_matching(
self.current_source_down, self.current_target_down,
source_fpfh, target_fpfh,
mutual_filter=False,
max_correspondence_distance=distance_threshold,
estimation_method=o3d.pipelines.registration.TransformationEstimationPointToPoint(False),
ransac_n=3,
checkers=[
o3d.pipelines.registration.CorrespondenceCheckerBasedOnEdgeLength(0.9),
o3d.pipelines.registration.CorrespondenceCheckerBasedOnDistance(distance_threshold)
],
criteria=o3d.pipelines.registration.RANSACConvergenceCriteria(
int(max_iterations),
0.999,
)
)
msg = f"β RANSAC: fitness={result.fitness:.4f}, RMSE={result.inlier_rmse:.6f}"
return True, msg, result.transformation
except Exception as e:
return False, f"β RANSAC error: {e}", None
def icp_registration(self, init_transform: np.ndarray, voxel_size: float,
distance_mult: float = 0.4, max_iterations: int = 50) -> Tuple[bool, str, np.ndarray]:
"""Local registration using ICP"""
if self.current_source_down is None or self.current_target_down is None:
return False, "β No preprocessed point clouds", None
try:
normal_radius = voxel_size * 2.0
if not self.current_source_down.has_normals():
self.current_source_down.estimate_normals(
o3d.geometry.KDTreeSearchParamRadius(radius=normal_radius)
)
if not self.current_target_down.has_normals():
self.current_target_down.estimate_normals(
o3d.geometry.KDTreeSearchParamRadius(radius=normal_radius)
)
distance_threshold = voxel_size * distance_mult
result = o3d.pipelines.registration.registration_icp(
self.current_source_down, self.current_target_down,
max_correspondence_distance=distance_threshold,
init=init_transform,
criteria=o3d.pipelines.registration.ICPConvergenceCriteria(max_iteration=max_iterations),
estimation_method=o3d.pipelines.registration.TransformationEstimationPointToPlane()
)
msg = f"β ICP: fitness={result.fitness:.4f}, RMSE={result.inlier_rmse:.6f}"
return True, msg, result.transformation
except Exception as e:
return False, f"β ICP error: {e}", None
def create_before_visualization(self) -> Tuple[go.Figure, str]:
"""Create before registration Plotly visualization."""
if self.current_source is None or self.current_target is None:
return None, "β No point clouds loaded"
try:
src = point_cloud_to_arrays(self.current_source)
tgt = point_cloud_to_arrays(self.current_target)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=src[:, 0], y=src[:, 1], z=src[:, 2],
mode="markers",
marker=dict(size=1.5, color="orange"),
name="Source"
))
fig.add_trace(go.Scatter3d(
x=tgt[:, 0], y=tgt[:, 1], z=tgt[:, 2],
mode="markers",
marker=dict(size=1.5, color="cyan"),
name="Target"
))
fig.update_layout(
scene=dict(aspectmode="data"),
margin=dict(l=0, r=0, t=30, b=0),
height=520,
legend=dict(x=0, y=1)
)
return fig, "β Before visualization ready"
except Exception as e:
return None, f"β Visualization error: {e}"
def create_after_visualization(self, transform: np.ndarray) -> Tuple[go.Figure, str]:
"""Create after registration Plotly visualization."""
if self.current_source is None or self.current_target is None:
return None, "β No point clouds loaded"
if transform is None:
return None, "β No transformation available"
try:
source_transformed = copy.deepcopy(self.current_source)
source_transformed.transform(transform)
src = point_cloud_to_arrays(source_transformed)
tgt = point_cloud_to_arrays(self.current_target)
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=src[:, 0], y=src[:, 1], z=src[:, 2],
mode="markers",
marker=dict(size=1.5, color="lime"),
name="Aligned Source"
))
fig.add_trace(go.Scatter3d(
x=tgt[:, 0], y=tgt[:, 1], z=tgt[:, 2],
mode="markers",
marker=dict(size=1.5, color="cyan"),
name="Target"
))
fig.update_layout(
scene=dict(aspectmode="data"),
margin=dict(l=0, r=0, t=30, b=0),
height=520,
legend=dict(x=0, y=1)
)
return fig, "β After visualization ready"
except Exception as e:
return None, f"β Visualization error: {e}"
def get_metrics_and_transform(self, fitness: float, rmse: float,
transform: np.ndarray) -> Tuple[str, str]:
"""Format metrics and transformation matrix for display"""
metrics_text = f"""
Fitness: {fitness:.6f}
RMSE: {rmse:.6f}
""".strip()
if transform is not None:
transform_text = "Transformation Matrix (4x4):\n"
for row in transform:
transform_text += " ".join([f"{x:12.6f}" for x in row]) + "\n"
else:
transform_text = "No transformation available"
return metrics_text, transform_text
def save_aligned_source(self, transform: np.ndarray) -> Tuple[str, str]:
"""Save transformed source point cloud only."""
if self.current_source is None:
return None, "β No source point cloud loaded"
if transform is None:
return None, "β No transformation available"
try:
aligned_source = copy.deepcopy(self.current_source)
aligned_source.transform(transform)
with tempfile.NamedTemporaryFile(suffix=".ply", delete=False) as f:
o3d.io.write_point_cloud(f.name, aligned_source)
return f.name, "β Aligned source saved"
except Exception as e:
return None, f"β Save aligned source error: {e}"
def register(self, pair_selection: str, mode: str, voxel_size: float,
normal_radius_mult: float, fpfh_radius_mult: float,
ransac_dist_mult: float, ransac_iter: int,
icp_dist_mult: float, icp_iter: int,
source_upload, target_upload) -> Tuple:
"""Main registration pipeline"""
# Load point clouds
if pair_selection != "Upload" and pair_selection:
source_path, target_path = self.load_demo_pair(pair_selection)
if source_path is None:
return None, None, "β Failed to load demo pair", "", "", ""
else:
if source_upload is None or target_upload is None:
return None, None, "β Please upload both source and target", "", "", ""
source_path = get_file_path(source_upload)
target_path = get_file_path(target_upload)
# Load
success, msg = self.load_point_clouds(source_path, target_path)
if not success:
return None, None, msg, "", "", ""
# Preprocess
success, msg = self.preprocess_point_clouds(voxel_size)
if not success:
return None, None, msg, "", "", ""
# Registration
final_transform = np.eye(4)
ransac_msg = ""
icp_msg = ""
try:
if mode in ["RANSAC + ICP", "RANSAC only"]:
success, ransac_msg, transform = self.ransac_registration(
voxel_size,
normal_radius_mult,
fpfh_radius_mult,
ransac_dist_mult,
int(ransac_iter),
)
if not success:
return None, None, ransac_msg, "", "", ""
final_transform = transform
if mode in ["RANSAC + ICP", "ICP only"]:
if mode == "ICP only":
init = np.eye(4)
else:
init = final_transform
success, icp_msg, transform = self.icp_registration(
init, voxel_size, icp_dist_mult, icp_iter
)
if not success:
return None, None, icp_msg, "", "", ""
final_transform = transform
# Visualizations
before_file, before_msg = self.create_before_visualization()
after_file, after_msg = self.create_after_visualization(final_transform)
if before_file is None or after_file is None:
return None, None, before_msg or after_msg, "", "", ""
# Compute final fitness/RMSE
source_aligned = copy.deepcopy(self.current_source_down)
source_aligned.transform(final_transform)
distances = source_aligned.compute_point_cloud_distance(self.current_target_down)
distances = np.asarray(distances)
fitness = np.sum(distances < voxel_size * 1.5) / len(distances)
rmse = np.sqrt(np.mean(distances ** 2))
metrics_text, transform_text = self.get_metrics_and_transform(fitness, rmse, final_transform)
status_msg = f"β Registration complete!\n{ransac_msg}\n{icp_msg}".strip()
aligned_file, aligned_msg = self.save_aligned_source(final_transform)
if aligned_file is None:
return before_file, after_file, status_msg + "\n" + aligned_msg, metrics_text, transform_text, None
return before_file, after_file, status_msg, metrics_text, transform_text, aligned_file
except Exception as e:
return None, None, f"β Registration error: {e}", "", "", ""
def create_gradio_interface():
"""Create Gradio interface"""
demo = RegistrationDemo(examples_dir="examples")
pair_choices = demo.get_pair_choices()
if not pair_choices:
pair_choices = ["(No demo pairs loaded)"]
with gr.Blocks(title="Point Cloud Registration Demo") as app:
gr.Markdown("""
# Point Cloud Registration Demo
An interactive demo for pairwise 3D point cloud registration using **Open3D**.
This demo includes pre-selected point cloud pairs from the **3DMatch Geometric Registration Benchmark**, specifically the **RedKitchen** scene.
These point clouds are 3D fragments reconstructed from depth frames using TSDF fusion.
Below, you can choose a provided demo pair or upload your own source and target point clouds for registration.
The registration pipeline uses:
- **RANSAC + FPFH** for global registration to estimate the initial transformation.
- **ICP** for local refinement to improve the alignment.
- Supported modes include **RANSAC + ICP**, **RANSAC only**, and **ICP only**.
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Input")
pair_selection = gr.Dropdown(
choices=["Upload"] + pair_choices,
value="Upload" if not pair_choices else pair_choices[0],
label="Dataset Pair"
)
source_upload = gr.File(
label="Source Point Cloud (PLY)",
file_types=[".ply"],
type="filepath",
)
target_upload = gr.File(
label="Target Point Cloud (PLY)",
file_types=[".ply"],
type="filepath",
)
with gr.Column(scale=1):
gr.Markdown("### Registration Settings")
mode = gr.Radio(
choices=["RANSAC + ICP", "RANSAC only", "ICP only"],
value="RANSAC + ICP",
label="Registration Mode"
)
voxel_size = gr.Slider(0.01, 0.2, value=0.05, step=0.01, label="Voxel Size")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### RANSAC Parameters")
ransac_dist_mult = gr.Slider(0.5, 3.0, value=1.5, step=0.1, label="Distance Multiplier")
ransac_iter = gr.Slider(1000, 100000, value=50000, step=5000, label="Max Iterations")
with gr.Column(scale=1):
gr.Markdown("### ICP Parameters")
icp_dist_mult = gr.Slider(0.1, 1.0, value=0.4, step=0.1, label="Distance Multiplier")
icp_iter = gr.Slider(10, 100, value=50, step=5, label="Max Iterations")
with gr.Column(scale=1):
gr.Markdown("### Feature Parameters")
normal_radius_mult = gr.Slider(1.0, 5.0, value=2.0, step=0.5, label="Normal Radius Mult")
fpfh_radius_mult = gr.Slider(2.0, 10.0, value=5.0, step=0.5, label="FPFH Radius Mult")
run_button = gr.Button("Run Registration", variant="primary", size="lg")
with gr.Row():
status = gr.Textbox(label="Status", lines=2, interactive=False)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Before Registration")
before_viewer = gr.Plot(label="Before Registration")
with gr.Column(scale=1):
gr.Markdown("### After Registration")
after_viewer = gr.Plot(label="After Registration")
download_aligned = gr.File(
label="Download Aligned Source",
type="filepath",
)
with gr.Row():
with gr.Column(scale=1):
metrics = gr.Textbox(label="Metrics", lines=3, interactive=False)
with gr.Column(scale=1):
transform_matrix = gr.Textbox(label="Transformation Matrix", lines=5, interactive=False)
# Event handlers
run_button.click(
fn=demo.register,
inputs=[
pair_selection, mode, voxel_size,
normal_radius_mult, fpfh_radius_mult,
ransac_dist_mult, ransac_iter,
icp_dist_mult, icp_iter,
source_upload, target_upload
],
outputs=[before_viewer, after_viewer, status, metrics, transform_matrix, download_aligned]
)
return app
if __name__ == "__main__":
app = create_gradio_interface()
app.launch(
share=False,
server_name="0.0.0.0",
server_port=7860,
ssr_mode=False,
)
|