xref: /aosp_15_r20/external/armnn/samples/ObjectDetection/test/BoundingBoxTests.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <catch.hpp>
7 #include "BoundingBox.hpp"
8 
9 namespace
10 {
11     static constexpr unsigned int s_X = 100u;
12     static constexpr unsigned int s_Y = 200u;
13     static constexpr unsigned int s_W = 300u;
14     static constexpr unsigned int s_H = 400u;
15 } // anonymous namespace
16 
17 TEST_CASE("BoundingBoxTest_Default")
18 {
19     od::BoundingBox boundingBox;
20 
21     REQUIRE(boundingBox.GetX()      == 0u);
22     REQUIRE(boundingBox.GetY()      == 0u);
23     REQUIRE(boundingBox.GetWidth()  == 0u);
24     REQUIRE(boundingBox.GetHeight() == 0u);
25 }
26 
27 TEST_CASE("BoundingBoxTest_Custom")
28 {
29     od::BoundingBox boundingBox(s_X, s_Y, s_W, s_H);
30 
31     REQUIRE(boundingBox.GetX()      == s_X);
32     REQUIRE(boundingBox.GetY()      == s_Y);
33     REQUIRE(boundingBox.GetWidth()  == s_W);
34     REQUIRE(boundingBox.GetHeight() == s_H);
35 }
36 
37 TEST_CASE("BoundingBoxTest_Setters")
38 {
39     od::BoundingBox boundingBox;
40 
41     boundingBox.SetX(s_X);
42     boundingBox.SetY(s_Y);
43     boundingBox.SetWidth(s_W);
44     boundingBox.SetHeight(s_H);
45 
46     REQUIRE(boundingBox.GetX()      == s_X);
47     REQUIRE(boundingBox.GetY()      == s_Y);
48     REQUIRE(boundingBox.GetWidth()  == s_W);
49     REQUIRE(boundingBox.GetHeight() == s_H);
50 }
51 
AreBoxesEqual(od::BoundingBox & b1,od::BoundingBox & b2)52 static inline bool AreBoxesEqual(od::BoundingBox& b1, od::BoundingBox& b2)
53 {
54     return (b1.GetX() == b2.GetX() && b1.GetY() == b2.GetY() &&
55         b1.GetWidth() == b2.GetWidth() && b1.GetHeight() == b2.GetHeight());
56 }
57 
58 TEST_CASE("BoundingBoxTest_GetValidBoundingBox")
59 {
60     od::BoundingBox boxIn { 0,  0, 10, 20 };
61     od::BoundingBox boxOut;
62 
63     WHEN("Limiting box is completely within the input box")
64     {
65         od::BoundingBox boxLmt{ 1,  1,  9, 18 };
66         GetValidBoundingBox(boxIn, boxOut, boxLmt);
67         REQUIRE(AreBoxesEqual(boxLmt,boxOut));
68     }
69 
70     WHEN("Limiting box cuts off the top and left")
71     {
72         od::BoundingBox boxLmt{ 1,  1, 10, 20 };
73         od::BoundingBox boxExp{ 1,  1,  9, 19 };
74         GetValidBoundingBox(boxIn, boxOut, boxLmt);
75         REQUIRE(AreBoxesEqual(boxExp, boxOut));
76     }
77 
78     WHEN("Limiting box cuts off the bottom")
79     {
80         od::BoundingBox boxLmt{ 0,  0, 10, 19 };
81         GetValidBoundingBox(boxIn, boxOut, boxLmt);
82         REQUIRE(AreBoxesEqual(boxLmt, boxOut));
83     }
84 
85     WHEN("Limiting box cuts off the right")
86     {
87         od::BoundingBox boxLmt{ 0,  0,  9, 20 };
88         GetValidBoundingBox(boxIn, boxOut, boxLmt);
89         REQUIRE(AreBoxesEqual(boxLmt, boxOut));
90     }
91 
92     WHEN("Limiting box cuts off the bottom and right")
93     {
94         od::BoundingBox boxLmt{ 0,  0,  9, 19 };
95         GetValidBoundingBox(boxIn, boxOut, boxLmt);
96         REQUIRE(AreBoxesEqual(boxLmt, boxOut));
97     }
98 
99     WHEN("Limiting box cuts off the bottom and left")
100     {
101         od::BoundingBox boxLmt{ 1,  0, 10, 19 };
102         od::BoundingBox boxExp{ 1,  0,  9, 19 };
103         GetValidBoundingBox(boxIn, boxOut, boxLmt);
104         REQUIRE(AreBoxesEqual(boxExp, boxOut));
105     }
106 
107     WHEN("Limiting box does not impose any limit")
108     {
109         od::BoundingBox boxLmt{ 0,  0, 10, 20 };
110         GetValidBoundingBox(boxIn, boxOut, boxLmt);
111         REQUIRE(AreBoxesEqual(boxIn, boxOut));
112     }
113 
114     WHEN("Limiting box zeros out the width")
115     {
116         od::BoundingBox boxLmt{ 0,  0,  0, 20 };
117         od::BoundingBox boxExp{ 0,  0,  0,  0 };
118         GetValidBoundingBox(boxIn, boxOut, boxLmt);
119         REQUIRE(AreBoxesEqual(boxExp, boxOut));
120     }
121 
122     WHEN("Limiting box zeros out the height")
123     {
124         od::BoundingBox boxLmt{ 0,  0, 10,  0 };
125         od::BoundingBox boxExp{ 0,  0,  0,  0 };
126         GetValidBoundingBox(boxIn, boxOut, boxLmt);
127         REQUIRE(AreBoxesEqual(boxExp, boxOut));
128     }
129 
130     WHEN("Limiting box with negative starts - top and left with 1 sq pixel cut-off")
131     {
132         od::BoundingBox boxLmt{ -1, -1, 10, 20 };
133         od::BoundingBox boxExp{  0,  0,  9, 19 };
134         GetValidBoundingBox(boxIn, boxOut, boxLmt);
135         REQUIRE(AreBoxesEqual(boxExp, boxOut));
136     }
137 
138     WHEN("Limiting box with negative starts - top and left with full overlap")
139     {
140         od::BoundingBox boxLmt{ -1, -1, 11, 21 };
141         GetValidBoundingBox(boxIn, boxOut, boxLmt);
142         REQUIRE(AreBoxesEqual(boxIn, boxOut));
143     }
144 
145     WHEN("Limiting box with zero overlap")
146     {
147         od::BoundingBox boxLmt{-10,-20, 10, 20 };
148         od::BoundingBox boxExp{  0,  0,  0,  0 };
149         GetValidBoundingBox(boxIn, boxOut, boxLmt);
150         REQUIRE(AreBoxesEqual(boxExp, boxOut));
151     }
152 
153     WHEN("Limiting box with one square pixel overlap")
154     {
155         od::BoundingBox boxLmt{-9,-19, 10, 20 };
156         od::BoundingBox boxExp{ 0,  0,  1,  1 };
157         GetValidBoundingBox(boxIn, boxOut, boxLmt);
158         REQUIRE(AreBoxesEqual(boxExp, boxOut));
159     }
160 
161     WHEN("Limiting box with unrealistically high values in positive quadrant")
162     {
163         od::BoundingBox boxLmt{INT32_MAX, INT32_MAX, UINT32_MAX, UINT32_MAX };
164         od::BoundingBox boxExp{ 0,  0,  0,  0 };
165         GetValidBoundingBox(boxIn, boxOut, boxLmt);
166         REQUIRE(AreBoxesEqual(boxExp, boxOut));
167     }
168 
169     /* This should actually return a valid bounding box, currently not handled. */
170     WHEN("Limiting box with unrealistic values spanning 32 bit space")
171     {
172         od::BoundingBox boxLmt{-(INT32_MAX), -(INT32_MAX), UINT32_MAX, UINT32_MAX};
173         od::BoundingBox boxExp{ 0,  0,  0,  0 };
174         GetValidBoundingBox(boxIn, boxOut, boxLmt);
175         REQUIRE(AreBoxesEqual(boxExp, boxOut));
176     }
177 }