Apollo 11.0
自动驾驶开放平台
obj_mapper.h
浏览该文件的文档.
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 <vector>
20
21#include "Eigen/Core"
22
23#include "cyber/common/log.h"
27
28namespace apollo {
29namespace perception {
30namespace camera {
31
33 float hwl[3] = {0};
34 float bbox[4] = {0};
35 float ry = 0.0f;
36 bool is_veh = true;
37 bool check_dimension = true;
39};
40
41// hyper parameters
67
68class ObjMapper {
69 public:
70 ObjMapper() : width_(0), height_(0) {
71 memset(k_mat_, 0, sizeof(float) * 9);
74 }
75
76 ~ObjMapper() = default;
82 orientation_variance_(0) = 1.0f;
83 orientation_variance_(1) = orientation_variance_(2) = 0.0f;
84 position_uncertainty_ << 1, 0, 0, 0, 0, 0, 0, 0, 1;
85 }
93 void Init(const float *k_mat, int width, int height) {
94 memcpy(k_mat_, k_mat, sizeof(float) * 9);
95 width_ = width;
96 height_ = height;
97 object_template_manager_ = ObjectTemplateManager::Instance();
98 }
104 void resize_ry_score(int size) { ry_score_.resize(size); }
110 std::vector<float> get_ry_score() { return ry_score_; }
117 std::vector<float> get_ry_score(float ry) {
118 FillRyScoreSingleBin(ry);
119 return ry_score_;
120 }
126 Eigen::Vector3d get_orientation_var() { return orientation_variance_; }
132 Eigen::Matrix3d get_position_uncertainty() { return position_uncertainty_; }
133
134 // main interface, center is the bottom-face center ("center" for short)
145 bool Solve3dBbox(const ObjMapperOptions &options, float center[3],
146 float hwl[3], float *ry);
147
148 private:
162 bool Solve3dBboxGivenOneFullBboxDimensionOrientation(const float *bbox,
163 const float *hwl,
164 float *ry,
165 float *center);
178 bool SolveCenterFromNearestVerticalEdge(const float *bbox, const float *hwl,
179 float ry, float *center,
180 float *center_2d) const;
181
182 void UpdateCenterViaBackProjectZ(const float *bbox, const float *hwl,
183 const float *center_2d,
184 float *center) const {
185 float z = center[2];
186 algorithm::IBackprojectCanonical(center_2d, k_mat_, z, center);
187 center[1] += hwl[0] / 2;
188 }
200 float GetProjectionScore(float ry, const float *bbox, const float *hwl,
201 const float *center, bool check_truncation = false,
202 float *bbox_res = nullptr) const {
203 float rot[9] = {0};
204 GenRotMatrix(ry, rot);
205 float *bbox_near = nullptr;
206 float score = GetScoreViaRotDimensionCenter(
207 &k_mat_[0], width_, height_, bbox, rot, hwl, center, check_truncation,
208 bbox_res, bbox_near);
209 return score;
210 }
223 void PostRefineZ(const float *bbox, const float *hwl, const float *center2d,
224 float ry, float *center, float dz = 0.2f, float rz = 1.0f,
225 bool fix_proj = true) const {
226 float z = center[2];
227 float score_best = 0.0f;
228 float x[2] = {center2d[0], center2d[1]};
229 float center_best[3] = {center[0], center[1], center[2]};
230 for (float offset = -rz; offset <= rz; offset += dz) {
231 float z_test = std::max(z + offset, params_.depth_min);
232 float center_test[3] = {center[0], center[1], z_test};
233 if (fix_proj) {
234 algorithm::IBackprojectCanonical(x, k_mat_, z_test, center_test);
235 center_test[1] += hwl[0] / 2;
236 }
237 float score_test = GetProjectionScore(ry, bbox, hwl, center_test);
238 if (score_best < score_test) {
239 score_best = score_test;
240 memcpy(center_best, center_test, sizeof(float) * 3);
241 }
242 }
243 memcpy(center, center_best, sizeof(float) * 3);
244 }
253 void PostRefineOrientation(const float *bbox, const float *hwl,
254 const float *center, float *ry);
265 void GetCenter(const float *bbox, const float &z_ref, const float &ry,
266 const float *hwl, float *center, float *x) const;
272 void FillRyScoreSingleBin(const float &ry) {
273 int nr_bins_ry = static_cast<int>(ry_score_.size());
274 memset(ry_score_.data(), 0, sizeof(float) * nr_bins_ry);
275 const float PI = algorithm::Constant<float>::PI();
276 int index = static_cast<int>(
277 std::floor((ry + PI) * static_cast<float>(nr_bins_ry) / (2.0f * PI)));
278 ry_score_[index % nr_bins_ry] = 1.0f;
279 }
280
281 private:
282 // canonical camera: p_matrix = k_matrix [I 0]
283 float k_mat_[9] = {0};
284 int width_ = 0;
285 int height_ = 0;
286 std::vector<float> ry_score_ = {};
287 ObjMapperParams params_;
288
289 // per-dimension variance of the orientation [yaw, pitch, roll]
290 Eigen::Vector3d orientation_variance_;
291
292 // covariance matrix for position uncertainty
293 Eigen::Matrix3d position_uncertainty_;
294
295 protected:
297};
298
299} // namespace camera
300} // namespace perception
301} // namespace apollo
Eigen::Matrix3d get_position_uncertainty()
Get the position uncertainty object
Definition obj_mapper.h:132
Eigen::Vector3d get_orientation_var()
Get the orientation var object
Definition obj_mapper.h:126
void Init(const float *k_mat, int width, int height)
Init for ObjectTemplateManager
Definition obj_mapper.h:93
void set_default_variance()
Set the default variance object
Definition obj_mapper.h:81
ObjectTemplateManager * object_template_manager_
Definition obj_mapper.h:296
std::vector< float > get_ry_score(float ry)
Get the ry score object
Definition obj_mapper.h:117
void resize_ry_score(int size)
Split the ry_score_ accoring to the specified nr_bins_ry.
Definition obj_mapper.h:104
bool Solve3dBbox(const ObjMapperOptions &options, float center[3], float hwl[3], float *ry)
Calculate 3d result
std::vector< float > get_ry_score()
Get the ry score object
Definition obj_mapper.h:110
void IBackprojectCanonical(const T *x, const T *K, T depth, T *X)
Definition i_util.h:164
void GenRotMatrix(const T &ry, T *rot)
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)
class register implement
Definition arena_queue.h:37
#define PI