-
Notifications
You must be signed in to change notification settings - Fork 109
/
depth_edges.go
77 lines (68 loc) · 2.29 KB
/
depth_edges.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
package transformpipeline
import (
"context"
"image"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/gostream"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage"
"go.viam.com/rdk/rimage/transform"
"go.viam.com/rdk/utils"
)
type depthEdgesConfig struct {
HiThresh float64 `json:"high_threshold_pct"`
LoThresh float64 `json:"low_threshold_pct"`
BlurRadius float64 `json:"blur_radius_px"`
}
// depthEdgesSource applies a Canny Edge Detector to the depth map.
type depthEdgesSource struct {
stream gostream.VideoStream
detector *rimage.CannyEdgeDetector
blurRadius float64
}
func newDepthEdgesTransform(ctx context.Context, source gostream.VideoSource, am utils.AttributeMap,
) (gostream.VideoSource, camera.ImageType, error) {
conf, err := resource.TransformAttributeMap[*depthEdgesConfig](am)
if err != nil {
return nil, camera.UnspecifiedStream, err
}
props, err := propsFromVideoSource(ctx, source)
if err != nil {
return nil, camera.UnspecifiedStream, err
}
var cameraModel transform.PinholeCameraModel
cameraModel.PinholeCameraIntrinsics = props.IntrinsicParams
if props.DistortionParams != nil {
cameraModel.Distortion = props.DistortionParams
}
canny := rimage.NewCannyDericheEdgeDetectorWithParameters(conf.HiThresh, conf.LoThresh, true)
videoSrc := &depthEdgesSource{gostream.NewEmbeddedVideoStream(source), canny, 3.0}
src, err := camera.NewVideoSourceFromReader(ctx, videoSrc, &cameraModel, camera.DepthStream)
if err != nil {
return nil, camera.UnspecifiedStream, err
}
return src, camera.DepthStream, err
}
// Next applies a canny edge detector on the depth map of the next image.
func (os *depthEdgesSource) Read(ctx context.Context) (image.Image, func(), error) {
ctx, span := trace.StartSpan(ctx, "camera::transformpipeline::depthEdges::Read")
defer span.End()
i, closer, err := os.stream.Next(ctx)
if err != nil {
return nil, nil, err
}
dm, err := rimage.ConvertImageToDepthMap(ctx, i)
if err != nil {
return nil, nil, errors.Wrapf(err, "cannot transform for depth edges")
}
edges, err := os.detector.DetectDepthEdges(dm, os.blurRadius)
if err != nil {
return nil, nil, err
}
return edges, closer, nil
}
func (os *depthEdgesSource) Close(ctx context.Context) error {
return os.stream.Close(ctx)
}