-
Notifications
You must be signed in to change notification settings - Fork 9
/
bounding_box_2d.cpp
126 lines (108 loc) · 3.75 KB
/
bounding_box_2d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*!
* @copyright 2018 Bonn-Rhein-Sieg University
*
* @author Minh Nguyen
*
*/
#include <sstream>
#include <vector>
#include <opencv2/imgproc.hpp>
#include <mas_perception_libs/bounding_box_2d.h>
namespace mas_perception_libs
{
void
BoundingBox2D::updateBox(const cv::Rect &pNewRect)
{
mX = pNewRect.x;
mY = pNewRect.y;
mWidth = pNewRect.width;
mHeight = pNewRect.height;
}
void
drawLabeledBoxes(cv::Mat &pImage, std::vector<BoundingBox2D> pBoundingBoxes, int pThickness, double pFontScale)
{
cv::Size imageSize = pImage.size();
for (auto &pBoundingBox : pBoundingBoxes)
{
// fit box to within image boundaries
auto boxRect = pBoundingBox.getCvRect();
fitBoxToImage(imageSize, boxRect);
// fit label into within image boundaries
int baseline = 0;
cv::Size textSize = cv::getTextSize(pBoundingBox.mLabel, cv::FONT_HERSHEY_PLAIN,
pFontScale, pThickness, &baseline);
if (boxRect.y < textSize.height) boxRect.y = textSize.height;
// draw box
cv::rectangle(pImage, boxRect, pBoundingBox.mColor, pThickness);
// draw label with background
cv::Point textTopLeft = cv::Point(boxRect.x, boxRect.y - textSize.height);
cv::Point textBottomRight = cv::Point(boxRect.x + textSize.width, boxRect.y + baseline);
cv::rectangle(pImage, textTopLeft, textBottomRight, pBoundingBox.mColor, cv::FILLED);
cv::putText(pImage, pBoundingBox.mLabel, cv::Point(boxRect.x, boxRect.y),
cv::FONT_HERSHEY_PLAIN, pFontScale, CV_RGB(255, 255, 555), 1);
}
}
void
fitBoxToImage(const cv::Size &pImageSize, BoundingBox2D &pBox, int pSizeOffset)
{
auto cvBox = pBox.getCvRect();
fitBoxToImage(pImageSize, cvBox, pSizeOffset);
pBox.updateBox(cvBox);
}
void
fitBoxToImage(const cv::Size &pImageSize, cv::Rect &pBox, int pSizeOffset)
{
if (pSizeOffset)
{
// if size offset is specified, expand bounding box by x offset pixels
pBox.x -= pSizeOffset;
pBox.y -= pSizeOffset;
pBox.width += 2 * pSizeOffset;
pBox.height += 2 * pSizeOffset;
}
// ensure adjusted box has non-zero dimensions
if (pBox.x > pImageSize.width - 2 || pBox.y > pImageSize.height - 2) {
std::ostringstream msgStream;
msgStream << "fitBoxToImage: box coordinates (x=" << pBox.x << ", y=" << pBox.y << ") too large for image (w="
<< pImageSize.width << ",h=" << pImageSize.height << ")";
throw std::runtime_error(msgStream.str());
}
// check if box is outside of image, should be efficient since vectorized
cv::Rect image_rect(0, 0, pImageSize.width, pImageSize.height);
if ((pBox & image_rect) == pBox)
return;
if (pBox.x < 0)
pBox.x = 0;
if (pBox.y < 0)
pBox.y = 0;
if (pBox.x + pBox.width >= pImageSize.width)
pBox.width = pImageSize.width - pBox.x - 1;
if (pBox.y + pBox.height >= pImageSize.height)
pBox.height = pImageSize.height - pBox.y - 1;
}
cv::Mat
cropImage(cv::Mat &pImage, BoundingBox2D &pBox, int pOffset, bool pCopy)
{
cv::Rect cvBox = pBox.getCvRect();
return cropImage(pImage, cvBox, pOffset, pCopy);
}
cv::Mat
cropImage(cv::Mat &pImage, const std::vector<cv::Point2f> &pVertices, int pOffset, bool pCopy)
{
cv::Rect roiRect = cv::boundingRect(cv::Mat(pVertices));
return cropImage(pImage, roiRect, pOffset, pCopy);
}
cv::Mat
cropImage(cv::Mat &pImage, cv::Rect &pRoiRect, int pOffset, bool pCopy)
{
fitBoxToImage(pImage.size(), pRoiRect, pOffset);
cv::Mat croppedImage(pImage, pRoiRect);
if (pCopy)
{
cv::Mat croppedCopy;
croppedImage.copyTo(croppedCopy);
return croppedCopy;
}
return croppedImage;
}
} // namespace mas_perception_libs