-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detec_Segm_0.py
294 lines (229 loc) · 10.3 KB
/
Detec_Segm_0.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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 14 15:32:16 2019
@author: alien
"""
import numpy as np
from skimage.measure import label
from skimage.transform import resize
import skimage.morphology as skmorpho
from scipy import ndimage as ndi
import scipy.ndimage.morphology as morpho
# Import functions to read and write ply files
from utils.ply import write_ply, read_ply
import time
from alienlab import *
import segmentation_func
from segmentation_func import *
from progressbar import ProgressBar
#%%
if __name__ == '__main__':
# Load point cloud
# ****************
# Path of the file
#file_path = 'Cassette_idclass/Cassette_GT.ply'
#file_path = '../Z5-9/Z5.ply'
file_path = '../rueMadame_database/GT_Madame1_2.ply'
# Load point cloud
data = read_ply(file_path)
# Concatenate data
points = np.vstack((data['x'], data['y'], data['z'])).T
# Removal of exterme points
pts_ind = np.sqrt((points[:,0]-np.mean(points[:,0]))**2)<3*np.std(points[:,0])
points = points[pts_ind]
if True:
'''Elevation projection'''
print('Computing elevation images')
kx = 0.1
ky = 0.1
t0 = time.time()
#Get the elevation image as well as the tables allowing to reverse the projection
#For future reconstruction
elevation_image, reverse_projection = get_elevation(points, kx, ky)
t1 = time.time()
print('Elevation images computed in {:.3f} seconds'.format(t1 - t0))
write_ply('elevation_image.ply', [elevation_image],
['x', 'y', 'max_elevation', 'min_elevation', 'relative_elevation', 'accumulation'])
#%%
if True:
'''Observation of the elevation images'''
#Turn the elevation arrays into images
im_max, elevation_mask = make_image(elevation_image, elevation_image, im_type = 0)
im_min, msk = make_image(elevation_image, elevation_image, im_type = 1)
im_range, msk = make_image(elevation_image, elevation_image, im_type = 2)
im_accum, msk = make_image(elevation_image, elevation_image, im_type = 3)
#Fill holes of the max elevation image
im = np.copy(im_max)
im[1:-1, 1:-1] = im_max.max()
mask = im_max
filled_max = skmorpho.reconstruction(im, mask, method='erosion')
#Fill holes of the min elevation image
im = np.copy(im_min)
im[1:-1, 1:-1] = im_min.max()
mask = im_min
filled_min = skmorpho.reconstruction(im, mask, method='erosion')
g = showclass() #Class equivalent to matplotlib.pyplot (code in alienlab)
g.save_name = 'Elevation_images'
g.col_num = 4
g.title_list = ['Filled maximal elevation', 'Filled minimal elevation',
'Elevation range','Accumulation' ]
g.showing([filled_max.T, filled_min.T, im_range.T,im_accum.T])
#%%
if True:
'''Ground detection, method based on region growth in the image'''
print('Segmentation of the ground')
#Region growth radius of exploration
r = 40
#Region growth selection parameter
C1 = 0.5
#New seeds are chosen as the new elements
#added to the region at the edge of the radius of exploration
g = showclass()
#compute ground from maximal elevation image
ground_max = lambda_flat2(filled_max, r, C1)
no_ground_max = elevation_mask * ~ground_max
g.cmap = 'inferno'
g.col_num = 1
g.save_name = 'Ground_max_images'
g.title_list = (['original', 'ground', 'not ground'])
g.showing([filled_max, ground_max, no_ground_max])
#compute ground from minimal elevation image
ground_min = lambda_flat2(filled_min, r, C1)
no_ground_min = elevation_mask * ~ground_min
g.save_name = 'Ground_min_images'
g.showing([filled_min, ground_min, no_ground_min])
#%%
if True:
'''Selection of the object candidates'''
#OBJECTS ON THE SEGMENTED GROUND
ground = (ground_min + ground_max).astype(int)
im_ground = ground*filled_max #To find the objects that lay on the segmented ground
#To perform the search for peaks we select the surroudings of the actual image for the
#initiation of the search by applying a binary erosion on the ground
#and keep only the part that has been eroded
surround = morpho.binary_erosion(ground, iterations = 1) - ground
surround = surround.astype(bool)
seed = np.copy(im_ground)
seed[~surround] = np.min(im_ground) #All the rest of the image is set to the image minimum
mask = im_ground
cut_ground = skmorpho.reconstruction(seed, mask, method='dilation')
#Collect the peaks that have been cut-off
obj2 = im_ground-cut_ground
obj2= make_binary(obj2, 0.1) #Locate the peaks
#g.col_num = 1
#g.title_list = ['Seed', 'Detected peaks']
#g.showing([seed,obj2])
#OBJECTS NOT NECESSARILY ON THE SEGMENTED GROUND
obj1 = no_ground_max*im_max
obj1 = make_binary(obj1, 1)
#ALL OBJECT CANDIDATES
obj = obj1.astype(bool) + obj2.astype(bool)
#Remove the artifacts by opening
obj_clean = morpho.binary_opening(obj)
residue = obj & ~obj_clean
residue = residue
#Reinject residues with a high accumulation value: they are not artifacts
#But their small diameter seen from the top had them removed by the opening
#High accumulation value show they are not artifacts but rather thin vertical objects
#Like street posts.
im_accum_bin = make_binary(im_accum, 6, dtyp = 'bool')
clean = im_accum_bin & residue.astype(bool)
obj_restored = obj_clean + clean
g.col_num = 2
g.title_list = ['Ground', 'No ground (object candidates)',
'Fill ground holes (object candidates)',
'All object candidates (union)', 'opening result',
'opening corrected with accumulation']
g.save_name = 'Object_detection'
plot = g.showing([im_ground, obj1, obj2, obj, residue, obj_restored])
#%%
if True:
'''Watershed segmentation of the objects'''
#Selection of local maxima
obj_height = obj_restored * im_max
marks = skmorpho.h_maxima(obj_height,1, selem=None)
#Labelling as markers
marks = ndi.label(marks)[0]
#watershed segmentation
label_obj = skmorpho.watershed(-obj_height, marks)
#Coarser segmentation
#reference elevation image
ref = im_accum * im_min * obj_restored
#removal of small objects already segmented
ref = morpho.grey_opening(ref, size = 1)
#Downsizing factor
DS_factor = 10
#Resize image
image_resized = resize(ref, (ref.shape[0] // DS_factor, ref.shape[1] // DS_factor))
#New local maxima
marks2 = skmorpho.h_maxima(image_resized,10, selem=None)
marks2 = ndi.label(marks2)[0]# + np.max(obj_labels) to avoid giving the same labels,
#but removed here for visualisation
#Locate the local maxima
(indx, indy) = np.where(marks2 != 0)
marks_resized = obj_height * 0
#Assign local maxima in an image with original size
marks_resized[DS_factor*indx, DS_factor*indy] = marks2[indx, indy]
#Dilate the maxima to ease the watershed
marks_resized = morpho.grey_dilation(marks_resized, size = 2)
#Watershed segmentation on the new maxima
label_obj_mark = skmorpho.watershed(-obj_height, marks_resized, mask = ref,connectivity=1)
#selection of only the new labels
ind = label_obj_mark != 0
#update the labels in the original labelled image
label_obj[ind] = label_obj_mark[ind]
label_obj = label_obj*obj_restored
g.cmap = 'flag'
g.save_name = 'Object_segmentation'
g.title_list = ['Refined segmentation']
g.showing(label_obj)
#%%
if True:
'''Projection back to 3D'''
mount_cloud_labels = image_to_2Dcloud(label_obj, elevation_mask)
im_ground = make_binary(im_ground, 0, 'int')
ground = image_to_2Dcloud(im_ground, elevation_mask)
obj_labels = conv_2D_3D(mount_cloud_labels, ground, reverse_projection, points)
write_ply('object_labels.ply',
[points, obj_labels],
['x', 'y', 'z', 'segm'])
#%%
if True:
'''Evaluation'''
bar = ProgressBar()
#Class-wise segmentation evaluation
pts = np.vstack((data['x'], data['y'], data['z'], data['id'], data['class'])).T
pts = pts[pts_ind]
b = np.unique(pts[:,4])
correct_tot = 0
pred = []
for j in bar(range(len(b))):
ind_class = pts[:,4]==b[j]
pts2 = pts[ind_class]
obj_labels2 = obj_labels[ind_class]
a = np.unique(pts2[:,3])
correct = 0
for i in range(len(a)):
L = a[i]
ind_L = pts2[:,3] == L
segment_L = obj_labels2[ind_L]
set_L, count_L = np.unique(segment_L, return_counts = True)
ID = set_L[np.argmax(count_L)]
correct += np.count_nonzero(segment_L==ID)
correct_tot += np.count_nonzero(segment_L==ID)
pred.append(correct/pts2.shape[0]*100)
p1 = correct_tot/pts.shape[0]*100
#Undersegmentation evaluation
a = np.unique(obj_labels)
correct = 0
for i in range(len(a)):
L = a[i]
ind_L = obj_labels == L
segment_L = pts[ind_L]
set_L, count_L = np.unique(segment_L, return_counts = True)
ID = set_L[np.argmax(count_L)]
correct += np.count_nonzero(segment_L==ID)
p2 = correct/pts.shape[0]*100
print('Segmentation results per class (p1)',pred)
print('Oversegmentation index (p1)', p1)
print('Undersegmentation index (p2)',p2)