Apollo 11.0
自动驾驶开放平台
histogram_estimator.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 <cassert>
20#include <vector>
21
22#include "cyber/common/log.h"
23
24namespace apollo {
25namespace perception {
26namespace camera {
27
28// histogram related params
31 void Init();
32
34 float data_sp = 0.0f; // start point
35 float data_ep = 0.0f; // end point => [data_sp, data_ep)
36 float step_bin = 0.0f;
37
38 std::vector<uint32_t> smooth_kernel = {};
39 int smooth_kernel_width = 0; // only consider odd number
41
42 float hat_min_allowed = 0.0f;
43 float hat_std_allowed = 0.0f;
44
46
47 float decay_factor = 0.0f;
48
49 void operator=(const HistogramEstimatorParams &params) {
51
52 data_sp = params.data_sp;
53 data_ep = params.data_ep;
54 assert(nr_bins_in_histogram > 0 && data_ep > data_sp);
55
56 step_bin = (data_ep - data_sp) / static_cast<float>(nr_bins_in_histogram);
57
58 int w = static_cast<int>(params.smooth_kernel.size());
59 assert(w >= 1);
62 memcpy(smooth_kernel.data(), params.smooth_kernel.data(),
63 sizeof(uint32_t) * smooth_kernel_width);
65
68
70 assert(histogram_mass_limit > 0);
71
73 assert(decay_factor > 0.0f);
74 }
75};
76
77// histogram and its process
79 public:
80 static const int kMaxNrBins = 1000;
81
83 hist_.resize(kMaxNrBins);
84 std::fill(hist_.begin(), hist_.end(), 0);
85
86 hist_buffer_.resize(kMaxNrBins);
87 std::fill(hist_buffer_.begin(), hist_buffer_.end(), 0);
88
89 hist_hat_.resize(kMaxNrBins);
90 std::fill(hist_hat_.begin(), hist_hat_.end(), 0.0f);
91
92 Init();
93 }
94
96
97 void Init(const HistogramEstimatorParams *params = nullptr);
98
99 bool Push(float val) {
100 if (val < params_.data_sp || val >= params_.data_ep) {
101 AERROR << "Input data to histogram: out-of-range";
102 AERROR << params_.data_sp;
103 AERROR << params_.data_ep;
104 AERROR << val;
105 return false;
106 }
107 int index = GetIndex(val);
108 if (index < 0 || index >= params_.nr_bins_in_histogram) {
109 return false;
110 }
111 ++hist_[index];
112 val_cur_ = val;
113 return true;
114 }
115
116 void Clear() {
117 std::fill(hist_.begin(), hist_.end(), 0);
118 val_cur_ = val_estimation_ = 0.0f;
119 }
120
121 const std::vector<uint32_t> &get_hist() const { return hist_; }
122
123 float get_val_cur() const { return val_cur_; }
124
125 float get_val_estimation() const { return val_estimation_; }
126
127 uint32_t get_bin_value(int i) const {
128 assert(i >= 0 && i < params_.nr_bins_in_histogram);
129 return hist_[i];
130 }
131
132 // main function:
133 // smooth -> shape analysis -> update value -> decay
134 bool Process();
135
136 private:
137 int GetIndex(float val) const {
138 return static_cast<int>((val - params_.data_sp) * step_bin_reversed_);
139 }
140
141 float GetValFromIndex(int index) const {
142 return (params_.data_sp +
143 (static_cast<float>(index) + 0.5f) * params_.step_bin);
144 }
145
146 void Smooth(const uint32_t *hist_input, int nr_bins, uint32_t *hist_output);
147
148 bool IsGoodShape(const uint32_t *hist, int nr_bins, int max_index);
149
150 void GetPeakIndexAndMass(const uint32_t *hist, int nr_bins, int *index,
151 uint32_t *mass);
152
153 void Decay(uint32_t *hist, int nr_bins) {
154 float df = params_.decay_factor;
155 for (int i = 0; i < nr_bins; ++i) {
156 hist[i] = static_cast<uint32_t>(static_cast<float>(hist[i]) * df);
157 }
158 }
159
160 void GenerateHat(float *hist_hat, int nr_bins);
161
162 // bool SaveHist(const std::string &filename,
163 // const uint32_t *hist, int nr_bins);
164
165 private:
166 std::vector<uint32_t> hist_ = {};
167 std::vector<uint32_t> hist_buffer_ = {};
168
169 std::vector<float> hist_hat_ = {};
170
171 HistogramEstimatorParams params_;
172
173 float step_bin_reversed_ = 0.0f;
174 float val_cur_ = 0.0f;
175 float val_estimation_ = 0.0f;
176};
177
178} // namespace camera
179} // namespace perception
180} // namespace apollo
void Init(const HistogramEstimatorParams *params=nullptr)
const std::vector< uint32_t > & get_hist() const
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
void operator=(const HistogramEstimatorParams &params)