1 // Copyright (C) 2011  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #ifndef DLIB_DETECTION_TEMPlATE_TOOLS_Hh_
4 #define DLIB_DETECTION_TEMPlATE_TOOLS_Hh_
5 
6 #include "detection_template_tools_abstract.h"
7 #include "../geometry.h"
8 #include "../matrix.h"
9 #include <utility>
10 #include <vector>
11 #include <cmath>
12 
13 namespace dlib
14 {
15 
16 // ----------------------------------------------------------------------------------------
17 
compute_box_dimensions(const double width_to_height_ratio,const double area)18     inline rectangle compute_box_dimensions (
19         const double width_to_height_ratio,
20         const double area
21     )
22     {
23         // make sure requires clause is not broken
24         DLIB_ASSERT(width_to_height_ratio > 0 && area > 0,
25             "\t rectangle compute_box_dimensions()"
26             << "\n\t Invalid arguments were given to this function. "
27             << "\n\t width_to_height_ratio: " << width_to_height_ratio
28             << "\n\t area: " << area
29             );
30 
31         /*
32             width*height == area
33             width/height == width_to_height_ratio
34         */
35         using namespace std;
36 
37         const int height = (int)std::floor(std::sqrt(area/width_to_height_ratio) + 0.5);
38         const int width  = (int)std::floor(area/height + 0.5);
39 
40         return centered_rect(0,0,width,height);
41     }
42 
43 // ----------------------------------------------------------------------------------------
44 
create_single_box_detection_template(const rectangle & object_box)45     inline std::vector<rectangle> create_single_box_detection_template (
46         const rectangle& object_box
47     )
48     {
49         std::vector<rectangle> temp;
50         temp.push_back(object_box);
51         return temp;
52     }
53 
54 // ----------------------------------------------------------------------------------------
55 
create_overlapped_2x2_detection_template(const rectangle & object_box)56     inline std::vector<rectangle> create_overlapped_2x2_detection_template (
57         const rectangle& object_box
58     )
59     {
60         std::vector<rectangle> result;
61 
62         const point c = center(object_box);
63 
64         result.push_back(rectangle() + c + object_box.tl_corner() + object_box.tr_corner());
65         result.push_back(rectangle() + c + object_box.bl_corner() + object_box.br_corner());
66         result.push_back(rectangle() + c + object_box.tl_corner() + object_box.bl_corner());
67         result.push_back(rectangle() + c + object_box.tr_corner() + object_box.br_corner());
68 
69         return result;
70     }
71 
72 // ----------------------------------------------------------------------------------------
73 
create_grid_detection_template(const rectangle & object_box,unsigned int cells_x,unsigned int cells_y)74     inline std::vector<rectangle> create_grid_detection_template (
75         const rectangle& object_box,
76         unsigned int cells_x,
77         unsigned int cells_y
78     )
79     {
80         // make sure requires clause is not broken
81         DLIB_ASSERT(cells_x > 0 && cells_y > 0,
82             "\t std::vector<rectangle> create_grid_detection_template()"
83             << "\n\t The number of cells along a dimension can't be zero. "
84             << "\n\t cells_x: " << cells_x
85             << "\n\t cells_y: " << cells_y
86             );
87 
88         std::vector<rectangle> result;
89 
90         const matrix<double,1> x = linspace(object_box.left(), object_box.right(), cells_x+1);
91         const matrix<double,1> y = linspace(object_box.top(), object_box.bottom(), cells_y+1);
92 
93         for (long j = 0; j+1 < y.size(); ++j)
94         {
95             for (long i = 0; i+1 < x.size(); ++i)
96             {
97                 const dlib::vector<double,2> tl(x(i),y(j));
98                 const dlib::vector<double,2> br(x(i+1),y(j+1));
99                 result.push_back(rectangle(tl,br));
100             }
101         }
102 
103         return result;
104     }
105 
106 // ----------------------------------------------------------------------------------------
107 
108 }
109 
110 
111 #endif // DLIB_DETECTION_TEMPlATE_TOOLS_Hh_
112 
113 
114