2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
19 #include "pose_cell_hierarchy.h"
22 PoseCellHierarchy::PoseCellHierarchy() {
26 PoseCellHierarchy::PoseCellHierarchy(LabelledImagePool *train_pool) {
27 _nb_levels = global.nb_levels;
28 _min_head_radius = global.min_head_radius;
29 _max_head_radius = global.max_head_radius;
30 _root_cell_nb_xy_per_scale = global.root_cell_nb_xy_per_radius;
33 int nb_total_targets = 0;
34 for(int i = 0; i < train_pool->nb_images(); i++) {
35 image = train_pool->grab_image(i);
36 // We are going to symmetrize
37 nb_total_targets += 2 * image->nb_targets();
38 train_pool->release_image(i);
41 RelativeBellyPoseCell targets[nb_total_targets];
44 for(int i = 0; i < train_pool->nb_images(); i++) {
45 image = train_pool->grab_image(i);
48 add_root_cells(image, &cell_set);
50 for(int t = 0; t < image->nb_targets(); t++) {
51 Pose pose = *image->get_target_pose(t);
54 cell_set.get_containing_cell(&pose)->get_centroid(&coarse);
56 targets[u]._belly_xc.set((pose._belly_xc - coarse._head_xc) / coarse._head_radius);
57 targets[u]._belly_yc.set((pose._belly_yc - coarse._head_yc) / coarse._head_radius);
60 pose.horizontal_flip(image->width());
62 cell_set.get_containing_cell(&pose)->get_centroid(&coarse);
64 targets[u]._belly_xc.set((pose._belly_xc - coarse._head_xc) / coarse._head_radius);
65 targets[u]._belly_yc.set((pose._belly_yc - coarse._head_yc) / coarse._head_radius);
69 train_pool->release_image(i);
72 scalar_t fattening = 1.1;
74 Interval belly_rxc, belly_ryc;
76 belly_rxc.set(&targets[0]._belly_xc);
77 belly_ryc.set(&targets[0]._belly_yc);
79 for(int t = 0; t < nb_total_targets; t++) {
80 belly_rxc.swallow(&targets[t]._belly_xc);
81 belly_ryc.swallow(&targets[t]._belly_yc);
84 belly_rxc.min *= fattening;
85 belly_rxc.max *= fattening;
86 belly_ryc.min *= fattening;
87 belly_ryc.max *= fattening;
89 scalar_t belly_rxc_min = belly_resolution * floor(belly_rxc.min / belly_resolution);
90 int nb_belly_rxc = int(ceil((belly_rxc.max - belly_rxc_min) / belly_resolution));
92 scalar_t belly_ryc_min = belly_resolution * floor(belly_ryc.min / belly_resolution);
93 int nb_belly_ryc = int(ceil((belly_ryc.max - belly_ryc_min) / belly_resolution));
95 int used[nb_belly_rxc * nb_belly_rxc];
97 for(int k = 0; k < nb_belly_rxc * nb_belly_ryc; k++) {
101 // An ugly way to compute the convexe enveloppe
103 for(scalar_t alpha = 0; alpha < M_PI * 2; alpha += (2 * M_PI) / 100) {
104 scalar_t vx = cos(alpha), vy = sin(alpha);
107 for(int t = 0; t < nb_total_targets; t++) {
108 rho = min(rho, vx * targets[t]._belly_xc.middle() + vy * targets[t]._belly_yc.middle());
113 for(int j = 0; j < nb_belly_ryc; j++) {
114 for(int i = 0; i < nb_belly_rxc; i++) {
116 vx * (scalar_t(i + 0) * belly_resolution + belly_rxc_min) +
117 vy * (scalar_t(j + 0) * belly_resolution + belly_ryc_min) < rho
119 vx * (scalar_t(i + 1) * belly_resolution + belly_rxc_min) +
120 vy * (scalar_t(j + 0) * belly_resolution + belly_ryc_min) < rho
122 vx * (scalar_t(i + 0) * belly_resolution + belly_rxc_min) +
123 vy * (scalar_t(j + 1) * belly_resolution + belly_ryc_min) < rho
125 vx * (scalar_t(i + 1) * belly_resolution + belly_rxc_min) +
126 vy * (scalar_t(j + 1) * belly_resolution + belly_ryc_min) < rho
128 used[i + j * nb_belly_rxc] = 0;
135 for(int j = 0; j < nb_belly_ryc; j++) {
136 for(int i = 0; i < nb_belly_rxc; i++) {
137 if(used[i + nb_belly_rxc * j]) {
143 _belly_cells = new RelativeBellyPoseCell[_nb_belly_cells];
146 for(int j = 0; j < nb_belly_ryc; j++) {
147 for(int i = 0; i < nb_belly_rxc; i++) {
149 if(used[i + nb_belly_rxc * j]) {
151 RelativeBellyPoseCell mother;
153 scalar_t x = scalar_t(i) * belly_resolution + belly_rxc_min;
154 scalar_t y = scalar_t(j) * belly_resolution + belly_ryc_min;
156 mother._belly_xc.set(x, x + belly_resolution);
157 mother._belly_yc.set(y, y + belly_resolution);
159 _belly_cells[k++] = mother;
165 PoseCellHierarchy::~PoseCellHierarchy() {
166 delete[] _belly_cells;
169 int PoseCellHierarchy::nb_levels() {
173 void PoseCellHierarchy::get_containing_cell(Image *image, int level,
174 Pose *pose, PoseCell *result_cell) {
175 PoseCellSet cell_set;
177 for(int l = 0; l < level + 1; l++) {
178 cell_set.erase_content();
180 add_root_cells(image, &cell_set);
182 add_subcells(l, result_cell, &cell_set);
185 *result_cell = *(cell_set.get_containing_cell(pose));
189 void PoseCellHierarchy::add_root_cells(Image *image, PoseCellSet *cell_set) {
191 const int nb_scales = int((log(_max_head_radius) - log(_min_head_radius)) / log(2) *
192 global.nb_scales_per_power_of_two);
194 scalar_t alpha = log(_min_head_radius);
195 scalar_t beta = log(2) / scalar_t(global.nb_scales_per_power_of_two);
197 for(int s = 0; s < nb_scales; s++) {
198 scalar_t cell_xy_size = exp(alpha + scalar_t(s) * beta) / global.root_cell_nb_xy_per_radius;
200 cell._head_radius.min = exp(alpha + scalar_t(s) * beta);
201 cell._head_radius.max = exp(alpha + scalar_t(s+1) * beta);
202 cell._head_tilt.min = -M_PI;
203 cell._head_tilt.max = M_PI;
204 for(scalar_t y = 0; y < image->height(); y += cell_xy_size)
205 for(scalar_t x = 0; x < image->width(); x += cell_xy_size) {
206 cell._head_xc.min = x;
207 cell._head_xc.max = x + cell_xy_size;
208 cell._head_yc.min = y;
209 cell._head_yc.max = y + cell_xy_size;
210 cell._belly_xc.min = cell._head_xc.min - pseudo_infty;
211 cell._belly_xc.max = cell._head_xc.max + pseudo_infty;
212 cell._belly_yc.min = cell._head_yc.min - pseudo_infty;
213 cell._belly_yc.max = cell._head_yc.max + pseudo_infty;
214 cell_set->add_cell(&cell);
219 void PoseCellHierarchy::add_subcells(int level, PoseCell *root,
220 PoseCellSet *cell_set) {
226 // Here we split the belly-center coordinate cell part
227 PoseCell cell = *root;
228 scalar_t r = sqrt(cell._head_radius.min * cell._head_radius.max);
229 scalar_t x = (cell._head_xc.min + cell._head_xc.max) / 2.0;
230 scalar_t y = (cell._head_yc.min + cell._head_yc.max) / 2.0;
231 for(int k = 0; k < _nb_belly_cells; k++) {
232 cell._belly_xc.min = (_belly_cells[k]._belly_xc.min * r) + x;
233 cell._belly_xc.max = (_belly_cells[k]._belly_xc.max * r) + x;
234 cell._belly_yc.min = (_belly_cells[k]._belly_yc.min * r) + y;
235 cell._belly_yc.max = (_belly_cells[k]._belly_yc.max * r) + y;
236 cell_set->add_cell(&cell);
243 cerr << "Inconsistent level in PoseCellHierarchy::add_subcells" << endl;
251 int PoseCellHierarchy::nb_incompatible_poses(LabelledImagePool *pool) {
252 PoseCell target_cell;
253 PoseCellSet cell_set;
254 LabelledImage *image;
258 for(int i = 0; i < pool->nb_images(); i++) {
259 image = pool->grab_image(i);
261 for(int t = 0; t < image->nb_targets(); t++) {
262 cell_set.erase_content();
264 int error_level = -1;
266 for(int l = 0; error_level < 0 && l < _nb_levels; l++) {
267 cell_set.erase_content();
270 add_root_cells(image, &cell_set);
272 add_subcells(l, &target_cell, &cell_set);
275 int nb_compliant = 0;
277 for(int c = 0; c < cell_set.nb_cells(); c++) {
278 if(cell_set.get_cell(c)->contains(image->get_target_pose(t))) {
279 target_cell = *(cell_set.get_cell(c));
284 if(nb_compliant != 1) {
289 if(error_level >= 0) {
294 pool->release_image(i);
300 void PoseCellHierarchy::write(ostream *os) {
301 write_var(os, &_min_head_radius);
302 write_var(os, &_max_head_radius);
303 write_var(os, &_root_cell_nb_xy_per_scale);
304 write_var(os, &_nb_belly_cells);
305 for(int k = 0; k < _nb_belly_cells; k++)
306 write_var(os, &_belly_cells[k]);
309 void PoseCellHierarchy::read(istream *is) {
310 delete[] _belly_cells;
311 read_var(is, &_min_head_radius);
312 read_var(is, &_max_head_radius);
313 read_var(is, &_root_cell_nb_xy_per_scale);
314 read_var(is, &_nb_belly_cells);
315 delete[] _belly_cells;
316 _belly_cells = new RelativeBellyPoseCell[_nb_belly_cells];
317 for(int k = 0; k < _nb_belly_cells; k++) {
318 read_var(is, &_belly_cells[k]);