Apollo  6.0
Open source self driving car software
segment_graph.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 /*
17 Copyright (C) 2006 Pedro Felzenszwalb
18 
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23 
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28 
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33 
34 #pragma once
35 #include <algorithm>
36 #include <cmath>
37 
38 #include "modules/perception/lidar/detector/ncut_segmentation/common/graph_felzenszwalb/disjoint_set.h"
39 
40 namespace apollo {
41 namespace perception {
42 namespace lidar {
43 
44 // threshold function
45 #define THRESHOLD(size, c) (c / size)
46 
47 typedef struct {
48  float w;
49  int a;
50  int b;
51 } edge;
52 
53 bool operator<(const edge &a, const edge &b) { return a.w < b.w; }
54 
55 /*
56  * Segment a graph
57  *
58  * Returns a disjoint-set forest representing the segmentation.
59  *
60  * num_vertices: number of vertices in graph.
61  * num_edges: number of edges in graph
62  * edges: array of edges.
63  * c: constant for threshold function.
64  */
65 Universe *segment_graph(int num_vertices, int num_edges, edge *edges, float c) {
66  // sort edges by weight
67  std::sort(edges, edges + num_edges);
68 
69  // make a disjoint-set forest
70  Universe *u = new Universe(num_vertices);
71 
72  // init thresholds
73  float *threshold = new float[num_vertices];
74  for (int i = 0; i < num_vertices; i++) {
75  threshold[i] = THRESHOLD(1, c);
76  }
77 
78  // for each edge, in non-decreasing weight order...
79  for (int i = 0; i < num_edges; i++) {
80  edge *pedge = &edges[i];
81 
82  // components connected by this edge
83  int a = u->find(pedge->a);
84  int b = u->find(pedge->b);
85  if (a != b) {
86  if ((pedge->w <= threshold[a]) && (pedge->w <= threshold[b])) {
87  u->join(a, b);
88  a = u->find(a);
89  threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
90  }
91  }
92  }
93 
94  // free up
95  delete threshold;
96  return u;
97 }
98 
99 } // namespace lidar
100 } // namespace perception
101 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int find(int x)
Definition: disjoint_set.h:75
void join(int x, int y)
Definition: disjoint_set.h:84
bool operator<(const edge &a, const edge &b)
Definition: segment_graph.h:53
int a
Definition: segment_graph.h:49
Definition: segment_graph.h:47
int size(int x) const
Definition: disjoint_set.h:55
#define THRESHOLD(size, c)
Definition: segment_graph.h:45
Definition: disjoint_set.h:49
Universe * segment_graph(int num_vertices, int num_edges, edge *edges, float c)
Definition: segment_graph.h:65
float w
Definition: segment_graph.h:48
int b
Definition: segment_graph.h:50
Image< uchar > * threshold(Image< T > *src, int t)
Definition: imutil.h:63