-
Notifications
You must be signed in to change notification settings - Fork 17
/
ground_seg.py
375 lines (320 loc) · 11.8 KB
/
ground_seg.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
######################################
#######realsense plotting#############
######################################
'''
Working with
UBUNTU 16.04 LTS
OPENCV 4.0~
Python 3.6
pyrealsense2
matplotlib
numpy
for intel D435i
font
'''
import pyrealsense2 as rs
import numpy as np
import cv2
import matplotlib.pyplot as plt
import math
import time
import banner
from sensor_msgs.msg import Image
import rospy
from easygo import easyGo
from std_msgs.msg import String
from cv_bridge import CvBridge, CvBridgeError
import threading
from time import sleep
global depth_scale, ROW, COL
global currentStatus
#size of images
COL= 480
ROW = 640
#ROBOT MOVE
SPEED = 0.4
ROTATE_SPEED = 35
VERTICAL_CORRECTION = 0.15 #0.45 #parameter of correction for parabola to linear
WARP_PARAM = 0.45 #value should be 0.0 ~ 1.0. Bigger get more warped. 0.45
GRN_ROI = 322 #The index of col that want to consider as ground ROI 400 300
ZOOM_PARAM = 0.15 #Force calibrating the depth image to match the color image 0.15 0.205
UNAVAILABLE_THRES = 400 #The index of col that is threshold of unavailable virtual lane
ROBOT_WIDTH_LIST = [2,3,4,5]
ROBOT_LEFT = 1
ROBOT_RIGHT = 6
currentStatus = ""
font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
fontScale = 1.5
yellow = (0, 255, 255)
handle_easy = True
depth_image_raw = 0
color_image_raw = 0
#Topview image. src, dst are numpy array.
#########################Move LAVACON TO EACH EDGES AND TRY AGAING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def Topview(src):
global WARP_PARAM, ROW, COL
# col=720, row=1280
col, row = src.shape[0], src.shape[1]
corners = np.float32([[row*WARP_PARAM/2, 0], [row*(1-WARP_PARAM/2), 0], [0, col], [row, col]])
warp_corners = np.float32([[0, 0], [ROW, 0], [0, COL], [ROW, COL]])
trans_matrix = cv2.getPerspectiveTransform(corners, warp_corners)
dst = cv2.warpPerspective(src, trans_matrix, (ROW, COL))
return dst
#vertically scan ground
def verticalGround(depth_image2, images, numCol, plot):
global depth_scale, GRN_ROI, ROW, COL
###################################################################################
#############Calibration. CHECK HERE WHEN YOU USE DIFFERENT CAMERA!!###############
###################################################################################
numLine=numCol
#Force correction. Depth and color pixels don't match.
numCol=(int)((numCol-150)*(-0.15)+numCol)
# get [i,640] column
_640col = [a[numCol] for a in depth_image2]
abs_x = []
abs_y = []
ground_center_idx = []
# depth_image2[360] = depth_image2[360] * float(depth_scale)
for idx, temp in enumerate(_640col):
if _640col[idx] == 0:
abs_x.append(None)
abs_y.append(None)
else:
#true_idx is a calibrated idx. Because the start point is not zero. Start point of ROI is GRN_ROI.
true_idx = GRN_ROI + idx*(COL-GRN_ROI)/COL
#Correction for parabola to linear. In other words, correcting lens distortion using 2nd-order function.
_640col[idx] = temp * depth_scale * (abs(true_idx -COL/2)**2/360**2*VERTICAL_CORRECTION + 1)
#58.0 is vertical FOV of the depth IR sensor. abs_x and abs_y are absolute coordinate of one column of depth image.
abs_x.append(
_640col[idx] * math.cos(
((float)(58.0 / 2.0 - 58.0 * (float)(true_idx) / COL)) * 3.14 / 180.0))
abs_y.append(
_640col[idx] * math.sin((float)(58.0 / 2.0 - 58.0 * (float)(true_idx) / COL) * 3.14 / 180.0))
idx = 20 #temporary set the point, that we want to consider it would be almost ground.
try:
while abs_x[COL - idx] == None:
idx += 1
ground_center_idx.append(COL - idx) #ground_center_idx contains all the indexes of ground.
except:
print("TOO CLOSE!!!!!!!!!!!!!")
ground_center_idx.append(COL - 30)
i = 0
groundCount = 0 #Count points that considered as ground
hurdleCount = 0 #Count points that not considered as ground subsequently. Initialize it to zero when found ground.
while idx < COL:
if abs_x[COL - idx] == None or abs_y[COL - idx] == None:
idx += 1
#print(idx)
continue
#To found ground indexes, we use differential. If variation dy/dx is lower than threshold, append it.
####################################################################################################
#######19/04/26 : I have updated the way of checking gradient. Now I use two gradients##############
#######from original, and from the current ground pixel. It works better ###########################
####################################################################################################
gradient_from_original = (abs_y[(COL - idx)] - abs_y[ground_center_idx[0]]) / (abs_x[(COL - idx)] - abs_x[ground_center_idx[0]])
gradient_from_current = (abs_y[(COL - idx)] - abs_y[ground_center_idx[i]]) / (abs_x[(COL - idx)] - abs_x[ground_center_idx[i]])
#print("#######")
#print("idx " + str(COL-idx))
#print("original" + str(gradient_from_original))
#print("current" + str(gradient_from_current))
#print("dist" + str(_640col[COL - idx]))
if abs(gradient_from_original + 0.33) < 0.2 and abs(gradient_from_current) < 0.6: #These number are carefully selected
ground_center_idx.append((COL - idx))
i += 1
cv2.circle(images, (numLine, (COL - idx)), 5, (0, 255, 0), 5)
groundCount += 1
hurdleCount = 0
idx += 5
elif hurdleCount > 3:
break
else:
hurdleCount += 1
idx += 10
if plot:
print(abs_x[ground_center_idx[0]], abs_y[ground_center_idx[0]])
print(abs_x[ground_center_idx[-1]], abs_y[ground_center_idx[-1]])
print((abs_x[ground_center_idx[-1]]-abs_x[ground_center_idx[0]])/(abs_y[ground_center_idx[-1]]-abs_y[ground_center_idx[0]]))
try:
plt.plot(abs_x, abs_y)
plt.scatter(abs_x[ground_center_idx[0]], abs_y[ground_center_idx[0]], color='r',
s=20) # red point on start point of ground
plt.scatter(abs_x[ground_center_idx[-1]], abs_y[ground_center_idx[-1]], color='r', s=20)
plt.xlim(0, 2) #5
plt.ylim(-2, 2)
plt.pause(0.05)
plt.cla()
plt.clf()
except:
pass
if groundCount < 3:
dead_end = COL
cv2.line(images, (numLine, 0), (numLine, ROW), (0, 0, 255), 5) #Draw a red line when ground indexes is less than we want.
else:
dead_end = ground_center_idx[-1]
cv2.line(images, (numLine, ground_center_idx[-1]), (numLine, COL), (0, 255, 0), 5) #Draw a green line.
try:
#pass
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)5
cv2.circle(images, (numLine, ground_center_idx[0]), 5, (255, 255, 255), 10)
cv2.putText(images, str(round(abs_x[ground_center_idx[0]],2)) + "m", (numLine, COL - 100), font, fontScale, yellow, 2)
except:
pass
return images, dead_end
def preGroundSeg(depth_image, color_image):
global ROW, COL, GRN_ROI
# Force calibrating the depth image to match the color image. Interpolation is really important. DO NOT USE INTER_LINEAR. IT MAKES NOISES!!!!
depth_image = cv2.resize(depth_image[(int)(COL * ZOOM_PARAM):(int)(COL * (1 - ZOOM_PARAM)), (int)(ROW * ZOOM_PARAM):(int)(ROW * (1 - ZOOM_PARAM))],
dsize=(ROW, COL), interpolation=cv2.INTER_NEAREST)
# ROI image
#depth_image = depth_image[GRN_ROI:COL, 0:ROW]
#color_image = color_image[GRN_ROI:COL, 0:ROW]
# Topview image
depth_image2 = Topview(depth_image)
color_image2 = Topview(color_image)
return depth_image, color_image
def GroundSeg(depth_image, color_image, stride=80):
global ROW
virtual_lane_available = []
for i in range(stride, ROW, stride):
if i == ROW/2:
temp_image, dead_end = verticalGround(depth_image, color_image, i, plot=False)
else:
temp_image, dead_end = verticalGround(depth_image, color_image, i, plot=False)
virtual_lane_available.append(dead_end)
return temp_image, virtual_lane_available
def bool_straight(virtual_lane_available, unavailable_thres):
global ROBOT_WIDTH_LIST
for i in ROBOT_WIDTH_LIST:
# > means unavailable path
if virtual_lane_available[i] > unavailable_thres:
return False
return True
def LaneHandling(virtual_lane_available, unavailable_thres, n):
center = int(len(virtual_lane_available)/2)
#If center lane is blocked.
if virtual_lane_available[center] > unavailable_thres:
#two lanes are both unavailable
if n > center:
print("GO BACK")
return 0
if virtual_lane_available[center-n] > unavailable_thres and virtual_lane_available[center+n] > unavailable_thres:
n+=1
if n > center:
print("GO BACK")
return 0
else:
return LaneHandling(virtual_lane_available, unavailable_thres, n)
elif virtual_lane_available[center-n] > unavailable_thres:
print("TURN RIGHT")
return 3
elif virtual_lane_available[center+n] > unavailable_thres:
print("TURN LEFT")
return 2
else:
n += 1
return LaneHandling(virtual_lane_available, unavailable_thres, n)
#Checking where is future obstable and avoid it.
else:
if n > center:
print("GO STRAIGHT")
return 1
if virtual_lane_available[center-n] > unavailable_thres:
print("TURN RIGHT")
return 3
elif virtual_lane_available[center+n] > unavailable_thres:
print("TURN LEFT")
return 2
else:
n+=1
if n > center:
print("GO STRAIGHT")
return 1
else:
return LaneHandling(virtual_lane_available, unavailable_thres, n)
def GoEasy(direc):
if direc == 0:
easyGo.mvCurve(-SPEED, 0)
elif direc == 1:
#print("COME HERE")
easyGo.mvCurve(SPEED, 0)
elif direc == 2:
#print("COME HERE2")
easyGo.mvRotate(ROTATE_SPEED, -1, False)
elif direc == 3:
easyGo.mvRotate(ROTATE_SPEED, -1, True)
def depth_callback(data):
try:
global depth_image_raw
depth_image_raw = bridge.imgmsg_to_cv2(data, "32FC1")
except CvBridgeError as e:
#raise(e)
print(e)
def image_callback(data):
try:
global color_image_raw
color_image_raw = bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print(e)
def listener():
#rospy.init_node('node_name')
bridge = CvBridge()
rospy.Subscriber("/camera/depth/image_rect_raw", Image, depth_callback)
rospy.Subscriber("/camera/color/image_raw", Image, image_callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
direc = 0
def main():
# Configure depth and color streams
global depth_scale, ROW, COL, GRN_ROI, bridge, direc
fpsFlag = False
numFrame = 0
fps = 0.0
bridge = CvBridge()
realsense_listener = threading.Thread(target=listener)
realsense_listener.start()
print("FUCK!!")
while 1:
try:
'''
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, ROW, COL, rs.format.z16, 30)
config.enable_stream(rs.stream.color, ROW, COL, rs.format.bgr8, 30)
'''
break
except:
pass
#COL=720, ROW=1280
depth_scale = 0.0010000000474974513
startTime = time.time()
while True:
# first step
global depth_image_raw, color_image_raw, currentStatus, handle_easy
if type(depth_image_raw) == type(0) or type(color_image_raw) == type(0):
sleep(0.1)
continue
print('MORP CYCLE : ',abcd)
depth_image, color_image = preGroundSeg(depth_image_raw, color_image_raw)
# last step
color_image, virtual_lane_available = GroundSeg(depth_image, color_image)
# handling lane
cv2.line(color_image, (0, UNAVAILABLE_THRES), (ROW, UNAVAILABLE_THRES), (0, 255, 0), 2)
direc = LaneHandling(virtual_lane_available, UNAVAILABLE_THRES, 1)
if direc == 1:
currentStatus = "NO OBSTACLE"
rospy.set_param('/point_init', True)
else:
currentStatus = "YES OBSTACLE"
print(direc)
if handle_easy:
easyGo.stopper=handle_easy
GoEasy(direc)
print('Morp easyGo stoppper :: ' + str(easyGo.stopper))
#LaneHandling(virtual_lane_available, UNAVAILABLE_THRES, 1)
# FPS
numFrame += 1
# Stop streaming
easyGo.stop()
if __name__ == "__main__":
rospy.init_node('robot_mvs', anonymous=False)
main()