-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortCars.py
191 lines (143 loc) · 5.4 KB
/
SortCars.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
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
import numpy as np
import open3d as o3d
import math
from sklearn.linear_model import LinearRegression
import lineSegmentation as seg
############################## Macro ###############################
pi = 3.141592653589793238
######################### Define Function ##########################
def get_angle(input_list):
angle = math.atan2(input_list[1], input_list[0])
return angle
def get_distance(xy1,xy2):
distance = ((xy1[0]-xy2[0])**2 + (xy1[1]-xy2[1])**2)**0.5
return distance
def sort_Car(clusterCloud, z_max, z_min):
# Get Centroid
x_sum = clusterCloud[:,0].sum()
y_sum = clusterCloud[:,1].sum()
num_points = len(clusterCloud)
x_inner = x_sum / num_points
y_inner = y_sum / num_points
inner_point = [x_inner, y_inner]
if (len(inner_point)==0) : return 0,0
# Convert Numpy to Pointcloud
clusterCloud_pcd = o3d.geometry.PointCloud()
clusterCloud_pcd.points = o3d.utility.Vector3dVector(clusterCloud)
convexhull = clusterCloud[(clusterCloud_pcd.compute_convex_hull()[1])[:],:]
clusterCloud_2D = convexhull[:,0:2]
#points_x = clusterCloud_2D[:,0]
#points_y = clusterCloud_2D[:,1]
# Line Segmentation to extract two lines
tmp1 = seg.RansacLine(clusterCloud_2D, 140, 0.1)
if(tmp1 is not None):
inliers1_list, outliers1_list = tmp1
else:
return None, None
if(len(inliers1_list)==0 or len(outliers1_list)==0):
return None, None
line1_inliers = clusterCloud_2D[inliers1_list[:], :]
line1_outliers = clusterCloud_2D[outliers1_list[:], :]
if(len(line1_outliers)==0):
return None, None
tmp = seg.RansacLine(line1_outliers, 70, 0.2)
if(tmp is not None):
inliers2_list, _ = tmp
else:
return None, None
line2_inliers = line1_outliers[inliers2_list[:],:]
#######################################Linear Regression ###################
line_fitter1 = LinearRegression()
line_fitter2 = LinearRegression()
len1 = len(line1_inliers[:][:,0])
len2 = len(line2_inliers[:][:,0])
xline1 = line1_inliers[:][:,0].reshape(len1,1)
yline1 = line1_inliers[:][:,1].reshape(len1,1)
xline2 = line2_inliers[:][:,0].reshape(len2,1)
yline2 = line2_inliers[:][:,1].reshape(len2,1)
line1_fit = line_fitter1.fit(xline1,yline1)
line2_fit = line_fitter2.fit(xline2,yline2)
line1dy = line1_fit.coef_
#line1pred = line1_fit.predict(xline1).reshape([len1,1])
line2dy = line2_fit.coef_
#line2pred = line2_fit.predict(xline2).reshape([len2,1])
line1dict = {}
line2dict = {}
line1vectors = line1_inliers - inner_point
line2vectors = line2_inliers - inner_point
list1angle = list(map(get_angle, line1vectors))
list2angle = list(map(get_angle, line2vectors))
for i in range(0,len1):
line1dict[line1_inliers[i][0]] = line1_inliers[i][:]
for i in range(0,len2):
line2dict[line2_inliers[i][0]] = line2_inliers[i][:]
line1dict_sorted = sorted(line1dict.items())
line2dict_sorted = sorted(line2dict.items())
list1angle = sorted(list1angle)
list2angle = sorted(list2angle)
line1_sorted = np.empty([0,2])
line2_sorted = np.empty([0,2])
len1 = len(line1dict_sorted)
len2 = len(line2dict_sorted)
for j in range(0,len1):
line1_sorted = np.append(line1_sorted, [line1dict_sorted[j][1]],axis = 0)
for j in range(0, len2):
line2_sorted = np.append(line2_sorted, [line2dict_sorted[j][1]],axis = 0)
for i in range(0,len1-1):
theta = abs(list1angle[i]-list1angle[i+1])
if 180 < theta:
move = line1_sorted[:i+1]
line1_sorted = line1_sorted[i+1:]
line1_sorted = np.append(line1_sorted,move,axis = 0)
for i in range(0,len2-1):
theta = abs(list2angle[i]-list2angle[i+1])
if 180<theta:
move = line2_sorted[:i+1 ]
line2_sorted = line2_sorted[i+1 :]
line2_sorted = np.append(line2_sorted,move,axis = 0)
####################### Get result #########################
x1, y1 = line1_sorted[0][0], line1_sorted[0][1]
x2, y2 = line1_sorted[len1-1][0], line1_sorted[len1-1][1]
x3, y3 = line2_sorted[0][0], line2_sorted[0][1]
x4, y4 = line2_sorted[len2-1][0], line2_sorted[len2-1][1]
x1x3 = ((x1-x3)**2+(y1-y3)**2)**0.5
x2x3 = ((x2-x3)**2+(y2-y3)**2)**0.5
x1x4 = ((x1-x4)**2+(y1-y4)**2)**0.5
x2x4 = ((x2-x4)**2+(y2-y4)**2)**0.5
w = ((x3-x4)**2+(y3-y4)**2)**0.5
delx = x2-x1
dely = y2-y1
if(x2x3<x1x3):
if(x2x4<x2x3):
x4 = x3-delx
y4 = y3-dely
else:
x3 = x4-delx
y3 = y4-dely
else:
if(x1x4<x1x3):
x4 = x3+delx
y4 = y3+dely
else:
x3 = x4+delx
y3 = y4+dely
center = [(x1+x2+x3+x4)/4,(y1+y2+y3+y4)/4]
yaw = get_angle([1,line1dy])
l = (abs(x1-x2)**2+abs(y1-y2)**2)**0.5
h = z_max - z_min + 0.5
if(l<w):
temp = w
w = l
l = temp
yaw = get_angle([1,line2dy])
ang1 = get_angle([1, line1dy])*180/pi
ang2 = get_angle([1, line2dy])*180/pi
if(62<abs(ang1-ang2)<131.2):
if(w<2.8 and l<7.1):
return [center[0], center[1], yaw], [w, l,h]
else:
return None, None
else:
return None, None
if __name__ == "__main__":
print("Error.. Why sortCar Module execute")