Apollo 10.0
自动驾驶开放平台
util.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 <fstream>
20#include <memory>
21#include <numeric>
22#include <string>
23#include <vector>
24
29
30namespace apollo {
31namespace perception {
32namespace camera {
33
34bool Equal(float x, float target, float eps = 1e-6f);
35bool Equal(double x, double target, double eps = 1e-6);
36
37// @brief whether rect1 is covered by rect2
38template <typename T>
39bool 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}
44template <typename T>
45bool 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}
53template <typename T>
54bool 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
63template <typename T>
64bool 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
73template <typename T>
74bool 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}
84template <typename T>
85bool 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
91template <typename T>
92void 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
127template <typename T>
128void 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
138bool LoadAnchors(const std::string &path, std::vector<float> *anchors);
139bool LoadTypes(const std::string &path,
140 std::vector<base::ObjectSubType> *types);
141bool LoadExpand(const std::string &path, std::vector<float> *expands);
142
143bool ResizeCPU(const base::Blob<uint8_t> &src_gpu,
144 std::shared_ptr<base::Blob<float>> dst, int stepwidth,
145 int start_axis);
146
148
149template <typename T>
150void CalculateMeanAndVariance(const std::vector<T> &data, T *mean,
151 T *variance) {
152 if (!mean || !variance) {
153 return;
154 }
155 if (data.empty()) {
156 *mean = 0;
157 *variance = 0;
158 return;
159 }
160 T sum = std::accumulate(data.begin(), data.end(), static_cast<T>(0));
161 *mean = sum / data.size();
162
163 std::vector<T> diff(data.size());
164 std::transform(data.begin(), data.end(), diff.begin(),
165 [mean](T x) { return x - *mean; });
166 T sum_of_diff_sqrs = std::inner_product(diff.begin(), diff.end(),
167 diff.begin(), static_cast<T>(0));
168 *variance = sum_of_diff_sqrs / data.size();
169}
170
171} // namespace camera
172} // namespace perception
173} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
Rect< float > RectF
Definition box.h:160
bool Equal(double x, double target, double eps)
Definition util.cc:24
bool Contain(const std::vector< T > &array, const T &element)
Definition util.h:64
bool IsCoveredVertical(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition util.h:54
bool ResizeCPU(const base::Blob< uint8_t > &src_blob, std::shared_ptr< base::Blob< float > > dst_blob, int stepwidth, int start_axis)
Definition util.cc:87
void CalculateMeanAndVariance(const std::vector< T > &data, T *mean, T *variance)
Definition util.h:150
bool LoadTypes(const std::string &path, std::vector< base::ObjectSubType > *types)
Definition util.cc:51
bool IsCoveredHorizon(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition util.h:45
void FillObjectPolygonFromBBox3D(base::Object *object_ptr)
Definition util.cc:157
bool OutOfValidRegion(const base::BBox2D< T > box, const T width, const T height, const T border_size=0)
Definition util.h:74
void RefineBox(const base::Rect< T > &box_in, const T width, const T height, base::Rect< T > *box_out)
Definition util.h:92
bool LoadAnchors(const std::string &path, std::vector< float > *anchors)
Definition util.cc:31
bool IsCovered(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition util.h:39
bool LoadExpand(const std::string &path, std::vector< float > *expands)
Definition util.cc:72
class register implement
Definition arena_queue.h:37