Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
fb44c7ba
Commit
fb44c7ba
authored
Aug 27, 2016
by
Davis King
Browse files
Added box_intersection_over_union() and also renamed the class members of
test_box_overlap so they are less confusing and vague.
parent
f0dff5fc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
60 deletions
+107
-60
dlib/image_processing/box_overlap_testing.h
dlib/image_processing/box_overlap_testing.h
+65
-41
dlib/image_processing/box_overlap_testing_abstract.h
dlib/image_processing/box_overlap_testing_abstract.h
+42
-19
No files found.
dlib/image_processing/box_overlap_testing.h
View file @
fb44c7ba
...
...
@@ -10,27 +10,51 @@
namespace
dlib
{
// ----------------------------------------------------------------------------------------
inline
double
box_intersection_over_union
(
const
drectangle
&
a
,
const
drectangle
&
b
)
{
const
double
inner
=
a
.
intersect
(
b
).
area
();
if
(
inner
==
0
)
return
0
;
const
double
outer
=
(
a
+
b
).
area
();
return
inner
/
outer
;
}
// ----------------------------------------------------------------------------------------
inline
double
box_intersection_over_union
(
const
rectangle
&
a
,
const
rectangle
&
b
)
{
return
box_intersection_over_union
(
drectangle
(
a
),
drectangle
(
b
));
}
// ----------------------------------------------------------------------------------------
class
test_box_overlap
{
public:
test_box_overlap
(
)
:
match
_thresh
(
0.5
),
over
lap
_thresh
(
1.0
)
)
:
iou
_thresh
(
0.5
),
percent_c
over
ed
_thresh
(
1.0
)
{}
explicit
test_box_overlap
(
double
match
_thresh_
,
double
over
lap
_thresh_
=
1.0
)
:
match
_thresh
(
match
_thresh_
),
over
lap
_thresh
(
over
lap
_thresh_
)
double
iou
_thresh_
,
double
percent_c
over
ed
_thresh_
=
1.0
)
:
iou
_thresh
(
iou
_thresh_
),
percent_c
over
ed
_thresh
(
percent_c
over
ed
_thresh_
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
0
<=
match
_thresh
&&
match
_thresh
<=
1
&&
0
<=
over
lap
_thresh
&&
over
lap
_thresh
<=
1
,
"
\t
test_box_overlap::test_box_overlap(
match
_thresh, over
lap
_thresh)"
DLIB_ASSERT
(
0
<=
iou
_thresh
&&
iou
_thresh
<=
1
&&
0
<=
percent_c
over
ed
_thresh
&&
percent_c
over
ed
_thresh
<=
1
,
"
\t
test_box_overlap::test_box_overlap(
iou
_thresh,
percent_c
over
ed
_thresh)"
<<
"
\n\t
Invalid inputs were given to this function "
<<
"
\n\t
match
_thresh: "
<<
match
_thresh
<<
"
\n\t
over
lap
_thresh: "
<<
over
lap
_thresh
<<
"
\n\t
iou
_thresh: "
<<
iou
_thresh
<<
"
\n\t
percent_c
over
ed
_thresh: "
<<
percent_c
over
ed
_thresh
<<
"
\n\t
this: "
<<
this
);
...
...
@@ -46,29 +70,29 @@ namespace dlib
return
false
;
const
double
outer
=
(
a
+
b
).
area
();
if
(
inner
/
outer
>
match
_thresh
||
inner
/
a
.
area
()
>
over
lap
_thresh
||
inner
/
b
.
area
()
>
over
lap
_thresh
)
if
(
inner
/
outer
>
iou
_thresh
||
inner
/
a
.
area
()
>
percent_c
over
ed
_thresh
||
inner
/
b
.
area
()
>
percent_c
over
ed
_thresh
)
return
true
;
else
return
false
;
}
double
get_over
lap
_thresh
(
double
get_
percent_c
over
ed
_thresh
(
)
const
{
return
over
lap
_thresh
;
return
percent_c
over
ed
_thresh
;
}
double
get_
match
_thresh
(
double
get_
iou
_thresh
(
)
const
{
return
match
_thresh
;
return
iou
_thresh
;
}
private:
double
match
_thresh
;
double
over
lap
_thresh
;
double
iou
_thresh
;
double
percent_c
over
ed
_thresh
;
};
// ----------------------------------------------------------------------------------------
...
...
@@ -78,8 +102,8 @@ namespace dlib
std
::
ostream
&
out
)
{
serialize
(
item
.
get_
match
_thresh
(),
out
);
serialize
(
item
.
get_over
lap
_thresh
(),
out
);
serialize
(
item
.
get_
iou
_thresh
(),
out
);
serialize
(
item
.
get_
percent_c
over
ed
_thresh
(),
out
);
}
inline
void
deserialize
(
...
...
@@ -87,10 +111,10 @@ namespace dlib
std
::
istream
&
in
)
{
double
over
lap
_thresh
,
match
_thresh
;
deserialize
(
match
_thresh
,
in
);
deserialize
(
over
lap
_thresh
,
in
);
item
=
test_box_overlap
(
match
_thresh
,
over
lap
_thresh
);
double
percent_c
over
ed
_thresh
,
iou
_thresh
;
deserialize
(
iou
_thresh
,
in
);
deserialize
(
percent_c
over
ed
_thresh
,
in
);
item
=
test_box_overlap
(
iou
_thresh
,
percent_c
over
ed
_thresh
);
}
// ----------------------------------------------------------------------------------------
...
...
@@ -99,8 +123,8 @@ namespace dlib
const
std
::
vector
<
std
::
vector
<
rectangle
>
>&
rects
)
{
double
max_ov
erlap
=
0
;
double
max_
match
_score
=
0
;
double
max_
pc
ov
=
0
;
double
max_
iou
_score
=
0
;
for
(
unsigned
long
i
=
0
;
i
<
rects
.
size
();
++
i
)
{
for
(
unsigned
long
j
=
0
;
j
<
rects
[
i
].
size
();
++
j
)
...
...
@@ -109,29 +133,29 @@ namespace dlib
{
const
rectangle
a
=
rects
[
i
][
j
];
const
rectangle
b
=
rects
[
i
][
k
];
const
double
match
_score
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
a
+
b
).
area
();
const
double
ov
erlap
_a
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
a
).
area
();
const
double
ov
erlap
_b
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
b
).
area
();
const
double
iou
_score
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
a
+
b
).
area
();
const
double
pc
ov_a
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
a
).
area
();
const
double
pc
ov_b
=
(
a
.
intersect
(
b
)).
area
()
/
(
double
)(
b
).
area
();
if
(
match
_score
>
max_
match
_score
)
max_
match
_score
=
match
_score
;
if
(
iou
_score
>
max_
iou
_score
)
max_
iou
_score
=
iou
_score
;
if
(
ov
erlap
_a
>
max_ov
erlap
)
max_
overlap
=
overlap
_a
;
if
(
ov
erlap
_b
>
max_ov
erlap
)
max_
overlap
=
overlap
_b
;
if
(
pc
ov_a
>
max_
pc
ov
)
max_
pcov
=
pcov
_a
;
if
(
pc
ov_b
>
max_
pc
ov
)
max_
pcov
=
pcov
_b
;
}
}
}
// Relax these thresholds very slightly. We do this because on some systems the
// boxes that generated the max values erroneously trigger a box overlap
match
//
even
though their over
lap
and
match
values are *equal* to the thresholds but
not
// greater. That is, sometimes when double values get moved around they change
// boxes that generated the max values erroneously trigger a box overlap
iou even
// though their
percent c
over
ed
and
iou
values are *equal* to the thresholds but
//
not
greater. That is, sometimes when double values get moved around they change
// their values slightly, so this avoids the problems that can create.
max_
match
_score
=
std
::
min
(
1.0000001
*
max_
match
_score
,
1.0
);
max_ov
erlap
=
std
::
min
(
1.0000001
*
max_ov
erlap
,
1.0
);
return
test_box_overlap
(
max_
match
_score
,
max_ov
erlap
);
max_
iou
_score
=
std
::
min
(
1.0000001
*
max_
iou
_score
,
1.0
);
max_
pc
ov
=
std
::
min
(
1.0000001
*
max_
pc
ov
,
1.0
);
return
test_box_overlap
(
max_
iou
_score
,
max_
pc
ov
);
}
// ----------------------------------------------------------------------------------------
...
...
dlib/image_processing/box_overlap_testing_abstract.h
View file @
fb44c7ba
...
...
@@ -8,6 +8,30 @@
namespace
dlib
{
// ----------------------------------------------------------------------------------------
inline
double
box_intersection_over_union
(
const
drectangle
&
a
,
const
drectangle
&
b
);
/*!
ensures
- returns area of the intersection of a and b divided by (a+b).area(). If both
boxes are empty then returns 0.
!*/
// ----------------------------------------------------------------------------------------
inline
double
box_intersection_over_union
(
const
rectangle
&
a
,
const
rectangle
&
b
);
/*!
ensures
- returns area of the intersection of a and b divided by (a+b).area(). If both
boxes are empty then returns 0.
!*/
// ----------------------------------------------------------------------------------------
class
test_box_overlap
...
...
@@ -28,21 +52,21 @@ namespace dlib
);
/*!
ensures
- #get_
match
_thresh() == 0.5
- #get_over
lap
_thresh() == 1.0
- #get_
iou
_thresh() == 0.5
- #get_
percent_c
over
ed
_thresh() == 1.0
!*/
explicit
test_box_overlap
(
double
match
_thresh
,
double
over
lap
_thresh
=
1.0
double
iou
_thresh
,
double
percent_c
over
ed
_thresh
=
1.0
);
/*!
requires
- 0 <=
match
_thresh <= 1
- 0 <= over
lap
_thresh <= 1
- 0 <=
iou
_thresh <= 1
- 0 <=
percent_c
over
ed
_thresh <= 1
ensures
- #get_
match
_thresh() ==
match
_thresh
- #get_over
lap
_thresh() == over
lap
_thresh
- #get_
iou
_thresh() ==
iou
_thresh
- #get_
percent_c
over
ed
_thresh() ==
percent_c
over
ed
_thresh
!*/
bool
operator
()
(
...
...
@@ -52,31 +76,30 @@ namespace dlib
/*!
ensures
- returns true if a and b overlap "enough". This is defined precisely below.
- if (a.intersect(b).area()/(a+b).area() > get_
match
_thresh() ||
a.intersect(b).area()/a.area() > get_over
lap
_thresh() ||
a.intersect(b).area()/b.area() > get_over
lap
_thresh() ) then
- if (a.intersect(b).area()/(a+b).area() > get_
iou
_thresh() ||
a.intersect(b).area()/a.area() > get_
percent_c
over
ed
_thresh() ||
a.intersect(b).area()/b.area() > get_
percent_c
over
ed
_thresh() ) then
- returns true
- else
- returns false
!*/
double
get_
match
_thresh
(
double
get_
iou
_thresh
(
)
const
;
/*!
ensures
- returns the threshold used to determine if two rectangles
match.
Note that the match score varies from 0 to 1 and only becomes 1
when two rectangles are
identical.
- returns the threshold used to determine if two rectangle
'
s
intersection
over union value is big enough to be considered a match. Note that the
iou score varies from 0 to 1 and only becomes 1
when two rectangles are
identical.
!*/
double
get_over
lap
_thresh
(
double
get_
percent_c
over
ed
_thresh
(
)
const
;
/*!
ensures
- returns the threshold used to determine if two rectangles overlap. This
value is the percent of a rectangle's area covered by another rectangle.
!*/
};
...
...
@@ -110,7 +133,7 @@ namespace dlib
that is consistent with the given set of sets of rectangles.
- To be precise, this function finds and returns a test_box_overlap object
TBO such that:
- TBO.get_
match
_thresh() and TBO.get_over
lap
_thresh() are as small
- TBO.get_
iou
_thresh() and TBO.get_
percent_c
over
ed
_thresh() are as small
as possible such that the following conditions are satisfied.
- for all valid i:
- for all distinct rectangles A and B in rects[i]:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment