Apollo 11.0
自动驾驶开放平台
ncut.h
浏览该文件的文档.
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#pragma once
17
18#include <cmath>
19#include <cstdlib>
20#include <iostream>
21#include <map>
22#include <memory>
23#include <string>
24#include <tuple>
25#include <vector>
26
27#include <opencv2/opencv.hpp>
28
29#include "Eigen/Core"
30
31#include "modules/perception/lidar_segmentation/segmentor/ncut_segmentation/proto/ncut_config.pb.h"
32#include "modules/perception/lidar_segmentation/segmentor/ncut_segmentation/proto/ncut_param.pb.h"
33
36
37namespace apollo {
38namespace perception {
39namespace lidar {
40
41class NCut {
42public:
43 NCut();
44 ~NCut();
52 bool Init(const NCutParam& param);
58 int NumSegments() const {
59 return static_cast<int>(_segment_pids.size());
60 }
67 std::string GetSegmentLabel(int sid) const {
68 return _segment_labels[sid];
69 }
78 void GetSegmentSize(int sid, float* length, float* width, float* height) const {
79 NcutBoundingBox box = _segment_bbox[sid];
80 *length = std::get<1>(box) - std::get<0>(box);
81 *width = std::get<3>(box) - std::get<2>(box);
82 *height = std::get<5>(box) - std::get<4>(box);
83 }
91 base::PointFCloudPtr pc = base::PointFCloudPtr(new base::PointFCloud(*_cloud_obstacles, _segment_pids[sid]));
92 return pc;
93 }
107 std::string GetPcRoughLabel(const base::PointFCloudPtr& cloud, bool only_check_pedestrian);
116 void GetSegmentRoughSize(const base::PointFCloudPtr& cloud, float* length, float* width, float* height);
117
118private:
119 struct gridIndex {
120 int irow;
121 int jcol;
122 };
123
124 // x_min, x_max, y_min, y_max, z_min, z_max;
125 typedef std::tuple<float, float, float, float, float, float> NcutBoundingBox;
126 base::PointFCloudPtr _cloud_obstacles;
127 // super pixels related
128 float _grid_radius;
129 float _super_pixel_cell_size;
130 std::unique_ptr<LRClassifier> _classifier;
131 // felzenszwalb
132 double _felzenszwalb_sigma;
133 double _felzenszwalb_k;
134 int _felzenszwalb_min_size;
135 // graph cut related
136 double _sigma_feature;
137 double _sigma_space;
138 double _connect_radius;
139 int _num_cuts;
140 float _ncuts_stop_threshold;
141 double _ncuts_enable_classifier_threshold;
142 // component (cluster) information
143 std::vector<std::vector<int>> _cluster_points;
144 // x_min, x_max, y_min, y_max, z_min, z_max;
145 std::vector<NcutBoundingBox> _cluster_bounding_box;
146 std::vector<std::string> _cluster_labels;
147 // skeleton related
148 float _skeleton_cell_size; // skeleton sample size
149 int _patch_size;
150 cv::Mat _cv_feature_map;
151 FloodFill _ff_feature_grid;
152 std::vector<Eigen::MatrixXf> _cluster_skeleton_points;
153 std::vector<Eigen::MatrixXf> _cluster_skeleton_features;
154 // merge overlap
155 double _overlap_factor;
156 // final segments, each vector contains
157 std::vector<std::vector<int>> _segment_pids;
158 std::vector<std::string> _segment_labels;
159 std::vector<NcutBoundingBox> _segment_bbox;
160 std::vector<std::vector<int>> _outlier_pids;
161
162 void SampleByGrid(
163 const std::vector<int>& point_gids,
164 Eigen::MatrixXf* skeleton_coords,
165 Eigen::MatrixXf* skeleton_feature);
166
167 void PrecomputeAllSkeletonAndBbox();
168
169 bool Configure(const NCutParam& ncut_param_);
170
171 void SuperPixelsFloodFill(
173 float radius,
174 float cell_size,
175 std::vector<std::vector<int>>* super_pixels);
176
177 // super pixels
178 void BuildAverageHeightMap(
180 const FloodFill& ff_map,
181 cv::Mat* cv_height_map,
182 std::vector<gridIndex>* point_pixel_indices);
183
184 // skeleton
185 void GetPatchFeature(const Eigen::MatrixXf& points, Eigen::MatrixXf* features);
186
187 // bounding box
188 NcutBoundingBox ComputeClusterBoundingBox(const std::vector<int>& point_gids);
189
190 std::string GetPcLabel(const base::PointFCloudPtr& cloud);
191
192 void NormalizedCut(
193 float ncuts_threshold,
194 bool use_classifier,
195 std::vector<std::vector<int>>* segment_clusters,
196 std::vector<std::string>* segment_labels);
197
198 void ComputeSkeletonWeights(Eigen::MatrixXf* weights);
199
200 float GetMinNcuts(
201 const Eigen::MatrixXf& in_weights,
202 const std::vector<int>* in_clusters,
203 std::vector<int>* seg1,
204 std::vector<int>* seg2);
205
206 void LaplacianDecomposition(const Eigen::MatrixXf& weights, Eigen::MatrixXf* eigenvectors);
207
208 bool ComputeSquaredSkeletonDistance(
209 const Eigen::MatrixXf& in1_points,
210 const Eigen::MatrixXf& in1_features,
211 const Eigen::MatrixXf& in2_points,
212 const Eigen::MatrixXf& in2_features,
213 float* dist_point,
214 float* dist_feature);
215
216 bool IsMovableObstacle(const std::vector<int>& cluster_ids, std::string* label);
217
218 inline bool IsPotentialPedestrianSize(float length, float width) {
219 return ((length > 0.5 && length < 1.5) && (width > 0.3 && width < 1));
220 }
221
222 inline bool IsPotentialBicyclistSize(float length, float width) {
223 return ((length > 1 && length < 2.5) && (width > 0.3 && width < 1.5));
224 }
225
226 inline bool IsPotentialCarSize(float length, float width) {
227 // return ( (length > 1.0 && length < 20.0) && (width > 1.0 && width < 3.5)
228 // );
229 return ((length > 1.0 && length < 8.0) && (width > 1.0 && width < 3.5));
230 }
231
232 int GetComponentBoundingBox(const std::vector<int>& cluster_ids, NcutBoundingBox* box);
233
234 inline float GetBboxLength(const NcutBoundingBox& box) {
235 return (std::get<1>(box) - std::get<0>(box));
236 }
237
238 inline float GetBboxWidth(const NcutBoundingBox& box) {
239 return (std::get<3>(box) - std::get<2>(box));
240 }
241
242 inline float GetBboxHeight(const NcutBoundingBox& box) {
243 return (std::get<5>(box) - std::get<4>(box));
244 }
245
246 std::string GetClustersLabel(const std::vector<int>& cluster_ids);
247
248 void GetClustersPids(const std::vector<int>& cids, std::vector<int>* pids);
249};
250
251} // namespace lidar
252} // namespace perception
253} // namespace apollo
void GetSegmentSize(int sid, float *length, float *width, float *height) const
Get the segment size
Definition ncut.h:78
std::string GetSegmentLabel(int sid) const
Get the segment label
Definition ncut.h:67
base::PointFCloudPtr GetSegmentPointCloud(int sid) const
Get the segment point cloud
Definition ncut.h:90
int NumSegments() const
Get segmentation number
Definition ncut.h:58
std::string GetPcRoughLabel(const base::PointFCloudPtr &cloud, bool only_check_pedestrian)
Get the label of point cloud
Definition ncut.cc:749
void GetSegmentRoughSize(const base::PointFCloudPtr &cloud, float *length, float *width, float *height)
Get the segment size
Definition ncut.cc:786
bool Init(const NCutParam &param)
Init Ncut according to parameters
Definition ncut.cc:53
void Segment(base::PointFCloudConstPtr cloud)
Segmen main entrance
Definition ncut.cc:87
std::shared_ptr< PointFCloud > PointFCloudPtr
std::shared_ptr< const PointFCloud > PointFCloudConstPtr
class register implement
Definition arena_queue.h:37