NVIDIA DeepStream SDK API Reference

6.4 Release
BBox.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA CORPORATION and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA CORPORATION is strictly prohibited.
9  */
10 
11 #ifndef CVCORE_BBOX_H
12 #define CVCORE_BBOX_H
13 
14 #include <algorithm>
15 #include <stdexcept>
16 #include <utility>
17 
18 namespace cvcore {
19 
24 struct BBox
25 {
26  int xmin{0};
27  int ymin{0};
28  int xmax{0};
29  int ymax{0};
36  BBox clamp(const BBox &clampBox) const
37  {
38  BBox outbox;
39  outbox.xmin = std::max(clampBox.xmin, xmin);
40  outbox.xmax = std::min(clampBox.xmax, xmax);
41  outbox.ymin = std::max(clampBox.ymin, ymin);
42  outbox.ymax = std::min(clampBox.ymax, ymax);
43  return outbox;
44  }
45 
49  size_t getWidth() const
50  {
51  return xmax - xmin;
52  }
53 
57  size_t getHeight() const
58  {
59  return ymax - ymin;
60  }
61 
65  bool isValid() const
66  {
67  return (xmin < xmax) && (ymin < ymax) && (getWidth() > 0) && (getHeight() > 0);
68  }
69 
74  std::pair<int, int> getCenter() const
75  {
76  int centerX = xmin + getWidth() / 2;
77  int centerY = ymin + getHeight() / 2;
78  return std::pair<int, int>(centerX, centerY);
79  }
80 
87  BBox scale(float scaleW, float scaleH) const
88  {
89  auto center = getCenter();
90  float newW = getWidth() * scaleW;
91  float newH = getHeight() * scaleH;
92  BBox outbox;
93  outbox.xmin = center.first - newW / 2;
94  outbox.xmax = center.first + newW / 2;
95  outbox.ymin = center.second - newH / 2;
96  outbox.ymax = center.second + newH / 2;
97 
98  return outbox;
99  }
100 
107  BBox squarify(const BBox &clampBox) const
108  {
109  size_t w = getWidth();
110  size_t h = getHeight();
111 
112  BBox clampedBox1 = clamp(clampBox);
113  if (!clampedBox1.isValid())
114  {
115  throw std::range_error("Invalid bounding box generated\n");
116  }
117  float scaleW = static_cast<float>(std::max(w, h)) / w;
118  float scaleH = static_cast<float>(std::max(w, h)) / h;
119  BBox scaledBBox = clampedBox1.scale(scaleW, scaleH);
120  BBox clampedBox2 = scaledBBox.clamp(clampBox);
121  if (!clampedBox2.isValid())
122  {
123  throw std::range_error("Invalid bounding box generated\n");
124  }
125  size_t newW = clampedBox2.getWidth();
126  size_t newH = clampedBox2.getHeight();
127  size_t minW = std::min(newH, newW);
128  clampedBox2.ymax = clampedBox2.ymin + minW;
129  clampedBox2.xmax = clampedBox2.xmin + minW;
130  return clampedBox2;
131  }
132 };
133 
134 } // namespace cvcore
135 #endif // CVCORE_BBOX_H
cvcore::BBox
A struct.
Definition: BBox.h:24
cvcore::BBox::getCenter
std::pair< int, int > getCenter() const
Returns the center of the bounding box.
Definition: BBox.h:74
cvcore
Definition: PnP.h:20
cvcore::BBox::scale
BBox scale(float scaleW, float scaleH) const
Scales bounding box based along the width and height retaining the same center.
Definition: BBox.h:87
cvcore::BBox::ymin
int ymin
minimum y coordinate.
Definition: BBox.h:27
cvcore::BBox::squarify
BBox squarify(const BBox &clampBox) const
Resizes bounding box to a square bounding box based on the longest edge and clamps the bounding box b...
Definition: BBox.h:107
cvcore::BBox::xmax
int xmax
maximum x coordinate.
Definition: BBox.h:28
cvcore::BBox::getWidth
size_t getWidth() const
Definition: BBox.h:49
cvcore::BBox::clamp
BBox clamp(const BBox &clampBox) const
Clamp a bounding box based on a restricting clamp box.
Definition: BBox.h:36
cvcore::BBox::ymax
int ymax
maximum y coordinate.
Definition: BBox.h:29
cvcore::BBox::xmin
int xmin
minimum x coordinate.
Definition: BBox.h:26
cvcore::BBox::getHeight
size_t getHeight() const
Definition: BBox.h:57
cvcore::BBox::isValid
bool isValid() const
Checks if the bounding box is valid.
Definition: BBox.h:65