-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
342 lines (290 loc) · 10 KB
/
filter.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
import numpy as np
from numba import njit
@njit
def left_warp(dmap):
"""
Warp left disparity map to right view.
Original values are preserved.
Interpolation is not applied, only round.
Uniqueness check: if a point collide then save max value.
Parameters
----------
dmap: HxW np.ndarray
Disparity map in the left view
Returns
-------
omap: HxW np.ndarray
Original disparity map warped to right view (occlusion map)
conf_map: HxW np.ndarray
Binary confidence map (1 for rejected points)
filtered_i: int
Number of points filtered by uniqueness check
"""
h,w = dmap.shape[:2]
omap = np.zeros(dmap.shape, dtype=dmap.dtype)
#Verbose info
warping_filtered_i = 0
#Warp left dmap in occlusion dmap
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
d = int(round(dmap[y,x]))
xd = x-d
if 0 <= xd and xd <= w-1:
if omap[y,xd] < dmap[y,x]:
if omap[y,xd] != 0:
warping_filtered_i += 1
omap[y,xd] = dmap[y,x]
return omap, warping_filtered_i
@njit
def conf_unwarp(conf, dmap):
"""
Unwarp the confidence map to left view.
Original values are preserved.
Interpolation is not applied, only round.
Parameters
----------
conf: HxW np.ndarray
Confidence map to unwarp.
dmap: HxW np.ndarray
Disparity map for warping operation.
Returns
-------
conf_rst: HxW np.ndarray
Unwarped confidence map
"""
h,w = dmap.shape[:2]
conf_rst = np.ones(conf.shape, dtype=conf.dtype)
#Warp occlusion dmap in left dmap
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
d = int(round(dmap[y,x]))
xd = x+d
if 0 <= xd and xd <= w-1:
conf_rst[y,xd] = conf[y,x]
return conf_rst
@njit
def weighted_conf(dmap, rx=9, ry=7, l=2, g=0.4375, th=1.1):
"""
Return a confidence map based on weighted distance.
Points that are too close to foreground pixel are rejected (conf=1)
Parameters
----------
dmap: HxW np.ndarray
Disparity map used to extract confidence map.
rx: int
Horizontal search radius (1,3,5,...)
ry: int
Vertical search radius (1,3,5,...)
th: float
Threshold for absolute difference
Returns
-------
conf_rst: HxW np.ndarray
Binary confidence map (1 for rejected points)
"""
h,w = dmap.shape[:2]
#Confidence map between 0 and 1 (binary)
conf_map = np.zeros(dmap.shape, dtype=np.uint8)
rx = rx//2
ry = ry//2
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
for xw in range(-rx,rx+1):
for yw in range(-ry-1,ry+1):
if 0 <= y+yw and y+yw <= h-1 and 0 <= x+xw and x+xw <= w-1:
if dmap[y+yw, x+xw] > 0:
#Check that's a "background point"
#For slanted surfaces: check later with a threshold
if dmap[y+yw, x+xw] < dmap[y,x]:
#Use Manhattan distance to keep in mind y-shifts
#Reject a point if foreground disparity is greather than distance between fg and bg
#if (dmap[y,x]-dmap[y+yw, x+xw]) - 2*((rx/(rx+ry))*abs(yw)+(ry/(rx+ry))*abs(xw)) > th:
if (dmap[y,x]-dmap[y+yw, x+xw]) - l*(g*abs(xw)+(1-g)*abs(yw)) > th:
conf_map[y+yw, x+xw] = 1
else:
conf_map[y,x] = 1
return conf_map
@njit
def filter(dmap,conf_map,th):
"""
Drop points from a disparity map based on a confidence map.
Parameters
----------
dmap: HxW np.ndarray
Disparity map to modify: there is side-effect.
conf_map: HxW np.ndarray
Confidence map to use for filtering (1 if point is filtered).
th: float
Threshold for filtering
Returns
-------
filtered_i: int
Number of points filtered
"""
h,w = dmap.shape[:2]
filtered_i = 0
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
if conf_map[y,x] > th:
dmap[y,x] = 0
filtered_i += 1
return filtered_i
def occlusion_heuristic(dmap, rx=9, ry=7, l=2, g=0.4375, th_conf=1, th_filter=0.1):
"""
Occlusion filter based on a weighted window.
Parameters
----------
dmap: HxW np.ndarray
Disparity map to modify: there is side-effect.
rx: int
x-axis radius of the window
ry: int
y-axis radius of the window
th_conf: float
confidence threshold: used to classify a occluded point
th_filter: float
If confidence is binary (and weighted is) every value (0.0,1.0) is fine
Return
------
dmap: HxW np.ndarray
Filtered disparity map
conf_map: HxW np.ndarray
Binary confidence map: 0 for no occlusion
Usage
-----
```python
from filter import occlusion_heuristic
gts = sample_gt(gt)
gts_filtered = occlusion_heuristic(gts)[0]
```
"""
omap, _ = left_warp(dmap)
conf_map = weighted_conf(omap,rx=rx, ry=ry, l=l, g=g, th=th_conf)
conf_map = conf_unwarp(conf_map, omap)
return None, conf_map
@njit
def conti_conf(dmap, n=7, th=15):
"""
Return a confidence map based on Conti's method (https://arxiv.org/abs/2210.03118).
Points in a window that are far from foreground are rejected.
Parameters
----------
dmap: HxW np.ndarray
Disparity map used to extract confidence map.
n: int
Window size (3,5,7,...)
th: float
Threshold for absolute difference
Returns
-------
conf_rst: HxW np.ndarray
Binary confidence map (1 for rejected points)
"""
h,w = dmap.shape[:2]
#Confidence map between 0 and 1 (binary)
conf_map = np.zeros(dmap.shape, dtype=np.uint8)
n = n//2
#Conti's filtering method
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
#Search max
dmax = 0
for yw in range(-n,n+1):
for xw in range(-n,n+1):
if 0 <= y+yw and y+yw <= h-1 and 0 <= x+xw and x+xw <= w-1:
if dmap[y+yw,x+xw] > dmax:
dmax = dmap[y+yw,x+xw]
#Find pixel-wise confidence
for yw in range(-n,n+1):
for xw in range(-n,n+1):
if 0 <= y+yw and y+yw <= h-1 and 0 <= x+xw and x+xw <= w-1:
#Absolute thresholding
if dmax-dmap[y+yw,x+xw] > th:
conf_map[y+yw,x+xw] = 1
else:
conf_map[y,x] = 1
return conf_map
def filter_heuristic(dmap, n=9, th=10, th_filter=0.1):
filtered_dmap = dmap.copy()
conf_map = conti_conf(dmap, n, th)
_ = filter(filtered_dmap, conf_map, th_filter)
return filtered_dmap, conf_map
@njit
def conti_conf_depth(delta_map, th=3):
"""
Return a confidence map based on Conti's method (https://arxiv.org/abs/2210.03118).
Points in a window that are far from foreground are rejected.
Parameters
----------
dmap: HxW np.ndarray
Depth map used to extract confidence map.
n: int
Window size (3,5,7,...)
th: float
Threshold for absolute difference
Returns
-------
conf_rst: HxW np.ndarray
Binary confidence map (1 for rejected points)
"""
h,w = delta_map.shape[:2]
#Confidence map between 0 and 1 (binary)
conf_map = np.zeros(delta_map.shape, dtype=np.uint8)
#Conti's filtering method
for y in range(h):
for x in range(w):
#Absolute thresholding
if delta_map[y,x] > th:
conf_map[y,x] = 1
return conf_map
@njit
def delta_depth(dmap, nx=7, ny=3):
"""
Return a confidence map based on Conti's method (https://arxiv.org/abs/2210.03118).
Points in a window that are far from foreground are rejected.
Parameters
----------
dmap: HxW np.ndarray
Depth map used to extract confidence map.
n: int
Window size (3,5,7,...)
th: float
Threshold for absolute difference
Returns
-------
conf_rst: HxW np.ndarray
Binary confidence map (1 for rejected points)
"""
h,w = dmap.shape[:2]
delta_map = np.zeros(dmap.shape, dtype=np.float32)
nx = (nx-1)//2
ny = (ny-1)//2
#Conti's filtering method
for y in range(h):
for x in range(w):
if dmap[y,x] > 0:
#Search min
dmin = 1000000.0
for yw in range(-ny,ny+1):
for xw in range(-nx,nx+1):
if 0 <= y+yw and y+yw <= h-1 and 0 <= x+xw and x+xw <= w-1:
if dmap[y+yw,x+xw] < dmin and dmap[y+yw,x+xw] > 1e-3:
dmin = dmap[y+yw,x+xw]
#Find pixel-wise confidence
for yw in range(-ny,ny+1):
for xw in range(-nx,nx+1):
if 0 <= y+yw and y+yw <= h-1 and 0 <= x+xw and x+xw <= w-1:
if delta_map[y+yw,x+xw] < dmap[y+yw,x+xw]-dmin:
delta_map[y+yw,x+xw] = dmap[y+yw,x+xw]-dmin
return delta_map
def filter_heuristic_depth(dmap, nx=7, ny=3, th=1.5, th_filter=0.1):
dmap_copy = dmap.copy()
deltamap = delta_depth(dmap_copy, nx, ny)
conf_map = conti_conf_depth(deltamap, th)
_ = filter(dmap_copy, conf_map, th_filter)
return dmap_copy, conf_map