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