Removed the definition of basename, which confuses an existing system one.
[folded-ctf.git] / pose_cell_hierarchy.cc
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "pose_cell_hierarchy.h"
26 #include "gaussian.h"
27
28 PoseCellHierarchy::PoseCellHierarchy() {
29   _belly_cells = 0;
30 }
31
32 PoseCellHierarchy::PoseCellHierarchy(LabelledImagePool *train_pool) {
33   _nb_levels = global.nb_levels;
34   _min_head_radius = global.min_head_radius;
35   _max_head_radius = global.max_head_radius;
36   _root_cell_nb_xy_per_radius = global.root_cell_nb_xy_per_radius;
37
38   LabelledImage *image;
39   int nb_total_targets = 0;
40   for(int i = 0; i < train_pool->nb_images(); i++) {
41     image = train_pool->grab_image(i);
42     // We are going to symmetrize
43     nb_total_targets += 2 * image->nb_targets();
44     train_pool->release_image(i);
45   }
46
47   RelativeBellyPoseCell targets[nb_total_targets];
48
49   int u = 0;
50   for(int i = 0; i < train_pool->nb_images(); i++) {
51     image = train_pool->grab_image(i);
52
53     PoseCellSet cell_set;
54     add_root_cells(image, &cell_set);
55
56     for(int t = 0; t < image->nb_targets(); t++) {
57       Pose pose = *image->get_target_pose(t);
58       Pose coarse;
59
60       cell_set.get_containing_cell(&pose)->get_centroid(&coarse);
61
62       targets[u]._belly_xc.set((pose._belly_xc - coarse._head_xc) / coarse._head_radius);
63       targets[u]._belly_yc.set((pose._belly_yc - coarse._head_yc) / coarse._head_radius);
64       u++;
65
66       pose.horizontal_flip(image->width());
67
68       cell_set.get_containing_cell(&pose)->get_centroid(&coarse);
69
70       targets[u]._belly_xc.set((pose._belly_xc - coarse._head_xc) / coarse._head_radius);
71       targets[u]._belly_yc.set((pose._belly_yc - coarse._head_yc) / coarse._head_radius);
72       u++;
73     }
74
75     train_pool->release_image(i);
76   }
77
78   scalar_t fattening = 1.1;
79
80   Interval belly_rxc, belly_ryc;
81
82   belly_rxc.set(&targets[0]._belly_xc);
83   belly_ryc.set(&targets[0]._belly_yc);
84
85   for(int t = 0; t < nb_total_targets; t++) {
86     belly_rxc.swallow(&targets[t]._belly_xc);
87     belly_ryc.swallow(&targets[t]._belly_yc);
88   }
89
90   belly_rxc.min *= fattening;
91   belly_rxc.max *= fattening;
92   belly_ryc.min *= fattening;
93   belly_ryc.max *= fattening;
94
95   scalar_t belly_rxc_min = belly_resolution * floor(belly_rxc.min / belly_resolution);
96   int nb_belly_rxc = int(ceil((belly_rxc.max - belly_rxc_min) / belly_resolution));
97
98   scalar_t belly_ryc_min = belly_resolution * floor(belly_ryc.min / belly_resolution);
99   int nb_belly_ryc = int(ceil((belly_ryc.max - belly_ryc_min) / belly_resolution));
100
101   int used[nb_belly_rxc * nb_belly_rxc];
102
103   for(int k = 0; k < nb_belly_rxc * nb_belly_ryc; k++) {
104     used[k] = 1;
105   }
106
107   // An ugly way to compute the convexe enveloppe
108
109   for(scalar_t alpha = 0; alpha < M_PI * 2; alpha += (2 * M_PI) / 100) {
110     scalar_t vx = cos(alpha), vy = sin(alpha);
111     scalar_t rho = 0;
112
113     for(int t = 0; t < nb_total_targets; t++) {
114       rho = min(rho, vx * targets[t]._belly_xc.middle() + vy * targets[t]._belly_yc.middle());
115     }
116
117     rho *= fattening;
118
119     for(int j = 0; j < nb_belly_ryc; j++) {
120       for(int i = 0; i < nb_belly_rxc; i++) {
121         if(
122            vx * (scalar_t(i + 0) * belly_resolution + belly_rxc_min) +
123            vy * (scalar_t(j + 0) * belly_resolution + belly_ryc_min) < rho
124            &&
125            vx * (scalar_t(i + 1) * belly_resolution + belly_rxc_min) +
126            vy * (scalar_t(j + 0) * belly_resolution + belly_ryc_min) < rho
127            &&
128            vx * (scalar_t(i + 0) * belly_resolution + belly_rxc_min) +
129            vy * (scalar_t(j + 1) * belly_resolution + belly_ryc_min) < rho
130            &&
131            vx * (scalar_t(i + 1) * belly_resolution + belly_rxc_min) +
132            vy * (scalar_t(j + 1) * belly_resolution + belly_ryc_min) < rho
133            ) {
134           used[i + j * nb_belly_rxc] = 0;
135         }
136       }
137     }
138   }
139
140   _nb_belly_cells = 0;
141   for(int j = 0; j < nb_belly_ryc; j++) {
142     for(int i = 0; i < nb_belly_rxc; i++) {
143       if(used[i + nb_belly_rxc * j]) {
144         _nb_belly_cells++;
145       }
146     }
147   }
148
149   _belly_cells = new RelativeBellyPoseCell[_nb_belly_cells];
150
151   int k = 0;
152   for(int j = 0; j < nb_belly_ryc; j++) {
153     for(int i = 0; i < nb_belly_rxc; i++) {
154
155       if(used[i + nb_belly_rxc * j]) {
156
157         RelativeBellyPoseCell mother;
158
159         scalar_t x = scalar_t(i) * belly_resolution + belly_rxc_min;
160         scalar_t y = scalar_t(j) * belly_resolution + belly_ryc_min;
161
162         mother._belly_xc.set(x, x + belly_resolution);
163         mother._belly_yc.set(y, y + belly_resolution);
164
165         _belly_cells[k++] = mother;
166       }
167     }
168   }
169 }
170
171 PoseCellHierarchy::~PoseCellHierarchy() {
172   delete[] _belly_cells;
173 }
174
175 int PoseCellHierarchy::nb_levels() {
176   return _nb_levels;
177 }
178
179 void PoseCellHierarchy::get_containing_cell(Image *image, int level,
180                                             Pose *pose, PoseCell *result_cell) {
181   PoseCellSet cell_set;
182
183   for(int l = 0; l < level + 1; l++) {
184     cell_set.erase_content();
185     if(l == 0) {
186       add_root_cells(image, &cell_set);
187     } else {
188       add_subcells(l, result_cell, &cell_set);
189     }
190
191     *result_cell = *(cell_set.get_containing_cell(pose));
192   }
193 }
194
195 void PoseCellHierarchy::add_root_cells(Image *image, PoseCellSet *cell_set) {
196
197   const int nb_scales = int((log(_max_head_radius) - log(_min_head_radius)) / log(2) *
198                             global.nb_scales_per_power_of_two);
199
200   scalar_t alpha = log(_min_head_radius);
201   scalar_t beta = log(2) / scalar_t(global.nb_scales_per_power_of_two);
202
203   for(int s = 0; s < nb_scales; s++) {
204     scalar_t cell_xy_size = exp(alpha + scalar_t(s) * beta) / global.root_cell_nb_xy_per_radius;
205     PoseCell cell;
206     cell._head_radius.min = exp(alpha + scalar_t(s) * beta);
207     cell._head_radius.max = exp(alpha + scalar_t(s+1) * beta);
208     cell._head_tilt.min = -M_PI;
209     cell._head_tilt.max = M_PI;
210     for(scalar_t y = 0; y < image->height(); y += cell_xy_size)
211       for(scalar_t x = 0; x < image->width(); x += cell_xy_size) {
212         cell._head_xc.min = x;
213         cell._head_xc.max = x + cell_xy_size;
214         cell._head_yc.min = y;
215         cell._head_yc.max = y + cell_xy_size;
216         cell._belly_xc.min = cell._head_xc.min - pseudo_infty;
217         cell._belly_xc.max = cell._head_xc.max + pseudo_infty;
218         cell._belly_yc.min = cell._head_yc.min - pseudo_infty;
219         cell._belly_yc.max = cell._head_yc.max + pseudo_infty;
220         cell_set->add_cell(&cell);
221       }
222   }
223 }
224
225 void PoseCellHierarchy::add_subcells(int level, PoseCell *root,
226                                      PoseCellSet *cell_set) {
227
228   switch(level) {
229
230   case 1:
231     {
232       // Here we split the belly-center coordinate cell part
233       PoseCell cell = *root;
234       scalar_t r = sqrt(cell._head_radius.min * cell._head_radius.max);
235       scalar_t x = (cell._head_xc.min + cell._head_xc.max) / 2.0;
236       scalar_t y = (cell._head_yc.min + cell._head_yc.max) / 2.0;
237       for(int k = 0; k < _nb_belly_cells; k++) {
238         cell._belly_xc.min = (_belly_cells[k]._belly_xc.min * r) + x;
239         cell._belly_xc.max = (_belly_cells[k]._belly_xc.max * r) + x;
240         cell._belly_yc.min = (_belly_cells[k]._belly_yc.min * r) + y;
241         cell._belly_yc.max = (_belly_cells[k]._belly_yc.max * r) + y;
242         cell_set->add_cell(&cell);
243       }
244     }
245     break;
246
247   default:
248     {
249       cerr << "Inconsistent level in PoseCellHierarchy::add_subcells" << endl;
250       abort();
251     }
252     break;
253   }
254 }
255
256
257 int PoseCellHierarchy::nb_incompatible_poses(LabelledImagePool *pool) {
258   PoseCell target_cell;
259   PoseCellSet cell_set;
260   LabelledImage *image;
261
262   int nb_errors = 0;
263
264   for(int i = 0; i < pool->nb_images(); i++) {
265     image = pool->grab_image(i);
266
267     for(int t = 0; t < image->nb_targets(); t++) {
268       cell_set.erase_content();
269
270       int error_level = -1;
271
272       for(int l = 0; error_level < 0 && l < _nb_levels; l++) {
273         cell_set.erase_content();
274
275         if(l == 0) {
276           add_root_cells(image, &cell_set);
277         } else {
278           add_subcells(l, &target_cell, &cell_set);
279         }
280
281         int nb_compliant = 0;
282
283         for(int c = 0; c < cell_set.nb_cells(); c++) {
284           if(cell_set.get_cell(c)->contains(image->get_target_pose(t))) {
285             target_cell = *(cell_set.get_cell(c));
286             nb_compliant++;
287           }
288         }
289
290         if(nb_compliant != 1) {
291           error_level = l;
292         }
293       }
294
295       if(error_level >= 0) {
296         nb_errors++;
297       }
298     }
299
300     pool->release_image(i);
301   }
302
303   return nb_errors;
304 }
305
306 void PoseCellHierarchy::write(ostream *os) {
307   write_var(os, &_min_head_radius);
308   write_var(os, &_max_head_radius);
309   write_var(os, &_root_cell_nb_xy_per_radius);
310   write_var(os, &_nb_belly_cells);
311   for(int k = 0; k < _nb_belly_cells; k++)
312     write_var(os, &_belly_cells[k]);
313 }
314
315 void PoseCellHierarchy::read(istream *is) {
316   delete[] _belly_cells;
317   read_var(is, &_min_head_radius);
318   read_var(is, &_max_head_radius);
319   read_var(is, &_root_cell_nb_xy_per_radius);
320   read_var(is, &_nb_belly_cells);
321   delete[] _belly_cells;
322   _belly_cells = new RelativeBellyPoseCell[_nb_belly_cells];
323   for(int k = 0; k < _nb_belly_cells; k++) {
324     read_var(is, &_belly_cells[k]);
325   }
326 }