Apollo 10.0
自动驾驶开放平台
util.cc
浏览该文件的文档.
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 *****************************************************************************/
17
18#include "cyber/common/log.h"
19
20namespace apollo {
21namespace perception {
22namespace camera {
23
24bool Equal(double x, double target, double eps) {
25 return std::abs(x - target) < eps;
26}
27bool Equal(float x, float target, float eps) {
28 return std::abs(x - target) < eps;
29}
30
31bool LoadAnchors(const std::string &path, std::vector<float> *anchors) {
32 int num_anchors = 0;
33 std::ifstream ifs(path, std::ifstream::in);
34 ifs >> num_anchors;
35 if (!ifs.good()) {
36 AERROR << "Failed to get number of anchors!";
37 return false;
38 }
39 (*anchors).resize(num_anchors * 2);
40 for (int i = 0; i < num_anchors; ++i) {
41 ifs >> (*anchors)[i * 2] >> (*anchors)[i * 2 + 1];
42 if (!ifs.good()) {
43 AERROR << "Failed to load the " << i << "-th anchor!";
44 return false;
45 }
46 }
47 ifs.close();
48 return true;
49}
50
51bool LoadTypes(const std::string &path,
52 std::vector<base::ObjectSubType> *types) {
53 std::ifstream ifs(path, std::ifstream::in);
54 if (!ifs.good()) {
55 AERROR << "Type_list not found: " << path;
56 return false;
57 }
58 std::string type;
59 AINFO << "Supported types: ";
60 while (ifs >> type) {
61 if (base::kName2SubTypeMap.find(type) == base::kName2SubTypeMap.end()) {
62 AERROR << "Invalid type: " << type;
63 return false;
64 }
65 (*types).push_back(base::kName2SubTypeMap.at(type));
66 AINFO << "\t\t" << type;
67 }
68 AINFO << "\t\t" << (*types).size() << " in total.";
69 ifs.close();
70 return true;
71}
72bool LoadExpand(const std::string &path, std::vector<float> *expands) {
73 std::ifstream ifs(path, std::ifstream::in);
74 if (!ifs.good()) {
75 AERROR << "expand_list not found: " << path;
76 return false;
77 }
78 float expand;
79 AINFO << "Expand nums: ";
80 while (ifs >> expand) {
81 expands->push_back(expand);
82 AINFO << "\t\t" << expand;
83 }
84 ifs.close();
85 return true;
86}
87bool ResizeCPU(const base::Blob<uint8_t> &src_blob,
88 std::shared_ptr<base::Blob<float>> dst_blob, int stepwidth,
89 int start_axis) {
90 int width = dst_blob->shape(2);
91 int height = dst_blob->shape(1);
92 int channel = dst_blob->shape(3);
93 int origin_channel = src_blob.shape(3);
94 int origin_height = src_blob.shape(1);
95 int origin_width = src_blob.shape(2);
96 if (origin_channel != dst_blob->shape(3)) {
97 AERROR << "channel should be the same after resize.";
98 return false;
99 }
100 float fx = static_cast<float>(origin_width) / static_cast<float>(width);
101 float fy = static_cast<float>(origin_height) / static_cast<float>(height);
102 auto src = src_blob.cpu_data();
103 auto dst = dst_blob->mutable_cpu_data();
104 for (int dst_y = 0; dst_y < height; dst_y++) {
105 for (int dst_x = 0; dst_x < width; dst_x++) {
106 float src_x = (static_cast<float>(dst_x) + 0.5f) * fx - 0.5f;
107 float src_y = (static_cast<float>(dst_y) + 0.5f) * fy - 0.5f;
108 const int x1 = static_cast<int>(src_x + 0.5);
109 const int y1 = static_cast<int>(src_y + 0.5);
110 const int x1_read = std::max(x1, 0);
111 const int y1_read = std::max(y1, 0);
112 const int x2 = x1 + 1;
113 const int y2 = y1 + 1;
114 const int x2_read = std::min(x2, width - 1);
115 const int y2_read = std::min(y2, height - 1);
116 int src_reg = 0;
117 for (int c = 0; c < channel; c++) {
118 float out = 0.0f;
119
120 int idx11 = (y1_read * stepwidth + x1_read) * channel;
121 src_reg = src[idx11 + c];
122 out = out + (static_cast<float>(x2) - src_x) *
123 (static_cast<float>(y2) - src_y) *
124 static_cast<float>(src_reg);
125 int idx12 = (y1_read * stepwidth + x2_read) * channel;
126 src_reg = src[idx12 + c];
127 out = out + static_cast<float>(src_reg) *
128 (src_x - static_cast<float>(x1)) *
129 (static_cast<float>(y2) - src_y);
130
131 int idx21 = (y2_read * stepwidth + x1_read) * channel;
132 src_reg = src[idx21 + c];
133 out = out + static_cast<float>(src_reg) *
134 (static_cast<float>(x2) - src_x) *
135 (src_y - static_cast<float>(y1));
136
137 int idx22 = (y2_read * stepwidth + x2_read) * channel;
138 src_reg = src[idx22 + c];
139 out = out + static_cast<float>(src_reg) *
140 (src_x - static_cast<float>(x1)) *
141 (src_y - static_cast<float>(y1));
142 if (out < 0) {
143 out = 0;
144 }
145 if (out > 255) {
146 out = 255;
147 }
148 int dst_idx = (dst_y * width + dst_x) * channel + c;
149 // printf("%f %d %d %d %d\n",out,x1,y1,x2,y2);
150 dst[dst_idx] = out;
151 }
152 }
153 }
154 return true;
155}
156
158 if (!object_ptr) {
159 return;
160 }
161 const double length = object_ptr->size(0);
162 const double width = object_ptr->size(1);
163 // const double height = object_ptr->size(2);
164 double x1 = length / 2;
165 double x2 = -x1;
166 double y1 = width / 2;
167 double y2 = -y1;
168 double len = sqrt(object_ptr->direction[0] * object_ptr->direction[0] +
169 object_ptr->direction[1] * object_ptr->direction[1]);
170 double cos_theta = object_ptr->direction[0] / len;
171 double sin_theta = -object_ptr->direction[1] / len;
172
173 object_ptr->polygon.resize(4);
174 object_ptr->polygon[0].x =
175 x1 * cos_theta + y1 * sin_theta + object_ptr->center[0];
176 object_ptr->polygon[0].y =
177 y1 * cos_theta - x1 * sin_theta + object_ptr->center[1];
178 object_ptr->polygon[0].z = 0.0;
179
180 object_ptr->polygon[1].x =
181 x1 * cos_theta + y2 * sin_theta + object_ptr->center[0];
182 object_ptr->polygon[1].y =
183 y2 * cos_theta - x1 * sin_theta + object_ptr->center[1];
184 object_ptr->polygon[1].z = 0.0;
185
186 object_ptr->polygon[2].x =
187 x2 * cos_theta + y2 * sin_theta + object_ptr->center[0];
188 object_ptr->polygon[2].y =
189 y2 * cos_theta - x2 * sin_theta + object_ptr->center[1];
190 object_ptr->polygon[2].z = 0.0;
191
192 object_ptr->polygon[3].x =
193 x2 * cos_theta + y1 * sin_theta + object_ptr->center[0];
194 object_ptr->polygon[3].y =
195 y1 * cos_theta - x2 * sin_theta + object_ptr->center[1];
196 object_ptr->polygon[3].z = 0.0;
197}
198
199} // namespace camera
200} // namespace perception
201} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
const Dtype * cpu_data() const
Definition blob.cc:137
const std::vector< int > & shape() const
Definition blob.h:130
virtual void resize(size_t size)
Definition point_cloud.h:88
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
const std::map< std::string, ObjectSubType > kName2SubTypeMap
bool Equal(double x, double target, double eps)
Definition util.cc:24
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
bool LoadTypes(const std::string &path, std::vector< base::ObjectSubType > *types)
Definition util.cc:51
void FillObjectPolygonFromBBox3D(base::Object *object_ptr)
Definition util.cc:157
bool LoadAnchors(const std::string &path, std::vector< float > *anchors)
Definition util.cc:31
bool LoadExpand(const std::string &path, std::vector< float > *expands)
Definition util.cc:72
class register implement
Definition arena_queue.h:37
Eigen::Vector3f direction
Definition object.h:47
PointCloud< PointD > polygon
Definition object.h:43