19 #include "opencv2/core.hpp"
29 float getWidth()
const {
30 return (right - left) + 1.0f;
32 float getHeight()
const {
33 return (bottom - top) + 1.0f;
35 float getXCenter()
const {
36 return left + (getWidth() - 1.0f) / 2.0f;
38 float getYCenter()
const {
39 return top + (getHeight() - 1.0f) / 2.0f;
43 template <
typename Anchor>
44 std::vector<int> nms(
const std::vector<Anchor>& boxes,
const std::vector<float>& scores,
45 const float thresh,
bool includeBoundaries=
false) {
46 std::vector<float> areas(boxes.size());
47 for (
size_t i = 0; i < boxes.size(); ++i) {
48 areas[i] = (boxes[i].right - boxes[i].left + includeBoundaries) * (boxes[i].bottom - boxes[i].top + includeBoundaries);
50 std::vector<int> order(scores.size());
51 std::iota(order.begin(), order.end(), 0);
52 std::sort(order.begin(), order.end(), [&scores](
int o1,
int o2) { return scores[o1] > scores[o2]; });
55 for (; ordersNum < order.size() && scores[order[ordersNum]] >= 0; ordersNum++);
57 std::vector<int> keep;
58 bool shouldContinue =
true;
59 for (
size_t i = 0; shouldContinue && i < ordersNum; ++i) {
63 shouldContinue =
false;
64 for (
size_t j = i + 1; j < ordersNum; ++j) {
67 shouldContinue =
true;
68 auto overlappingWidth = std::fminf(boxes[idx1].right, boxes[idx2].right) - std::fmaxf(boxes[idx1].left, boxes[idx2].left);
69 auto overlappingHeight = std::fminf(boxes[idx1].bottom, boxes[idx2].bottom) - std::fmaxf(boxes[idx1].top, boxes[idx2].top);
70 auto intersection = overlappingWidth > 0 && overlappingHeight > 0 ? overlappingWidth * overlappingHeight : 0;
71 auto overlap = intersection / (areas[idx1] + areas[idx2] - intersection);
73 if (overlap >= thresh) {