-
Notifications
You must be signed in to change notification settings - Fork 110
/
radius_clustering.go
171 lines (161 loc) · 5.35 KB
/
radius_clustering.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
package segmentation
import (
"context"
"github.com/golang/geo/r3"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"go.viam.com/rdk/components/camera"
pc "go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/utils"
"go.viam.com/rdk/vision"
)
// RadiusClusteringConfig specifies the necessary parameters for 3D object finding.
type RadiusClusteringConfig struct {
resource.TriviallyValidateConfig
MinPtsInPlane int `json:"min_points_in_plane"`
MinPtsInSegment int `json:"min_points_in_segment"`
ClusteringRadiusMm float64 `json:"clustering_radius_mm"`
MeanKFiltering int `json:"mean_k_filtering"`
Label string `json:"label,omitempty"`
}
// CheckValid checks to see in the input values are valid.
func (rcc *RadiusClusteringConfig) CheckValid() error {
if rcc.MinPtsInPlane <= 0 {
return errors.Errorf("min_points_in_plane must be greater than 0, got %v", rcc.MinPtsInPlane)
}
if rcc.MinPtsInSegment <= 0 {
return errors.Errorf("min_points_in_segment must be greater than 0, got %v", rcc.MinPtsInSegment)
}
if rcc.ClusteringRadiusMm <= 0 {
return errors.Errorf("clustering_radius_mm must be greater than 0, got %v", rcc.ClusteringRadiusMm)
}
return nil
}
// ConvertAttributes changes the AttributeMap input into a RadiusClusteringConfig.
func (rcc *RadiusClusteringConfig) ConvertAttributes(am utils.AttributeMap) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{TagName: "json", Result: rcc})
if err != nil {
return err
}
err = decoder.Decode(am)
if err == nil {
err = rcc.CheckValid()
}
return err
}
// NewRadiusClustering returns a Segmenter that removes the planes (if any) and returns
// a segmentation of the objects in a point cloud using a radius based clustering algo
// described in the paper "A Clustering Method for Efficient Segmentation of 3D Laser Data" by Klasing et al. 2008.
func NewRadiusClustering(params utils.AttributeMap) (Segmenter, error) {
// convert attributes to appropriate struct
if params == nil {
return nil, errors.New("config for radius clustering segmentation cannot be nil")
}
cfg := &RadiusClusteringConfig{}
err := cfg.ConvertAttributes(params)
if err != nil {
return nil, err
}
return cfg.RadiusClustering, nil
}
// RadiusClustering applies the radius clustering algorithm directly on a given point cloud.
func (rcc *RadiusClusteringConfig) RadiusClustering(ctx context.Context, src camera.VideoSource) ([]*vision.Object, error) {
// get next point cloud
cloud, err := src.NextPointCloud(ctx)
if err != nil {
return nil, err
}
ps := NewPointCloudPlaneSegmentation(cloud, 10, rcc.MinPtsInPlane)
// if there are found planes, remove them, and keep all the non-plane points
_, nonPlane, err := ps.FindPlanes(ctx)
if err != nil {
return nil, err
}
// filter out the noise on the point cloud if mean K is greater than 0
if rcc.MeanKFiltering > 0.0 {
filter, err := pc.StatisticalOutlierFilter(rcc.MeanKFiltering, 1.25)
if err != nil {
return nil, err
}
nonPlane, err = filter(nonPlane)
if err != nil {
return nil, err
}
}
// do the segmentation
segments, err := segmentPointCloudObjects(nonPlane, rcc.ClusteringRadiusMm, rcc.MinPtsInSegment)
if err != nil {
return nil, err
}
objects, err := NewSegmentsFromSlice(segments, rcc.Label)
if err != nil {
return nil, err
}
return objects.Objects, nil
}
// segmentPointCloudObjects uses radius based nearest neighbors to segment the images, and then prunes away
// segments that do not pass a certain threshold of points.
func segmentPointCloudObjects(cloud pc.PointCloud, radius float64, nMin int) ([]pc.PointCloud, error) {
segments, err := radiusBasedNearestNeighbors(cloud, radius)
if err != nil {
return nil, err
}
segments = pc.PrunePointClouds(segments, nMin)
return segments, nil
}
// radiusBasedNearestNeighbors partitions the pointcloud, grouping points within a given radius of each other.
func radiusBasedNearestNeighbors(cloud pc.PointCloud, radius float64) ([]pc.PointCloud, error) {
kdt, ok := cloud.(*pc.KDTree)
if !ok {
kdt = pc.ToKDTree(cloud)
}
var err error
clusters := NewSegments()
c := 0
kdt.Iterate(0, 0, func(v r3.Vector, d pc.Data) bool {
// skip if point already is assigned cluster
if _, ok := clusters.Indices[v]; ok {
return true
}
// if not assigned, see if any of its neighbors are assigned a cluster
nn := kdt.RadiusNearestNeighbors(v, radius, false)
for _, neighbor := range nn {
nv := neighbor.P
ptIndex, ptOk := clusters.Indices[v]
neighborIndex, neighborOk := clusters.Indices[nv]
switch {
case ptOk && neighborOk:
if ptIndex != neighborIndex {
err = clusters.MergeClusters(ptIndex, neighborIndex)
}
case !ptOk && neighborOk:
err = clusters.AssignCluster(v, d, neighborIndex)
case ptOk && !neighborOk:
err = clusters.AssignCluster(neighbor.P, neighbor.D, ptIndex)
}
if err != nil {
return false
}
}
// if none of the neighbors were assigned a cluster, create a new cluster and assign all neighbors to it
if _, ok := clusters.Indices[v]; !ok {
err = clusters.AssignCluster(v, d, c)
if err != nil {
return false
}
for _, neighbor := range nn {
err = clusters.AssignCluster(neighbor.P, neighbor.D, c)
if err != nil {
return false
}
}
c++
}
return true
})
if err != nil {
return nil, err
}
return clusters.PointClouds(), nil
}