-
Notifications
You must be signed in to change notification settings - Fork 1
/
depthFusion.py
338 lines (284 loc) · 14.2 KB
/
depthFusion.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
import os
import glob
import cv2
from PIL import Image, ImageChops
import numpy as np
from pathlib import Path
import copy
import open3d as o3d
from planeDetect import ransac_plane_detection, ransac_planefit
from unproject import init_colmap_pointcloud
###############################################
# Block Sample Method
###############################################
def blockSampleFusionByColor(estimate_block, o3d_block, debug = False):
if debug:
print(estimate_block, "\n", o3d_block)
assert estimate_block.size == o3d_block.size
print(o3d_block.size)
output_block = copy.deepcopy(o3d_block)
# get valid pixel in open3d block
valid_pix = np.where(o3d_block != 0)
# get the value of mapping valid pixel
omapping_pix = o3d_block[valid_pix]
emapping_pix = estimate_block[valid_pix]
list_same = list(set(emapping_pix))
# enum every estimate depth
for source_depth in list_same:
# caculate average of the same estimate depth
target_depth = np.average(omapping_pix[np.where(emapping_pix == source_depth)])
print(target_depth)
# mask = cv2.inRange(estimate_block, source_depth, source_depth)
# o3d_block[mask > 0] = target_depth
# set the inf value in open3d block to average according to estimate block
output_block = np.array([[ target_depth if o3d_block[col, row] == 0 and estimate_block[col, row] == source_depth else output_block[col, row] \
for row in range(o3d_block.shape[1])] for col in range(o3d_block.shape[0])])
if debug:
print("output block:\n", output_block)
# cv2.imshow('image',output_block)
# cv2.waitKey(0)
# print(output_block)
# input()
return output_block
def generateTrueDepthByBlockSample(data_dir, sizeof_sample_block = 32, valid_threshold = 0.3, debug = False):
"""fusion the original depth and estimate depth block by block
Args:
data_dir (str): inputs data path
sizeof_sample_block (int, optional): sample block size for nxn fetched from input image. Defaults to 32.
valid_threshold (float, optional): available block threshold, take the ratio of valid original depth. Defaults to 0.3.
debug (bool, optional): debug~. Defaults to False.
Returns:
fused true depth map - uint32 png
"""
estimate_depth_dir = data_dir + "relative_depth_predict/"
o3d_depth_dir = data_dir + "unproject_depth/"
edepth_path = sorted(glob.glob(estimate_depth_dir+"*.png"))
odepth_path = sorted(glob.glob(o3d_depth_dir+ "*.png"))
file_idx = 0
edepth_seq = Path(edepth_path[file_idx]).stem.split("_")[-1]
odepth_seq = Path(odepth_path[file_idx]).stem.split("_")[-1]
assert edepth_seq == odepth_seq
edepth = cv2.imread(edepth_path[file_idx], cv2.IMREAD_UNCHANGED) # , cv2.IMREAD_GRAYSCALE
edepth = cv2.bitwise_not(edepth)
odepth = cv2.imread(odepth_path[file_idx], cv2.IMREAD_UNCHANGED) # , cv2.IMREAD_GRAYSCALE
if debug:
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',edepth)
cv2.waitKey(0)
cv2.imshow('image',odepth)
cv2.waitKey(0)
print("estimate depth: \n", edepth,"\n\noriginal depth: \n", odepth)
print("\nestimate depth type: {}, min: {}, max: {}".format(edepth.dtype, np.min(edepth), np.max(edepth)))
print("origin depth type: {}, min: {}, max: {}".format(odepth.dtype, np.min(odepth), np.max(odepth)))
print("="*50+"\n\n")
# print(edepth,"\n", odepth)
assert edepth.shape == odepth.shape
out = copy.deepcopy(odepth)
block_counter = 0
valid_block_counter = 0
for pix_col in range(0, edepth.shape[0], sizeof_sample_block):
for pix_row in range(0, edepth.shape[1], sizeof_sample_block):
block_counter += 1
esample_block = edepth[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)]
osample_block = odepth[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)]
# print(esample_block, osample_block)
valid_ratio = 1 - np.count_nonzero(osample_block == 0) / sizeof_sample_block**2
if valid_ratio > valid_threshold:
valid_block_counter += 1
merged_sample_block = blockSampleFusionByColor(esample_block, osample_block, debug)
out[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)] = merged_sample_block
if debug:
cv2.imshow('image',out)
cv2.waitKey(0)
else:
out[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)] = osample_block
print("=> Total {} blocks, {} valid blocks, {:.3}% of Image Mended".format(
block_counter, valid_block_counter, valid_block_counter/block_counter*100))
out = Image.fromarray(out).convert("I").save("d.png")
###############################################
# Total merged Method
###############################################
def merge_image(back, front):
mask = np.where(front != 0)
back[mask] = front[mask]
return back
def blockSampleFusionByRatio(estimate_block, o3d_block, debug = False):
if debug:
print("e:\n", estimate_block, "\no:\n", o3d_block)
print("size:",o3d_block.size)
assert estimate_block.size == o3d_block.size
# get valid pixel in open3d block
valid_pix = np.where(o3d_block != 0)
# get the value of mapping valid pixel
omapping = o3d_block[valid_pix]
emapping = estimate_block[valid_pix]
divide = omapping / emapping
divide = divide[divide != np.inf]
if len(divide) == 0:
return o3d_block
e2o_ratio = np.average(divide)
output_block = (estimate_block * e2o_ratio).astype(np.uint16)
if debug:
print("estimate to original ratio: ", e2o_ratio)
print("estimate mapping pixel:\n", emapping, "\noriginal mapping pixel:\n", omapping)
print("estimate min: {}, max: {}".format(np.min(emapping), np.max(emapping)))
print("origin min: {}, max: {}".format(np.min(omapping), np.max(omapping)))
print("Out image:\n", output_block)
output_block = merge_image(output_block, o3d_block)
return output_block
def generateTrueDepthByTotalMerge(data_dir, sizeof_sample_block = 128, valid_threshold = 0.2, debug = False):
estimate_depth_dir = data_dir + "relative_depth_predict/"
o3d_depth_dir = data_dir + "unproject_depth/"
edepth_path = sorted(glob.glob(estimate_depth_dir+"*.png"))
odepth_path = sorted(glob.glob(o3d_depth_dir+ "*.png"))
file_idx = 35
print("=> Mending depth {}".format(odepth_path[file_idx]))
edepth_seq = Path(edepth_path[file_idx]).stem.split("_")[-1]
odepth_seq = Path(odepth_path[file_idx]).stem.split("_")[-1]
assert edepth_seq == odepth_seq
edepth = cv2.imread(edepth_path[file_idx], cv2.IMREAD_GRAYSCALE) # , cv2.IMREAD_GRAYSCALE
edepth = cv2.bitwise_not(edepth)
odepth = cv2.imread(odepth_path[file_idx], cv2.IMREAD_UNCHANGED) # , cv2.IMREAD_GRAYSCALE
if debug:
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',edepth)
cv2.waitKey(0)
cv2.imshow('image',odepth)
cv2.waitKey(0)
print("estimate depth: \n", edepth,"\n\noriginal depth: \n", odepth)
print("\nestimate depth type: {}, min: {}, max: {}".format(edepth.dtype, np.min(edepth), np.max(edepth)))
print("origin depth type: {}, min: {}, max: {}".format(odepth.dtype, np.min(odepth), np.max(odepth)))
print("="*50+"\n\n")
assert edepth.shape == odepth.shape
edepth = np.array(edepth, dtype = "uint16") * 255
valid_pix = np.where(odepth != 0)
omap = odepth[valid_pix]
emap = edepth[valid_pix]
odepthmap_range = [np.min(odepth), np.max(odepth)]
edepthmap_range = [np.min(edepth), np.max(edepth)]
edepth = ((edepth - edepthmap_range[0]) * ((odepthmap_range[1]-odepthmap_range[0])/(edepthmap_range[1]-edepthmap_range[0]))).astype(np.uint16)
if debug:
print(edepth)
cv2.imshow('image',edepth)
cv2.waitKey(0)
# print(odepth_range)
out = copy.deepcopy(odepth)
block_counter = 0
valid_block_counter = 0
for pix_col in range(0, edepth.shape[0], sizeof_sample_block):
for pix_row in range(0, edepth.shape[1], sizeof_sample_block):
block_counter += 1
esample_block = edepth[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)]
osample_block = odepth[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)]
# print(esample_block, osample_block)
valid_ratio = 1 - np.count_nonzero(osample_block == 0) / sizeof_sample_block**2
if valid_ratio > valid_threshold:
valid_block_counter += 1
merged_sample_block = blockSampleFusionByRatio(esample_block, osample_block, debug)
out[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)] = merged_sample_block
if debug:
cv2.imshow('image',out)
cv2.waitKey(0)
else:
out[pix_col:min(edepth.shape[0], pix_col+sizeof_sample_block), \
pix_row:min(edepth.shape[1], pix_row+sizeof_sample_block)] = osample_block
out = Image.fromarray(out).convert("I")
out.save("d.png")
###############################################
# Edge detection Method
###############################################
def getPointCloudFromRGBDImage(color, depth, intrinsic, debug):
color = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
color = o3d.geometry.Image(color)
depth = o3d.geometry.Image(depth)
rgbd = o3d.geometry.RGBDImage.create_from_color_and_depth(
color, depth, depth_trunc=1000, convert_rgb_to_intensity = False)
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
rgbd, intrinsic, np.identity(4))
pcd.voxel_down_sample(0.05)
pcd, idx = pcd.remove_radius_outlier(nb_points=16, radius=0.05)
pcd = pcd.select_by_index(idx)
box = pcd.get_oriented_bounding_box()
box.color = [1, 0, 0]
if debug:
o3d.visualization.draw_geometries([pcd, box])
return pcd
def generateTrueDepthByEdgeDetect(data_dir,
valid_point_threshold_ratio = 0.1,
ransac_n = 3, max_dst = 5, max_trials=1000, stop_inliers_ratio=1.0, debug = False):
# create/save/link/read dirs
estimate_depth_dir = data_dir + "relative_depth_predict/"
o3d_depth_dir = data_dir + "unproject_depth/"
image_dir = data_dir + "undistorted/images/"
edepth_path = sorted(glob.glob(estimate_depth_dir+"*.png"))
odepth_path = sorted(glob.glob(o3d_depth_dir+ "*.png"))
image_path = sorted(glob.glob(image_dir+"*.jpg"))
file_idx = 0
edepth_seq = Path(edepth_path[file_idx]).stem.split("_")[-1]
odepth_seq = Path(odepth_path[file_idx]).stem.split("_")[-1]
image_seq = Path(image_path[file_idx]).stem
assert edepth_seq == odepth_seq == image_seq
# read images
edepth = cv2.imread(edepth_path[file_idx], cv2.IMREAD_GRAYSCALE) # , cv2.IMREAD_GRAYSCALE
edepth = cv2.bitwise_not(edepth)
odepth = cv2.imread(odepth_path[file_idx], cv2.IMREAD_UNCHANGED) # , cv2.IMREAD_GRAYSCALE
image = cv2.imread(image_path[file_idx], cv2.IMREAD_UNCHANGED)
if debug:
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',edepth)
cv2.waitKey(0)
cv2.imshow('image',odepth)
cv2.waitKey(0)
assert edepth.shape == odepth.shape
copy_edepth = np.uint8(edepth)
edepth = np.array(edepth, dtype = "uint16") * 255
# read detected planes
planes = np.load("m.npy")
scene, images, cameras, img_width, img_height = init_colmap_pointcloud("inputs/sample/", False)
# print(planes)
# iterate planes and slice it from origin depth and colors
for plane in planes:
plane = np.squeeze(plane, axis=(2,)).astype(np.uint8) * 255
plane = cv2.resize(plane, (odepth.shape[1], odepth.shape[0]), interpolation=cv2.INTER_AREA) # ,
# create mask from plane params
mask = np.where(plane != 255)
# Image.fromarray(plane).show()
# set covered area into black-0
odepth_slice = copy.deepcopy(odepth)
odepth_slice[mask] = 0
image_slice = copy.deepcopy(image)
image_slice[mask] = 0
# Next mask If out of valid range
valid_point_threshold = image.shape[0] * image.shape[1] * valid_point_threshold_ratio
# print(len(image_slice[image_slice != 0]), valid_point_threshold)
if len(image_slice[image_slice != 0]) < valid_point_threshold:
print("=> odepth_slice do not have enought points:", len(image_slice[image_slice != 0]))
continue
if debug:
cv2.imshow('image',plane)
cv2.waitKey(0)
cv2.imshow('image',odepth_slice)
cv2.waitKey(0)
cv2.imshow('image',image_slice)
cv2.waitKey(0)
# print(odepth_slice[odepth_slice != 0])
# create point cloud from color and depth
intrinsic = o3d.camera.PinholeCameraIntrinsic()
intrinsic.intrinsic_matrix = cameras[images[image_seq+".jpg"]["cam_id"]]["intrinsic"]
pcd = getPointCloudFromRGBDImage(image_slice, odepth_slice, intrinsic, debug)
# best_plane_params, pts_inliers, pts_outliers = ransac_planefit(
# np.array(pcd.points), ransac_n, max_dst, max_trials=max_trials, stop_inliers_ratio=stop_inliers_ratio)
# print("Best Plane Params: ", best_plane_params)
out = copy.deepcopy(odepth)
out = Image.fromarray(out).convert("I")
out.save("d.png")
if __name__ == "__main__":
data_dir = "inputs/sample/"
generateTrueDepthByEdgeDetect(data_dir, debug=True)