-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathalignment_rigid_3D.py
61 lines (43 loc) · 1.67 KB
/
alignment_rigid_3D.py
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
from functools import partial
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pycpd import rigid_registration
import numpy as np
import time, sys
max_iterations = 40
def saveNormalizedTxt(Y):
(rows, columns) = Y.shape
txt = open("output.txt", "w+")
for i in range(rows):
line = ""
for j in range(columns):
line += (str(Y[i][j]) + " ")
txt.write(line + '\n')
def visualize(iteration, error, X, Y, ax):
plt.cla()
ax.scatter(X[:, 0], X[:, 1], X[:, 2], color='red', label='Target')
ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], color='blue', label='Source')
ax.text2D(0.87, 0.92, 'Iteration: {:d}\nError: {:06.4f}'.format(iteration, error), horizontalalignment='center',
verticalalignment='center', transform=ax.transAxes, fontsize='x-large')
ax.legend(loc='upper left', fontsize='x-large')
saveNormalizedTxt(Y)
plt.draw()
plt.pause(0.001)
def saveData(iteration, error, X, Y):
saveNormalizedTxt(Y)
def alignment_rigid(target, source, visualization=False):
start = time.time()
X = np.loadtxt(target)
Y = np.loadtxt(source) #synthetic data, equaivalent to X + 1
if visualization is True:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
callback = partial(visualize, ax=ax)
reg = rigid_registration(**{'X': X, 'Y': Y}, max_iterations=max_iterations)
reg.register(callback)
plt.show()
else:
callback = partial(saveData)
reg = rigid_registration(**{'X': X, 'Y': Y}, max_iterations=max_iterations)
reg.register(callback)
print('Elapsed time: ' + str(time.time() - start))