-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_icp_pt2pl_err.py
132 lines (117 loc) · 5.83 KB
/
get_icp_pt2pl_err.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
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
import open3d as o3d
import numpy as np
import time
import copy
from sklearn.neighbors import KDTree
import matplotlib.pyplot as plt
def file2matrix(filename):
fr = open(filename)
numberOfLines = len(fr.readlines()) #get the number of lines in the file
trans = np.eye(4) #prepare matrix to return
truth = [] #prepare labels return
fr = open(filename)
index = 0
for line in fr.readlines():
line = line.strip()
# listFromLine = line.split('\t')
listFromLine = line.split()
listFromLine = [float(x) for x in listFromLine]
if(index % 5 ==0):
index = 0
elif(index % 5 ==1):
trans[0, :] = np.array(listFromLine)
elif(index % 5 ==2):
trans[1,:] = np.array(listFromLine)
elif(index % 5 ==3):
trans[2,:] = np.array(listFromLine)
elif(index % 5 ==4):
trans[3,:] = np.array(listFromLine)
truth.append(trans.copy())#这里不用copy的话,,,每个元素都是一样的
index += 1
return truth
if __name__ == '__main__':
root_path = '/Bill/DataSet/RedWood/'
dataset_names = ['loft', 'lobby', 'apartment','bedroom','boardroom']
root_save_path = '/icp_pt2pl/src2ref'
dataset_numbers = [252,199,319,219,243]
# for i in range(len(dataset_names)):
for i in range(1):
file_path = root_path + dataset_names[i]
end = dataset_numbers[i]
save_path = dataset_names[i] + root_save_path
print(file_path)
groud_truth = file2matrix(file_path + '/reg_output.log')
voxel_size = 0.05 # means 5cm for this dataset
err_R = []
err_T = []
trans_all = []
fail_list = []
start = 0
# end = 251
for j in range(start, end):
print(
'j',j
)
# index_src = j + 1
# index_ref = j
index_src = j
index_ref = j + 1
current_transformation = np.identity(4)
source_show = o3d.io.read_point_cloud(file_path + "/mesh_%s.ply"%(index_src))
target_show = o3d.io.read_point_cloud(file_path + "/mesh_%s.ply"%(index_ref))
source = source_show.voxel_down_sample(voxel_size)
target = target_show.voxel_down_sample(voxel_size)
source.estimate_normals(
o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=30))
target.estimate_normals(
o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=30))
result_icp = o3d.pipelines.registration.registration_icp(
source, target, 0.2, current_transformation, # 后边这个距离阈值,就是大于这个距离的就去掉不计算
o3d.pipelines.registration.TransformationEstimationPointToPlane())
print(result_icp.transformation)
total_trans = result_icp.transformation
R = total_trans[:3,:3].reshape(3,3)
t = total_trans[:3,3].reshape(-1,1)
if index_src > index_ref:
err_R.append(np.arccos((np.trace(R.T @ groud_truth[j][:3,:3]) - 1) / 2) * 180 / np.pi )
err_T.append(np.linalg.norm(t - groud_truth[j][:3,3].reshape(-1,1), ord=2,axis=0))
trans_all.append((total_trans))
else:
err_R.append( np.arccos( (np.trace(R @ groud_truth[j][:3,:3] ) - 1) / 2) * 180 / np.pi )
err_T.append(np.linalg.norm(-R.T @ t - groud_truth[j][:3,3].reshape(-1,1), ord=2,axis=0))
trans_all.append((total_trans))
# print(total_trans[:3,:3] @ groud_truth[j][:3,:3], np.trace(total_trans[:3,:3] @ groud_truth[j][:3,:3] - np.eye(3)))
# print(total_trans, groud_truth[j])
print('err_R err_T', err_R[j - start], err_T[j - start],total_trans)
if index_src > index_ref:
#
# location = str(start) + '_' + str(end)
err_all = [err_R, err_T]
plt.figure("ERR_R ref2src") # 图像窗口名称
plt.plot(err_R)
plt.savefig(save_path + '/%s_%s_err_All_ref2src.jpg'%(start, end))
plt.show()
plt.figure("ERR_T ref2src") # 图像窗口名称
plt.plot(err_T)
plt.savefig(save_path + '/%s_%s_trans_all_ref2src.jpg' % (start, end))
plt.show()
np.savetxt(save_path + '/%s_%s_fail_list_ref2src.txt'%(start, end), fail_list)
np.save(save_path + '/%s_%s_err_All_ref2src.npy'%(start, end), err_all)
np.savetxt(save_path + '/%s_%s_err_All_ref2src.txt' % (start, end), err_all)
np.save(save_path + '/%s_%s_trans_all_ref2src.npy'%(start, end), trans_all)
np.savetxt(save_path + '/%s_%s_trans_all_ref2src.txt'%(start, end), np.array(trans_all).reshape(-1,4),fmt='%0.8f')
else:
err_all = [err_R, err_T]
plt.figure("ERR_R src2ref") # 图像窗口名称
plt.plot(err_R)
plt.savefig(save_path + '/%s_%serr_All_src2ref.jpg'%(start, end))
plt.show()
plt.figure("ERR_T src2ref") # 图像窗口名称
plt.plot(err_T)
plt.savefig(save_path + '/%s_%strans_all_src2ref.jpg' % (start, end))
plt.show()
np.savetxt(save_path + '/%s_%s_fail_list_src2ref.txt'%(start, end), fail_list)
np.savetxt(save_path + '/%s_%serr_All_src2ref.txt' % (start, end), err_all)
np.save(save_path + '/%s_%serr_All_src2ref.npy'%(start, end), err_all)
np.save(save_path + '/%s_%strans_all_src2ref.npy'%(start, end), trans_all)
np.savetxt(save_path + '/%s_%strans_all_src2ref.txt'%(start, end), np.array(trans_all).reshape(-1,4),fmt='%0.8f')