box_overlap_testing.h 4.74 KB
Newer Older
1
2
3
4
5
6
7
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_BOX_OVERlAP_TESTING_H__
#define DLIB_BOX_OVERlAP_TESTING_H__

#include "box_overlap_testing_abstract.h"
#include "../geometry.h"
8
#include <vector>
9
10
11
12
13
14
15
16
17
18

namespace dlib
{

// ----------------------------------------------------------------------------------------

    class test_box_overlap
    {
    public:
        test_box_overlap (
19
        ) : match_thresh(0.5), overlap_thresh(1.0)
20
21
        {}

22
        explicit test_box_overlap (
23
            double match_thresh_,
24
            double overlap_thresh_ = 1.0
25
        ) : match_thresh(match_thresh_), overlap_thresh(overlap_thresh_) 
Davis King's avatar
Davis King committed
26
27
        {
            // make sure requires clause is not broken
28
29
30
            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)"
Davis King's avatar
Davis King committed
31
                << "\n\t Invalid inputs were given to this function "
32
33
                << "\n\t match_thresh:   " << match_thresh
                << "\n\t overlap_thresh: " << overlap_thresh
Davis King's avatar
Davis King committed
34
35
36
37
                << "\n\t this: " << this
                );

        }
38
39
40
41
42
43
44

        bool operator() (
            const dlib::rectangle& a,
            const dlib::rectangle& b
        ) const
        {
            const double inner = a.intersect(b).area();
45
46
47
            if (inner == 0)
                return false;

48
            const double outer = (a+b).area();
49
50
51
            if (inner/outer > match_thresh || 
                inner/a.area() > overlap_thresh || 
                inner/b.area() > overlap_thresh)
52
53
54
55
56
57
58
59
60
61
62
                return true;
            else
                return false;
        }

        double get_overlap_thresh (
        ) const
        {
            return overlap_thresh;
        }

63
64
65
66
67
68
        double get_match_thresh (
        ) const
        {
            return match_thresh;
        }

69
    private:
70
        double match_thresh;
71
72
73
74
75
76
77
78
79
80
        double overlap_thresh;
    };

// ----------------------------------------------------------------------------------------

    inline void serialize (
        const test_box_overlap& item,
        std::ostream& out
    )
    {
81
        serialize(item.get_match_thresh(), out);
82
83
84
85
86
87
88
89
        serialize(item.get_overlap_thresh(), out);
    }

    inline void deserialize (
        test_box_overlap& item,
        std::istream& in 
    )
    {
90
91
        double overlap_thresh, match_thresh;
        deserialize(match_thresh, in);
92
        deserialize(overlap_thresh, in);
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
127
        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);
128
129
    }

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// ----------------------------------------------------------------------------------------

    inline bool overlaps_any_box (
        const test_box_overlap& tester,
        const std::vector<rectangle>& rects,
        const rectangle& rect
    )
    {
        for (unsigned long i = 0; i < rects.size(); ++i)
        {
            if (tester(rects[i],rect))
                return true;
        }
        return false;
    }

146
147
148
149
150
151
152
153
154
155
// ----------------------------------------------------------------------------------------

    inline bool overlaps_any_box (
        const std::vector<rectangle>& rects,
        const rectangle& rect
    )
    {
        return overlaps_any_box(test_box_overlap(),rects,rect);
    }

156
157
158
159
160
161
// ----------------------------------------------------------------------------------------

}

#endif // DLIB_BOX_OVERlAP_TESTING_H__