Apollo  v5.5.0
Open source self driving car software
util.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 <fstream>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "cyber/common/log.h"
29 
30 namespace apollo {
31 namespace perception {
32 namespace camera {
33 
34 bool Equal(float x, float target, float eps = 1e-6f);
35 bool Equal(double x, double target, double eps = 1e-6);
36 
37 // @brief whether rect1 is covered by rect2
38 template <typename T>
39 bool IsCovered(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
40  float thresh) {
41  base::RectF inter = rect1 & rect2;
42  return inter.Area() / rect1.Area() > thresh;
43 }
44 template <typename T>
45 bool IsCoveredHorizon(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
46  float thresh) {
47  base::RectF inter = rect1 & rect2;
48  if (inter.Area() > 0) {
49  return inter.width / rect1.width > thresh;
50  }
51  return false;
52 }
53 template <typename T>
54 bool IsCoveredVertical(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
55  float thresh) {
56  base::RectF inter = rect1 & rect2;
57  if (inter.Area() > 0) {
58  return inter.height / rect1.height > thresh;
59  }
60  return false;
61 }
62 
63 template <typename T>
64 bool Contain(const std::vector<T> &array, const T &element) {
65  for (const auto &item : array) {
66  if (item == element) {
67  return true;
68  }
69  }
70  return false;
71 }
72 
73 template <typename T>
74 bool OutOfValidRegion(const base::BBox2D<T> box, const T width, const T height,
75  const T border_size = 0) {
76  if (box.xmin < border_size || box.ymin < border_size) {
77  return true;
78  }
79  if (box.xmax + border_size > width || box.ymax + border_size > height) {
80  return true;
81  }
82  return false;
83 }
84 template <typename T>
85 bool OutOfValidRegion(const base::Rect<T> rect, const T width, const T height,
86  const T border_size = 0) {
87  base::BBox2D<T> box(rect);
88  return OutOfValidRegion(box, width, height, border_size);
89 }
90 
91 template <typename T>
92 void RefineBox(const base::Rect<T> &box_in, const T width, const T height,
93  base::Rect<T> *box_out) {
94  if (!box_out) {
95  return;
96  }
97  *box_out = box_in;
98  if (box_out->x < 0) {
99  box_out->width += box_out->x;
100  box_out->x = 0;
101  }
102  if (box_out->y < 0) {
103  box_out->height += box_out->y;
104  box_out->y = 0;
105  }
106  if (box_out->x >= width) {
107  box_out->x = 0;
108  box_out->width = 0;
109  }
110  if (box_out->y >= height) {
111  box_out->y = 0;
112  box_out->height = 0;
113  }
114  box_out->width = (box_out->x + box_out->width <= width) ? box_out->width
115  : width - box_out->x;
116  box_out->height = (box_out->y + box_out->height <= height)
117  ? box_out->height
118  : height - box_out->y;
119  if (box_out->width < 0) {
120  box_out->width = 0;
121  }
122  if (box_out->height < 0) {
123  box_out->height = 0;
124  }
125 }
126 
127 template <typename T>
128 void RefineBox(const base::BBox2D<T> &box_in, const T width, const T height,
129  base::BBox2D<T> *box_out) {
130  if (!box_out) {
131  return;
132  }
133  base::Rect<T> rect;
134  RefineBox(base::Rect<T>(box_in), width, height, &rect);
135  *box_out = base::BBox2D<T>(rect);
136 }
137 
138 bool LoadAnchors(const std::string &path, std::vector<float> *anchors);
139 bool LoadTypes(const std::string &path,
140  std::vector<base::ObjectSubType> *types);
141 bool LoadExpand(const std::string &path, std::vector<float> *expands);
142 
143 bool ResizeCPU(const base::Blob<uint8_t> &src_gpu,
144  std::shared_ptr<base::Blob<float>> dst, int stepwidth,
145  int start_axis);
146 
147 std::string GetCyberWorkRoot();
148 void FillObjectPolygonFromBBox3D(base::Object *object_ptr);
149 
150 template <typename T>
151 void CalculateMeanAndVariance(const std::vector<T> &data, T *mean,
152  T *variance) {
153  if (!mean || !variance) {
154  return;
155  }
156  if (data.empty()) {
157  *mean = 0;
158  *variance = 0;
159  return;
160  }
161  T sum = std::accumulate(data.begin(), data.end(), static_cast<T>(0));
162  *mean = sum / data.size();
163 
164  std::vector<T> diff(data.size());
165  std::transform(data.begin(), data.end(), diff.begin(),
166  [mean](T x) { return x - *mean; });
167  T sum_of_diff_sqrs = std::inner_product(diff.begin(), diff.end(),
168  diff.begin(), static_cast<T>(0));
169  *variance = sum_of_diff_sqrs / data.size();
170 }
171 
172 } // namespace camera
173 } // namespace perception
174 } // namespace apollo
bool IsCoveredHorizon(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:45
T ymin
Definition: box.h:154
void FillObjectPolygonFromBBox3D(base::Object *object_ptr)
T width
Definition: box.h:119
bool LoadExpand(const std::string &path, std::vector< float > *expands)
Definition: blob.h:72
bool LoadAnchors(const std::string &path, std::vector< float > *anchors)
void CalculateMeanAndVariance(const std::vector< T > &data, T *mean, T *variance)
Definition: util.h:151
T x
Definition: box.h:117
void RefineBox(const base::Rect< T > &box_in, const T width, const T height, base::Rect< T > *box_out)
Definition: util.h:92
T y
Definition: box.h:118
bool ResizeCPU(const base::Blob< uint8_t > &src_gpu, std::shared_ptr< base::Blob< float >> dst, int stepwidth, int start_axis)
Definition: object.h:34
bool OutOfValidRegion(const base::BBox2D< T > box, const T width, const T height, const T border_size=0)
Definition: util.h:74
bool LoadTypes(const std::string &path, std::vector< base::ObjectSubType > *types)
bool Equal(float x, float target, float eps=1e-6f)
bool IsCoveredVertical(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:54
T ymax
Definition: box.h:156
T height
Definition: box.h:120
T xmin
Definition: box.h:153
T xmax
Definition: box.h:155
std::string GetCyberWorkRoot()
Definition: box.h:33
bool IsCovered(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:39
float diff(Image< float > *I, int x1, int y1, int x2, int y2)
Definition: segment_image.h:44
T Area() const
Definition: box.h:66
bool Contain(const std::vector< T > &array, const T &element)
Definition: util.h:64