Apollo 11.0
自动驾驶开放平台
cropbox.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 <algorithm>
19
21
22namespace apollo {
23namespace perception {
24namespace trafficlight {
25
26void CropBox::getCropBox(const int width, const int height, const base::TrafficLightPtr &light, base::RectI *crop_box) {
27 int rows = height;
28 int cols = width;
29
30 if (camera::OutOfValidRegion(light->region.projection_roi, width, height)
31 || light->region.projection_roi.Area() <= 0) {
32 crop_box->x = 0;
33 crop_box->y = 0;
34 crop_box->width = 0;
35 crop_box->height = 0;
36 return;
37 }
38
39 int xl = light->region.projection_roi.x;
40 int yt = light->region.projection_roi.y;
41 int xr = xl + light->region.projection_roi.width - 1;
42 int yb = yt + light->region.projection_roi.height - 1;
43
44 // scale
45 int center_x = (xr + xl) / 2;
46 int center_y = (yb + yt) / 2;
47 int resize = static_cast<int>(
48 crop_scale_
49 * static_cast<float>(std::max(light->region.projection_roi.width, light->region.projection_roi.height)));
50
51 resize = std::max(resize, min_crop_size_);
52 resize = std::min(resize, width);
53 resize = std::min(resize, height);
54
55 // clamp
56 xl = center_x - resize / 2 + 1;
57 xl = (xl < 0) ? 0 : xl;
58 yt = center_y - resize / 2 + 1;
59 yt = (yt < 0) ? 0 : yt;
60 xr = xl + resize - 1;
61 yb = yt + resize - 1;
62 if (xr >= cols - 1) {
63 xl -= xr - cols + 1;
64 xr = cols - 1;
65 }
66
67 if (yb >= rows - 1) {
68 yt -= yb - rows + 1;
69 yb = rows - 1;
70 }
71
72 crop_box->x = xl;
73 crop_box->y = yt;
74 crop_box->width = xr - xl + 1;
75 crop_box->height = yb - yt + 1;
76}
77void CropBox::Init(float crop_scale, int min_crop_size) {
78 crop_scale_ = crop_scale;
79 min_crop_size_ = min_crop_size;
80}
81CropBox::CropBox(float crop_scale, int min_crop_size) {
82 Init(crop_scale, min_crop_size);
83}
85 const int width,
86 const int height,
87 const base::TrafficLightPtr &light,
88 base::RectI *crop_box) {
89 if (!camera::OutOfValidRegion(light->region.projection_roi, width, height)
90 && light->region.projection_roi.Area() > 0) {
91 crop_box->x = crop_box->y = 0;
92 crop_box->width = width;
93 crop_box->height = height;
94 return;
95 }
96
97 crop_box->x = 0;
98 crop_box->y = 0;
99 crop_box->width = 0;
100 crop_box->height = 0;
101}
102
103} // namespace trafficlight
104} // namespace perception
105} // namespace apollo
virtual void getCropBox(const int width, const int height, const base::TrafficLightPtr &light, base::RectI *crop_box)
Get the crop box object.
Definition cropbox.cc:84
virtual void getCropBox(const int width, const int height, const base::TrafficLightPtr &light, base::RectI *crop_box)
Get the crop box object.
Definition cropbox.cc:26
CropBox(float crop_scale, int min_crop_size)
Construct a new crop box object.
Definition cropbox.cc:81
void Init(float crop_scale, int min_crop_size)
Initialize crop box object parameters.
Definition cropbox.cc:77
std::shared_ptr< TrafficLight > TrafficLightPtr
bool OutOfValidRegion(const base::BBox2D< T > box, const T width, const T height, const T border_size=0)
Definition util.h:74
class register implement
Definition arena_queue.h:37