Made the test_box_overlap a little more flexible. This change breaks

backwards compatibility with the previous version though.
This commit is contained in:
Davis King 2011-12-24 15:12:30 -05:00
parent 8268c2fee7
commit ac83daec4a
2 changed files with 98 additions and 11 deletions

View File

@ -5,6 +5,7 @@
#include "box_overlap_testing_abstract.h"
#include "../geometry.h"
#include <vector>
namespace dlib
{
@ -15,17 +16,20 @@ namespace dlib
{
public:
test_box_overlap (
) : overlap_thresh(0.5)
) : match_thresh(0.5), overlap_thresh(0.5)
{}
test_box_overlap (
double match_thresh_,
double overlap_thresh_
) : overlap_thresh(overlap_thresh_)
) : match_thresh(match_thresh_), overlap_thresh(overlap_thresh_)
{
// make sure requires clause is not broken
DLIB_ASSERT(0 <= overlap_thresh && overlap_thresh <= 1,
"\t test_box_overlap::test_box_overlap(overlap_thresh)"
DLIB_ASSERT(0 <= match_thresh && match_thresh <= 1 &&
0 <= overlap_thresh && overlap_thresh <= 1,
"\t test_box_overlap::test_box_overlap(match_thresh, overlap_thresh)"
<< "\n\t Invalid inputs were given to this function "
<< "\n\t match_thresh: " << match_thresh
<< "\n\t overlap_thresh: " << overlap_thresh
<< "\n\t this: " << this
);
@ -39,7 +43,9 @@ namespace dlib
{
const double inner = a.intersect(b).area();
const double outer = (a+b).area();
if (inner/outer > overlap_thresh)
if (inner/outer > match_thresh ||
inner/a.area() > overlap_thresh ||
inner/b.area() > overlap_thresh)
return true;
else
return false;
@ -51,7 +57,14 @@ namespace dlib
return overlap_thresh;
}
double get_match_thresh (
) const
{
return match_thresh;
}
private:
double match_thresh;
double overlap_thresh;
};
@ -62,6 +75,7 @@ namespace dlib
std::ostream& out
)
{
serialize(item.get_match_thresh(), out);
serialize(item.get_overlap_thresh(), out);
}
@ -70,9 +84,44 @@ namespace dlib
std::istream& in
)
{
double overlap_thresh;
double overlap_thresh, match_thresh;
deserialize(match_thresh, in);
deserialize(overlap_thresh, in);
item = test_box_overlap(overlap_thresh);
item = test_box_overlap(match_thresh, overlap_thresh);
}
// ----------------------------------------------------------------------------------------
inline test_box_overlap find_tight_overlap_tester (
const std::vector<std::vector<rectangle> >& rects
)
{
double max_overlap = 0;
double max_match_score = 0;
for (unsigned long i = 0; i < rects.size(); ++i)
{
for (unsigned long j = 0; j < rects[i].size(); ++j)
{
for (unsigned long k = j+1; k < rects[i].size(); ++k)
{
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 overlap_a = (a.intersect(b)).area()/(double)(a).area();
const double overlap_b = (a.intersect(b)).area()/(double)(b).area();
if (match_score > max_match_score)
max_match_score = match_score;
if (overlap_a > max_overlap)
max_overlap = overlap_a;
if (overlap_b > max_overlap)
max_overlap = overlap_b;
}
}
}
return test_box_overlap(max_match_score, max_overlap);
}
// ----------------------------------------------------------------------------------------

View File

@ -29,15 +29,19 @@ namespace dlib
/*!
ensures
- #get_overlap_thresh() == 0.5
- #get_match_thresh() == 0.5
!*/
test_box_overlap (
double match_thresh,
double overlap_thresh
);
/*!
requires
- 0 <= match_thresh <= 1
- 0 <= overlap_thresh <= 1
ensures
- #get_match_thresh() == match_thresh
- #get_overlap_thresh() == overlap_thresh
!*/
@ -47,15 +51,31 @@ namespace dlib
) const;
/*!
ensures
- returns true if a.intersect(b).area()/(a+b).area > get_overlap_thresh()
and false otherwise. (i.e. returns true if a and b overlap enough)
- 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_overlap_thresh() ||
a.intersect(b).area()/a.area() > get_overlap_thresh() ) then
- returns true
- else
- returns false
!*/
double get_overlap_thresh (
) const;
/*!
ensures
- returns the threshold used to determine if two rectangles overlap.
- returns the threshold used to determine if two rectangles overlap. This
value is the percent of a rectangle's area covered by another rectangle.
!*/
double get_match_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.
!*/
@ -79,6 +99,24 @@ namespace dlib
provides deserialization support
!*/
// ----------------------------------------------------------------------------------------
test_box_overlap find_tight_overlap_tester (
const std::vector<std::vector<rectangle> >& rects
);
/*!
ensures
- This function finds the most restrictive test_box_overlap object possible
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_overlap_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]:
- TBO(A,B) == false
!*/
// ----------------------------------------------------------------------------------------
}