28 namespace perception {
33 static const double lane_eps_value = 1e-6;
34 static const int max_poly_order = 3;
46 template <
typename Dtype>
47 bool PolyFit(
const std::vector<Eigen::Matrix<Dtype, 2, 1>>& pos_vec,
49 Eigen::Matrix<Dtype, max_poly_order + 1, 1>* coeff,
50 const bool& is_x_axis =
true) {
51 if (coeff ==
nullptr) {
52 AERROR <<
"The coefficient pointer is NULL.";
56 if (order > max_poly_order) {
57 AERROR <<
"The order of polynomial must be smaller than " << max_poly_order;
61 int n =
static_cast<int>(pos_vec.size());
63 AERROR <<
"The number of points should be larger than the order. #points = " 69 Eigen::Matrix<Dtype, Eigen::Dynamic, Eigen::Dynamic>
A(n, order + 1);
70 Eigen::Matrix<Dtype, Eigen::Dynamic, 1>
y(n);
71 Eigen::Matrix<Dtype, Eigen::Dynamic, 1> result;
72 for (
int i = 0; i < n; ++i) {
73 for (
int j = 0; j <= order; ++j) {
74 A(i, j) =
static_cast<Dtype
>(
75 std::pow(is_x_axis ? pos_vec[i].
x() : pos_vec[i].
y(), j));
77 y(i) = is_x_axis ? pos_vec[i].y() : pos_vec[i].x();
81 result = A.householderQr().solve(y);
82 assert(result.size() == order + 1);
84 for (
int j = 0; j <= max_poly_order; ++j) {
85 (*coeff)(j) = (j <= order) ? result(j) :
static_cast<Dtype
>(0);
91 template <
typename Dtype>
93 const Eigen::Matrix<Dtype, max_poly_order + 1, 1>& coeff,
95 int poly_order = order;
96 if (order > max_poly_order) {
97 AERROR <<
"the order of polynomial function must be smaller than " 102 *y =
static_cast<Dtype
>(0);
103 for (
int j = 0; j <= poly_order; ++j) {
104 *y +=
static_cast<Dtype
>(coeff(j) * std::pow(x, j));
111 template <
typename Dtype>
113 std::vector<Eigen::Matrix<Dtype, 2, 1>>* selected_points,
114 Eigen::Matrix<Dtype, 4, 1>* coeff,
const int max_iters = 100,
116 const Dtype inlier_thres = static_cast<Dtype>(0.1)) {
117 if (coeff ==
nullptr) {
118 AERROR <<
"The coefficient pointer is NULL.";
122 selected_points->clear();
124 int n =
static_cast<int>(pos_vec.size());
125 int q1 =
static_cast<int>(n / 4);
126 int q2 =
static_cast<int>(n / 2);
127 int q3 =
static_cast<int>(n * 3 / 4);
129 AERROR <<
"The number of points should be larger than the order. #points = " 134 std::vector<int> index(3, 0);
136 Dtype min_residual = std::numeric_limits<float>::max();
137 Dtype early_stop_ratio = 0.95f;
138 Dtype good_lane_ratio = 0.666f;
139 for (
int j = 0; j < max_iters; ++j) {
140 index[0] = std::rand() % q2;
141 index[1] = q2 + std::rand() % q1;
142 index[2] = q3 + std::rand() % q1;
144 Eigen::Matrix<Dtype, 3, 3> matA;
145 matA << pos_vec[index[0]](0) * pos_vec[index[0]](0), pos_vec[index[0]](0),
146 1, pos_vec[index[1]](0) * pos_vec[index[1]](0), pos_vec[index[1]](0), 1,
147 pos_vec[index[2]](0) * pos_vec[index[2]](0), pos_vec[index[2]](0), 1;
149 Eigen::FullPivLU<Eigen::Matrix<Dtype, 3, 3>> mat(matA);
150 mat.setThreshold(1e-5f);
151 if (mat.rank() < 3) {
152 ADEBUG <<
"matA: " << matA;
153 ADEBUG <<
"Matrix is not full rank (3). The rank is: " << mat.rank();
159 Eigen::Matrix<Dtype, 3, 1> matB;
160 matB << pos_vec[index[0]](1), pos_vec[index[1]](1), pos_vec[index[2]](1);
162 static_cast<Eigen::Matrix<Dtype, 3, 1>
>(matA.inverse() * matB);
163 if (!(matA * c).isApprox(matB)) {
171 for (
int i = 0; i < n; ++i) {
172 y = pos_vec[i](0) * pos_vec[i](0) * c(0) + pos_vec[i](0) * c(1) + c(2);
173 if (
std::abs(y - pos_vec[i](1)) <= inlier_thres) ++num_inliers;
174 residual +=
std::abs(y - pos_vec[i](1));
177 if (num_inliers > max_inliers ||
178 (num_inliers == max_inliers && residual < min_residual)) {
183 max_inliers = num_inliers;
184 min_residual = residual;
187 if (max_inliers > early_stop_ratio * n)
break;
190 if (static_cast<Dtype>(max_inliers) / n < good_lane_ratio)
return false;
194 for (
int i = 0; i < n; ++i) {
195 Dtype
y = pos_vec[i](0) * pos_vec[i](0) * (*coeff)(2) +
196 pos_vec[i](0) * (*coeff)(1) + (*coeff)(0);
197 if (
std::abs(y - pos_vec[i](1)) <= inlier_thres) {
198 selected_points->push_back(pos_vec[i]);
207 explicit DisjointSet(
size_t size) : disjoint_array_(), subset_num_(0) {
208 disjoint_array_.reserve(size);
213 disjoint_array_.clear();
214 disjoint_array_.reserve(size);
219 disjoint_array_.clear();
225 void Unite(
int x,
int y);
226 int Size()
const {
return subset_num_; }
227 size_t Num()
const {
return disjoint_array_.size(); }
230 std::vector<int> disjoint_array_;
242 pixels_.push_back(point);
252 void AddPixel(
int x,
int y);
255 std::vector<base::Point2DI>
GetPixels()
const {
return pixels_; }
259 std::vector<base::Point2DI> pixels_;
263 bool FindCC(
const std::vector<unsigned char>& src,
int width,
int height,
264 const base::RectI& roi, std::vector<ConnectedComponent>* cc);
267 float camera_ground_height,
286 void QuickSort(
int* index,
const T* values,
int start,
int end) {
287 if (start >= end - 1) {
291 const T& pivot = values[index[(start + end - 1) / 2]];
298 while (lo < hi && values[index[lo]] < pivot) {
301 while (lo < hi && values[index[hi - 1]] >= pivot) {
304 if (lo == hi || lo == hi - 1) {
307 QSwap_(&(index[lo]), &(index[hi - 1]));
317 while (lo < hi && values[index[lo]] == pivot) {
320 while (lo < hi && values[index[hi - 1]] > pivot) {
323 if (lo == hi || lo == hi - 1) {
326 QSwap_(&(index[lo]), &(index[hi - 1]));
336 void QuickSort(
int* index,
const T* values,
int nsize) {
337 for (
int ii = 0; ii < nsize; ii++) {
342 bool FindKSmallValue(
const float* distance,
int dim,
int k,
int* index);
344 bool FindKLargeValue(
const float* distance,
int dim,
int k,
int* index);
float score
Definition: common_functions.h:39
int Size() const
Definition: common_functions.h:226
int GetPixelCount() const
Definition: common_functions.h:253
bool FindKLargeValue(const float *distance, int dim, int k, int *index)
Definition: common_functions.h:36
bool RansacFitting(const std::vector< Eigen::Matrix< Dtype, 2, 1 >> &pos_vec, std::vector< Eigen::Matrix< Dtype, 2, 1 >> *selected_points, Eigen::Matrix< Dtype, 4, 1 > *coeff, const int max_iters=100, const int N=5, const Dtype inlier_thres=static_cast< Dtype >(0.1))
Definition: common_functions.h:112
base::BBox2DI GetBBox() const
Definition: common_functions.h:254
bool CameraPoint2Image(const Eigen::Vector3d &camera_point, const Eigen::Matrix3f &intrinsic_params, base::Point2DF *img_point)
ConnectedComponent(int x, int y)
Definition: common_functions.h:238
bool FindCC(const std::vector< unsigned char > &src, int width, int height, const base::RectI &roi, std::vector< ConnectedComponent > *cc)
Eigen::Vector3f Vector3f
Definition: base_map_fwd.h:29
Definition: common_functions.h:234
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
DisjointSet()
Definition: common_functions.h:206
float x
Definition: common_functions.h:41
const size_t A
Definition: util.h:160
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
void Init(size_t size)
Definition: common_functions.h:212
std::vector< base::Point2DI > GetPixels() const
Definition: common_functions.h:255
T abs(const T &x)
Definition: misc.h:48
bool PolyEval(const Dtype &x, int order, const Eigen::Matrix< Dtype, max_poly_order+1, 1 > &coeff, Dtype *y)
Definition: common_functions.h:92
bool FindKSmallValue(const float *distance, int dim, int k, int *index)
T x
Definition: point.h:83
void Reset()
Definition: common_functions.h:218
Eigen::Matrix3f Matrix3f
Definition: base_map_fwd.h:27
T y
Definition: point.h:84
Definition: common_functions.h:204
#define ADEBUG
Definition: log.h:41
float y
Definition: common_functions.h:43
bool ComparePoint2DY(const base::Point2DF &point1, const base::Point2DF &point2)
DisjointSet(size_t size)
Definition: common_functions.h:207
int type
Definition: common_functions.h:37
ConnectedComponent()
Definition: common_functions.h:236
std::vector< base::Point2DF > Point2DSet
Definition: common_functions.h:31
bool ImagePoint2Camera(const base::Point2DF &img_point, float pitch_angle, float camera_ground_height, const Eigen::Matrix3f &intrinsic_params_inverse, Eigen::Vector3d *camera_point)
void QuickSort(int *index, const T *values, int start, int end)
Definition: common_functions.h:286
void QSwap_(T *a, T *b)
Definition: common_functions.h:279
#define AERROR
Definition: log.h:44
~DisjointSet()
Definition: common_functions.h:210
std::vector< base::Point3DF > Point3DSet
Definition: common_functions.h:30
size_t Num() const
Definition: common_functions.h:227
bool PolyFit(const std::vector< Eigen::Matrix< Dtype, 2, 1 >> &pos_vec, const int &order, Eigen::Matrix< Dtype, max_poly_order+1, 1 > *coeff, const bool &is_x_axis=true)
Definition: common_functions.h:47
~ConnectedComponent()
Definition: common_functions.h:249