-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdense_to_sparse.py
86 lines (67 loc) · 2.82 KB
/
dense_to_sparse.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
import numpy as np
import cv2
def rgb2grayscale(rgb):
return rgb[:, :, 0] * 0.2989 + rgb[:, :, 1] * 0.587 + rgb[:, :, 2] * 0.114
class DenseToSparse:
def __init__(self):
pass
def dense_to_sparse(self, rgb, depth):
pass
def __repr__(self):
pass
class UniformSampling(DenseToSparse):
name = "uar"
def __init__(self, num_samples, max_depth=np.inf):
DenseToSparse.__init__(self)
self.num_samples = num_samples
self.max_depth = max_depth
def __repr__(self):
return "%s{ns=%d,md=%f}" % (self.name, self.num_samples, self.max_depth)
def dense_to_sparse(self, rgb, depth):
"""
Samples pixels with `num_samples`/#pixels probability in `depth`.
Only pixels with a maximum depth of `max_depth` are considered.
If no `max_depth` is given, samples in all pixels
"""
mask_keep = depth > 0
if self.max_depth is not np.inf:
mask_keep = np.bitwise_and(mask_keep, depth <= self.max_depth)
n_keep = np.count_nonzero(mask_keep)
if n_keep == 0:
return mask_keep
else:
prob = float(self.num_samples) / n_keep
return np.bitwise_and(mask_keep, np.random.uniform(0, 1, depth.shape) < prob)
class SimulatedStereo(DenseToSparse):
name = "sim_stereo"
def __init__(self, num_samples, max_depth=np.inf, dilate_kernel=3, dilate_iterations=1):
DenseToSparse.__init__(self)
self.num_samples = num_samples
self.max_depth = max_depth
self.dilate_kernel = dilate_kernel
self.dilate_iterations = dilate_iterations
def __repr__(self):
return "%s{ns=%d,md=%f,dil=%d.%d}" % \
(self.name, self.num_samples, self.max_depth, self.dilate_kernel, self.dilate_iterations)
# We do not use cv2.Canny, since that applies non max suppression
# So we simply do
# RGB to intensitities
# Smooth with gaussian
# Take simple sobel gradients
# Threshold the edge gradient
# Dilatate
def dense_to_sparse(self, rgb, depth):
gray = rgb2grayscale(rgb)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
gx = cv2.Sobel(blurred, cv2.CV_64F, 1, 0, ksize=5)
gy = cv2.Sobel(blurred, cv2.CV_64F, 0, 1, ksize=5)
depth_mask = np.bitwise_and(depth != 0.0, depth <= self.max_depth)
edge_fraction = float(self.num_samples) / np.size(depth)
mag = cv2.magnitude(gx, gy)
min_mag = np.percentile(mag[depth_mask], 100 * (1.0 - edge_fraction))
mag_mask = mag >= min_mag
if self.dilate_iterations >= 0:
kernel = np.ones((self.dilate_kernel, self.dilate_kernel), dtype=np.uint8)
cv2.dilate(mag_mask.astype(np.uint8), kernel, iterations=self.dilate_iterations)
mask = np.bitwise_and(mag_mask, depth_mask)
return mask