Apollo  v5.5.0
Open source self driving car software
obj_mapper.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 <algorithm>
19 #include <map>
20 #include <vector>
21 
22 #include "cyber/common/log.h"
25 
26 namespace apollo {
27 namespace perception {
28 namespace camera {
29 
31  float hwl[3] = {0};
32  float bbox[4] = {0};
33  float ry = 0.0f;
34  bool is_veh = true;
35  bool check_dimension = true;
37 };
38 
39 // hyper parameters
41  ObjMapperParams() { set_default(); }
42 
43  void set_default();
44 
45  int nr_bins_z;
49 
51  float factor_small;
52  float learning_r;
53  float reproj_err;
54  float rz_ratio;
56  float stable_w;
57  float occ_ratio;
58  float depth_min;
59  float dist_far;
60  float eps_mapper;
61  float iou_suc;
62  float iou_high;
64 };
65 
66 class ObjMapper {
67  public:
68  ObjMapper() : width_(0), height_(0) {
69  memset(k_mat_, 0, sizeof(float) * 9);
70  resize_ry_score(params_.nr_bins_ry);
71  set_default_variance();
72  }
73 
75 
77  orientation_variance_(0) = 1.0f;
78  orientation_variance_(1) = orientation_variance_(2) = 0.0f;
79  position_uncertainty_ << 1, 0, 0, 0, 0, 0, 0, 0, 1;
80  }
81 
82  void Init(const float *k_mat, int width, int height) {
83  memcpy(k_mat_, k_mat, sizeof(float) * 9);
84  width_ = width;
85  height_ = height;
86  object_template_manager_ = ObjectTemplateManager::Instance();
87  }
88 
89  void resize_ry_score(int size) { ry_score_.resize(size); }
90 
91  std::vector<float> get_ry_score() { return ry_score_; }
92 
93  std::vector<float> get_ry_score(float ry) {
94  FillRyScoreSingleBin(ry);
95  return ry_score_;
96  }
97 
98  Eigen::Vector3d get_orientation_var() { return orientation_variance_; }
99 
100  Eigen::Matrix3d get_position_uncertainty() { return position_uncertainty_; }
101 
102  // main interface, center is the bottom-face center ("center" for short)
103  bool Solve3dBbox(const ObjMapperOptions &options, float center[3],
104  float hwl[3], float *ry);
105 
106  private:
107  bool Solve3dBboxGivenOneFullBboxDimensionOrientation(const float *bbox,
108  const float *hwl,
109  float *ry,
110  float *center);
111 
112  bool SolveCenterFromNearestVerticalEdge(const float *bbox, const float *hwl,
113  float ry, float *center,
114  float *center_2d) const;
115 
116  void UpdateCenterViaBackProjectZ(const float *bbox, const float *hwl,
117  const float *center_2d,
118  float *center) const {
119  float z = center[2];
120  common::IBackprojectCanonical(center_2d, k_mat_, z, center);
121  center[1] += hwl[0] / 2;
122  }
123 
124  float GetProjectionScore(float ry, const float *bbox, const float *hwl,
125  const float *center, bool check_truncation = false,
126  float *bbox_res = nullptr) const {
127  float rot[9] = {0};
128  GenRotMatrix(ry, rot);
129  float *bbox_near = nullptr;
130  float score = GetScoreViaRotDimensionCenter(
131  &k_mat_[0], width_, height_, bbox, rot, hwl, center, check_truncation,
132  bbox_res, bbox_near);
133  return score;
134  }
135 
136  void PostRefineZ(const float *bbox, const float *hwl, const float *center2d,
137  float ry, float *center, float dz = 0.2f, float rz = 1.0f,
138  bool fix_proj = true) const {
139  float z = center[2];
140  float score_best = 0.0f;
141  float x[2] = {center2d[0], center2d[1]};
142  float center_best[3] = {center[0], center[1], center[2]};
143  for (float offset = -rz; offset <= rz; offset += dz) {
144  float z_test = std::max(z + offset, params_.depth_min);
145  float center_test[3] = {center[0], center[1], z_test};
146  if (fix_proj) {
147  common::IBackprojectCanonical(x, k_mat_, z_test, center_test);
148  center_test[1] += hwl[0] / 2;
149  }
150  float score_test = GetProjectionScore(ry, bbox, hwl, center_test);
151  if (score_best < score_test) {
152  score_best = score_test;
153  memcpy(center_best, center_test, sizeof(float) * 3);
154  }
155  }
156  memcpy(center, center_best, sizeof(float) * 3);
157  }
158 
159  void PostRefineOrientation(const float *bbox, const float *hwl,
160  const float *center, float *ry);
161 
162  void GetCenter(const float *bbox, const float &z_ref, const float &ry,
163  const float *hwl, float *center,
164  float *x /*init outside*/) const;
165 
166  void FillRyScoreSingleBin(const float &ry) {
167  int nr_bins_ry = static_cast<int>(ry_score_.size());
168  memset(ry_score_.data(), 0, sizeof(float) * nr_bins_ry);
169  const float PI = common::Constant<float>::PI();
170  int index = static_cast<int>(
171  std::floor((ry + PI) * static_cast<float>(nr_bins_ry) / (2.0f * PI)));
172  ry_score_[index % nr_bins_ry] = 1.0f;
173  }
174 
175  private:
176  // canonical camera: p_matrix = k_matrix [I 0]
177  float k_mat_[9] = {0};
178  int width_ = 0;
179  int height_ = 0;
180  std::vector<float> ry_score_ = {};
181  ObjMapperParams params_;
182 
183  // per-dimension variance of the orientation [yaw, pitch, roll]
184  Eigen::Vector3d orientation_variance_;
185 
186  // covariance matrix for position uncertainty
187  Eigen::Matrix3d position_uncertainty_;
188 
189  // DISALLOW_COPY_AND_ASSIGN(ObjMapper);
190 
191  protected:
192  ObjectTemplateManager *object_template_manager_ = nullptr;
193 };
194 
195 } // namespace camera
196 } // namespace perception
197 } // namespace apollo
float iou_suc
Definition: obj_mapper.h:61
Definition: obj_mapper.h:66
float occ_ratio
Definition: obj_mapper.h:57
float eps_mapper
Definition: obj_mapper.h:60
std::vector< float > get_ry_score()
Definition: obj_mapper.h:91
float dist_far
Definition: obj_mapper.h:59
const double PI
Definition: types.h:22
float reproj_err
Definition: obj_mapper.h:53
float bbox[4]
Definition: obj_mapper.h:32
float angle_resolution_degree
Definition: obj_mapper.h:63
Definition: blob.h:72
Eigen::Vector3d get_orientation_var()
Definition: obj_mapper.h:98
int boundary_len
Definition: obj_mapper.h:47
float iou_high
Definition: obj_mapper.h:62
int type_min_vol_index
Definition: obj_mapper.h:36
float depth_min
Definition: obj_mapper.h:58
bool check_dimension
Definition: obj_mapper.h:35
void set_default_variance()
Definition: obj_mapper.h:76
float factor_small
Definition: obj_mapper.h:51
float small_bbox_height
Definition: obj_mapper.h:50
Definition: object_template_manager.h:49
void GenRotMatrix(const T &ry, T *rot)
Definition: twod_threed_util.h:101
float stable_w
Definition: obj_mapper.h:56
float rz_ratio
Definition: obj_mapper.h:54
void resize_ry_score(int size)
Definition: obj_mapper.h:89
float learning_r
Definition: obj_mapper.h:52
int nr_bins_z
Definition: obj_mapper.h:45
~ObjMapper()
Definition: obj_mapper.h:74
int max_nr_iter
Definition: obj_mapper.h:48
ObjMapperParams()
Definition: obj_mapper.h:41
T GetScoreViaRotDimensionCenter(const T *k_mat, int width, int height, const T *bbox, const T *rot, const T *hwl, const T *location, const bool &check_truncation, T *bbox_res=nullptr, T *bbox_near=nullptr)
Definition: twod_threed_util.h:177
float abnormal_h_veh
Definition: obj_mapper.h:55
std::vector< float > get_ry_score(float ry)
Definition: obj_mapper.h:93
void Init(const float *k_mat, int width, int height)
Definition: obj_mapper.h:82
float hwl[3]
Definition: obj_mapper.h:31
int nr_bins_ry
Definition: obj_mapper.h:46
bool is_veh
Definition: obj_mapper.h:34
ObjMapper()
Definition: obj_mapper.h:68
void IBackprojectCanonical(const T *x, const T *K, T depth, T *X)
Definition: i_util.h:164
float ry
Definition: obj_mapper.h:33
Eigen::Matrix3d get_position_uncertainty()
Definition: obj_mapper.h:100