|
| 1 | +// Copyright 2026 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "perception_utils/iou_bev_nms.hpp" |
| 16 | + |
| 17 | +#include <autoware/object_recognition_utils/geometry.hpp> |
| 18 | +#include <autoware/object_recognition_utils/object_recognition_utils.hpp> |
| 19 | +#include <autoware_utils_geometry/geometry.hpp> |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <cmath> |
| 23 | +#include <stdexcept> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace perception_utils |
| 27 | +{ |
| 28 | +namespace |
| 29 | +{ |
| 30 | + |
| 31 | +using DetectedObject = autoware_perception_msgs::msg::DetectedObject; |
| 32 | +using Label = autoware_perception_msgs::msg::ObjectClassification; |
| 33 | + |
| 34 | +} // namespace |
| 35 | + |
| 36 | +void IouBevNms::setParameters(const IouBevNmsParams & params) |
| 37 | +{ |
| 38 | + if (!std::isfinite(params.search_distance_2d) || params.search_distance_2d < 0.0) { |
| 39 | + throw std::invalid_argument("search_distance_2d must be a finite non-negative value."); |
| 40 | + } |
| 41 | + if ( |
| 42 | + !std::isfinite(params.iou_threshold) || params.iou_threshold < 0.0 || |
| 43 | + params.iou_threshold > 1.0) { |
| 44 | + throw std::invalid_argument("iou_threshold must be a finite value between 0 and 1."); |
| 45 | + } |
| 46 | + |
| 47 | + params_ = params; |
| 48 | + search_distance_2d_sq_ = params.search_distance_2d * params.search_distance_2d; |
| 49 | +} |
| 50 | + |
| 51 | +bool IouBevNms::isTargetPairObject( |
| 52 | + const DetectedObject & object1, const DetectedObject & object2) const |
| 53 | +{ |
| 54 | + const auto label1 = |
| 55 | + autoware::object_recognition_utils::getHighestProbLabel(object1.classification); |
| 56 | + const auto label2 = |
| 57 | + autoware::object_recognition_utils::getHighestProbLabel(object2.classification); |
| 58 | + |
| 59 | + if (label1 != label2 && (label1 == Label::PEDESTRIAN || label2 == Label::PEDESTRIAN)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + const auto sqr_dist_2d = autoware_utils_geometry::calc_squared_distance2d( |
| 64 | + autoware::object_recognition_utils::getPose(object1), |
| 65 | + autoware::object_recognition_utils::getPose(object2)); |
| 66 | + return sqr_dist_2d <= search_distance_2d_sq_; |
| 67 | +} |
| 68 | + |
| 69 | +std::vector<DetectedObject> IouBevNms::apply( |
| 70 | + const std::vector<DetectedObject> & input_objects, const bool sort) const |
| 71 | +{ |
| 72 | + std::vector<DetectedObject> ordered_objects = input_objects; |
| 73 | + if (sort) { |
| 74 | + std::stable_sort( |
| 75 | + ordered_objects.begin(), ordered_objects.end(), [](const auto & lhs, const auto & rhs) { |
| 76 | + return lhs.existence_probability > rhs.existence_probability; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + std::vector<DetectedObject> output_objects; |
| 81 | + output_objects.reserve(ordered_objects.size()); |
| 82 | + for (std::size_t target_i = 0; target_i < ordered_objects.size(); ++target_i) { |
| 83 | + double max_iou = 0.0; |
| 84 | + for (std::size_t source_i = 0; source_i < target_i; ++source_i) { |
| 85 | + const auto & target_object = ordered_objects.at(target_i); |
| 86 | + const auto & source_object = ordered_objects.at(source_i); |
| 87 | + if (!isTargetPairObject(target_object, source_object)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + const double iou = autoware::object_recognition_utils::get2dIoU(target_object, source_object); |
| 92 | + max_iou = std::max(max_iou, iou); |
| 93 | + if (iou > params_.iou_threshold) { |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (max_iou <= params_.iou_threshold) { |
| 99 | + output_objects.emplace_back(ordered_objects.at(target_i)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return output_objects; |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace perception_utils |
0 commit comments