forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth_processing.go
382 lines (356 loc) · 11.9 KB
/
depth_processing.go
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
376
377
378
379
380
381
382
package rimage
import (
"image"
"image/color"
"math"
"github.com/golang/geo/r2"
"go.viam.com/rdk/utils"
)
// PreprocessDepthMap applies data cleaning and smoothing procedures to an input depth map, and optional rgb image.
// It is assumed the depth map and rgb image are aligned.
func PreprocessDepthMap(dm *DepthMap, img *Image) (*DepthMap, error) {
var err error
// remove noisy data
CleanDepthMap(dm)
// fill in small holes
dm, err = ClosingMorph(dm, 5, 1)
if err != nil {
return nil, err
}
// fill in large holes using color info
if img != nil {
dm, err = FillDepthMap(dm, img)
if err != nil {
return nil, err
}
}
// smooth the sharp edges out
dm, err = OpeningMorph(dm, 5, 1)
if err != nil {
return nil, err
}
return dm, nil
}
// DetectDepthEdges uses a Canny edge detector to find edges in a depth map and returns a grayscale image of edges.
func (cd *CannyEdgeDetector) DetectDepthEdges(dmIn *DepthMap, blur float64) (*image.Gray, error) {
var err error
var dm *DepthMap
if cd.preprocessImage {
validPoints := MissingDepthData(dmIn)
dm, err = SavitskyGolaySmoothing(dmIn, validPoints, int(blur), 3)
if err != nil {
return nil, err
}
} else {
dm = dmIn
}
vectorField := ForwardDepthGradient(dm)
dmMagnitude, dmDirection := vectorField.MagnitudeField(), vectorField.DirectionField()
nms, err := GradientNonMaximumSuppressionC8(dmMagnitude, dmDirection)
if err != nil {
return nil, err
}
low, high, err := GetHysteresisThresholds(dmMagnitude, nms, cd.highRatio, cd.lowRatio)
if err != nil {
return nil, err
}
// low, high =
edges, err := EdgeHysteresisFiltering(dmMagnitude, low, high)
if err != nil {
return nil, err
}
return edges, nil
}
// MissingDepthData outputs a binary map where white represents where data is, and black is where data is missing.
func MissingDepthData(dm *DepthMap) *image.Gray {
nonHoles := image.NewGray(image.Rect(0, 0, dm.Width(), dm.Height()))
for y := 0; y < dm.Height(); y++ {
for x := 0; x < dm.Width(); x++ {
if dm.GetDepth(x, y) != 0 {
nonHoles.Set(x, y, color.Gray{255})
}
}
}
return nonHoles
}
// Smoothing Functions
// BilinearInterpolationDepth approximates the Depth value between pixels according to a bilinear
// interpolation. A nil return value means the interpolation is out of bounds.
func BilinearInterpolationDepth(pt r2.Point, dm *DepthMap) *Depth {
width, height := float64(dm.Width()), float64(dm.Height())
if pt.X < 0 || pt.Y < 0 || pt.X > width-1 || pt.Y > height-1 { // point out of bounds - skip it
return nil
}
xmin := int(math.Floor(pt.X))
xmax := int(math.Ceil(pt.X))
ymin := int(math.Floor(pt.Y))
ymax := int(math.Ceil(pt.Y))
// get depth values
d00 := float64(dm.GetDepth(xmin, ymin))
d10 := float64(dm.GetDepth(xmax, ymin))
d01 := float64(dm.GetDepth(xmin, ymax))
d11 := float64(dm.GetDepth(xmax, ymax))
// calculate weights
area := float64((xmax - xmin) * (ymax - ymin))
if area == 0.0 { // exactly on a pixel
result := dm.GetDepth(int(pt.X), int(pt.Y))
return &result
}
w00 := ((float64(xmax) - pt.X) * (float64(ymax) - pt.Y)) / area
w10 := ((pt.X - float64(xmin)) * (float64(ymax) - pt.Y)) / area
w01 := ((float64(xmax) - pt.X) * (pt.Y - float64(ymin))) / area
w11 := ((pt.X - float64(xmin)) * (pt.Y - float64(ymin))) / area
result := Depth(math.Round(d00*w00 + d01*w01 + d10*w10 + d11*w11))
return &result
}
// NearestNeighborDepth takes the value of the closest point to the intermediate pixel.
func NearestNeighborDepth(pt r2.Point, dm *DepthMap) *Depth {
width, height := float64(dm.Width()), float64(dm.Height())
if pt.X < 0 || pt.Y < 0 || pt.X > width-1 || pt.Y > height-1 { // point out of bounds - skip it
return nil
}
x := int(math.Round(pt.X))
y := int(math.Round(pt.Y))
// get depth value
result := dm.GetDepth(x, y)
return &result
}
// GaussianSmoothing smoothes a depth map affect by noise by using a weighted average of the pixel values in a window according
// to a gaussian distribution with a given sigma.
func GaussianSmoothing(dm *DepthMap, sigma float64) (*DepthMap, error) {
width, height := dm.Width(), dm.Height()
outDM := NewEmptyDepthMap(width, height)
if sigma <= 0. {
*outDM = *dm
return outDM, nil
}
filter := gaussianFilter(sigma)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if dm.GetDepth(x, y) == 0 {
continue
}
point := image.Point{x, y}
val := filter(point, dm)
outDM.Set(point.X, point.Y, Depth(val))
}
}
return outDM, nil
}
// SavitskyGolaySmoothing smoothes a depth map affected by noise by using a least-squares fit to a 2D polynomial equation.
// radius determines the window of the smoothing, while polyOrder determines the order of the polynomial fit.
func SavitskyGolaySmoothing(dm *DepthMap, validPoints *image.Gray, radius, polyOrder int) (*DepthMap, error) {
width, height := dm.Width(), dm.Height()
outDM := NewEmptyDepthMap(width, height)
if radius <= 0 || polyOrder <= 0 {
*outDM = *dm
return outDM, nil
}
filter, err := savitskyGolayFilter(radius, polyOrder)
if err != nil {
return nil, err
}
dmForConv := expandDepthMapForConvolution(dm, radius)
zero := color.Gray{0}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if validPoints.At(x, y) == zero {
continue
}
pointForConv := image.Point{x + radius, y + radius}
val := filter(pointForConv, dmForConv)
dmForConv.Set(x, y, Depth(val))
outDM.Set(x, y, Depth(val))
}
}
return outDM, nil
}
// JointBilateralSmoothing smoothes a depth map affected by noise by using the product of two gaussian filters,
// one based on spatial distance, and the other based on depth differences. depthSigma essentially sets a threshold to not
// smooth across large differences in depth.
func JointBilateralSmoothing(dm *DepthMap, spatialSigma, depthSigma float64) (*DepthMap, error) {
filter := jointBilateralFilter(spatialSigma, depthSigma)
width, height := dm.Width(), dm.Height()
outDM := NewEmptyDepthMap(width, height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if dm.GetDepth(x, y) == 0 {
continue
}
point := image.Point{x, y}
val := filter(point, dm)
outDM.Set(point.X, point.Y, Depth(val))
}
}
return outDM, nil
}
// expandDepthMapForConvolution pads an input depth map on every side with a mirror image of the data. This is so evaluation
// of the convolution at the borders will happen smoothly and will not created artifacts of sharp edges.
func expandDepthMapForConvolution(dm *DepthMap, radius int) *DepthMap {
if radius <= 0 {
return dm
}
width, height := dm.Width(), dm.Height()
outDM := NewEmptyDepthMap(width+2*radius, height+2*radius)
for y := -radius; y < height+radius; y++ {
for x := -radius; x < width+radius; x++ {
outDM.Set(x+radius, y+radius, dm.GetDepth(utils.AbsInt(x%width), utils.AbsInt(y%height)))
}
}
return outDM
}
// Gradient functions
// SobelDepthGradient computes the approximate gradients in the X and Y direction of a depth map and returns a vector field.
func SobelDepthGradient(dm *DepthMap) VectorField2D {
width, height := dm.Width(), dm.Height()
maxMag := 0.0
g := make([]Vec2D, 0, width*height)
sobel := sobelDepthFilter()
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// apply the Sobel Filter over a 3x3 square around each pixel
point := image.Point{x, y}
sX, sY := sobel(point, dm)
mag, dir := getMagnitudeAndDirection(sX, sY)
g = append(g, Vec2D{mag, dir})
maxMag = math.Max(math.Abs(mag), maxMag)
}
}
vf := VectorField2D{width, height, g, maxMag}
return vf
}
// ForwardDepthGradient computes the forward gradients in the X and Y direction of a depth map and returns a vector field.
func ForwardDepthGradient(dm *DepthMap) VectorField2D {
width, height := dm.Width(), dm.Height()
maxMag := 0.0
g := make([]Vec2D, 0, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
sX, sY := 0, 0
if dm.GetDepth(x, y) != 0 {
if x+1 >= 0 && x+1 < width && dm.GetDepth(x+1, y) != 0 {
sX = int(dm.GetDepth(x+1, y)) - int(dm.GetDepth(x, y))
}
if y+1 >= 0 && y+1 < height && dm.GetDepth(x, y+1) != 0 {
sY = int(dm.GetDepth(x, y+1)) - int(dm.GetDepth(x, y))
}
}
mag, dir := getMagnitudeAndDirection(float64(sX), float64(sY))
g = append(g, Vec2D{mag, dir})
maxMag = math.Max(math.Abs(mag), maxMag)
}
}
vf := VectorField2D{width, height, g, maxMag}
return vf
}
// Cleaning depth map functions
// CleanDepthMap removes the connected regions of data below a certain size thershold.
func CleanDepthMap(dm *DepthMap) {
validData := MissingDepthData(dm)
regionMap := segmentBinaryImage(validData)
for _, seg := range regionMap {
avgDepth := averageDepthInSegment(seg, dm)
threshold := thresholdFromDepth(avgDepth, dm.Width()*dm.Height())
if len(seg) < threshold {
for point := range seg {
dm.Set(point.X, point.Y, Depth(0))
}
}
}
}
// limits inpainting/cleaning to holes of a specific size. Farther distance means the same pixel size represents a larger area.
// It might be better to make it a real function of depth, right now just split into regions of close, middle, far based on distance
// in mm and make thresholds based on proportion of the image resolution.
func thresholdFromDepth(depth float64, imgResolution int) int {
res := float64(imgResolution)
switch {
case depth < 500.:
return int(0.05 * res)
case depth >= 500. && depth < 4000.:
return int(0.005 * res)
default:
return int(0.0005 * res)
}
}
// get the average depth within the segment. Assumes segment has only valid points. Checks if points are out of bounds.
func averageDepthInSegment(segment map[image.Point]bool, dm *DepthMap) float64 {
sum, count := 0.0, 0.0
for point := range segment {
if !point.In(dm.Bounds()) {
continue
}
d := float64(dm.GetDepth(point.X, point.Y))
sum += d
count++
}
return sum / count
}
// segmentBinaryImage does a bredth-first search on a black and white image and splits the non-connected white regions
// into different regions.
func segmentBinaryImage(img *image.Gray) map[int]map[image.Point]bool {
regionMap := make(map[int]map[image.Point]bool)
visited := make(map[image.Point]bool)
region := 0
queue := make([]image.Point, 0)
height, width := img.Bounds().Dy(), img.Bounds().Dx()
zero := color.Gray{0}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
p := image.Point{x, y}
if visited[p] || img.At(x, y) == zero {
continue
}
queue = append(queue, p)
segment := make(map[image.Point]bool)
for len(queue) != 0 {
// pop off element in queue and add to segment
point := queue[0]
segment[point] = true
queue = queue[1:]
// get non-visited, valid (i.e. non-zero) neighbors
neighbors := getValidNeighbors(point, img, visited)
// add neighbors to queue
queue = append(queue, neighbors...)
}
regionMap[region] = segment
region++
}
}
return regionMap
}
// getValidNeighbors uses both a B+W image of valid points as well as a map of points already visited to determine which points should
// be added in the queue for a breadth-first search.
func getValidNeighbors(pt image.Point, valid *image.Gray, visited map[image.Point]bool) []image.Point {
neighbors := make([]image.Point, 0, 4)
directions := []image.Point{
{0, 1}, // up
{0, -1}, // down
{-1, 0}, // left
{1, 0}, // right
}
zero := color.Gray{0}
for _, dir := range directions {
neighbor := image.Point{pt.X + dir.X, pt.Y + dir.Y}
if neighbor.In(valid.Bounds()) && !visited[neighbor] && valid.At(neighbor.X, neighbor.Y) != zero {
neighbors = append(neighbors, neighbor)
visited[neighbor] = true
}
}
return neighbors
}
// invertGrayImage produces a negated version of the input image.
func invertGrayImage(img *image.Gray) *image.Gray {
width, height := img.Bounds().Dx(), img.Bounds().Dy()
dst := image.NewGray(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pix := img.At(x, y)
val, err := utils.AssertType[color.Gray](pix)
if err != nil {
panic(err)
}
dst.Set(x, y, color.Gray{255 - val.Y})
}
}
return dst
}