Repository navigation
Expand file tree
/
Copy pathtest_icp.py
More file actions
100 lines (82 loc) · 2.93 KB
/
Copy pathtest_icp.py
File metadata and controls
100 lines (82 loc) · 2.93 KB
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
# Align two point clouds with the ICP algorithm
# Import the geometry processing library
import geomproc
# Import numpy for data arrays
import numpy as np
# Math functions
import math
# Default setting: 1000 points and 50 iterations
#
# Things to try:
# - A Completely random transformation
# - A different set of samples for pc2 (call tm.sample() again)
# - Estimate normals instead of using mesh normals
# - More points and iterations
#### Create a point sample for the test
# Load and normalize the mesh
tm = geomproc.load('meshes/bunny.obj')
tm.normalize()
# Sample a set of points from the surface of the mesh
n = 1000
pc1 = tm.sample(n)
# Apply a transformation to pc1 to create pc2
# Define the transformation
if False: # Choose the transformation by switching True/False
# Choose a completely random transformation
rnd = np.random.random((3, 3))
[q, r] = np.linalg.qr(rnd)
orig_rot = q
orig_trans = np.random.random((3, 1))
else:
# Choose a specific rotation around the y axis
angle = math.pi/3
orig_rot = np.array([[math.cos(angle), 0, math.sin(angle)],
[0, 1, 0],
[-math.sin(angle), 0, math.cos(angle)]])
orig_trans = np.zeros((3, 1))
# Apply the transformation to pc1 to create the second point cloud pc2
pc2 = pc1.copy()
# For a more realistic example, pc2 could be another set of samples
# pc2 = tm.sample(n)
pc2.point = geomproc.apply_transformation(pc2.point, orig_rot, orig_trans)
# Save input data
tm.save('output/bunny_normalized.obj')
pc1.save('output/bunny_sample1.obj')
pc2.save('output/bunny_sample2.obj')
#### Align the point clouds
# Run the ICP method
error_threshold = 0.01
num_iters = 50
[rot, trans, pc1tr, err, iter_count, corr] = geomproc.icp(pc1, pc2, error_threshold, num_iters)
# Save registration
pc1tr.save('output/bunny_sample1aligned.obj')
# Save final correspondence so that we can see it
if True: # Turn on/off with True/False
# Create lines to indicate the correspondences
line = np.zeros((pc1.point.shape[0], 6))
for i in range(corr.shape[0]):
line[i, :] = np.concatenate((pc1tr.point[corr[i, 0], :], pc2.point[corr[i, 1], :]))
cl = geomproc.create_lines(line, color=[0.7, 0.7, 0.7])
# Create points for the point sets
pt1 = geomproc.create_points(pc1tr.point, color=[1, 0, 0])
pt2 = geomproc.create_points(pc1.point, color=[0, 1, 0])
pt3 = geomproc.create_points(pc2.point, color=[0, 0, 1])
# Combine everything together
result = geomproc.mesh()
result.append(cl)
result.append(pt1)
result.append(pt2)
result.append(pt3)
# Save the mesh
wo = geomproc.write_options()
wo.write_vertex_colors = True
result.save('output/bunny_corr.obj', wo)
# Print some information
print('Original transformation = ')
print(orig_rot)
print(orig_trans)
print('Alignment result = ')
print(rot)
print(trans)
print('Number of iterations used = ', iter_count)
print('Error after alignment = ', err)