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 "pi_feature.h"
22 #include "rectangle.h"
24 int PiFeature::random_registration_mode(int level) {
26 switch(int(6 * drand48())) {
29 return PiReferential::RM_HEAD;
34 return PiReferential::RM_BELLY;
39 return PiReferential::RM_HEAD_BELLY;
44 return PiReferential::RM_HEAD_BELLY_EDGES;
57 void PiFeature::randomize_window(int registration_mode, Rectangle *window) {
58 scalar_t xc, yc, w, h;
61 xc = 2 * drand48() - 1.0;
62 yc = 2 * drand48() - 1.0;
66 // If we are in a non-rotating frame, allow rectangular window,
67 // otherwise force it to be squared
69 if(registration_mode == PiReferential::RM_HEAD ||
70 registration_mode == PiReferential::RM_BELLY ||
71 registration_mode == PiReferential::RM_HEAD_NO_POLARITY ||
72 registration_mode == PiReferential::RM_BELLY_NO_POLARITY) {
77 } while(w < global.pi_feature_window_min_size ||
78 h < global.pi_feature_window_min_size ||
79 xc - w/2 < -1.0 || xc + w/2 > 1.0 ||
80 yc - h/2 < -1.0 || yc + h/2 > 1.0);
82 window->xmin = xc - w/2;
83 window->ymin = yc - h/2;
84 window->xmax = xc + w/2;
85 window->ymax = yc + h/2;
88 //////////////////////////////////////////////////////////////////////
89 // PF_EDGE_THRESHOLDING
91 scalar_t PiFeature::response_edge_thresholding(RichImage *image,
92 PiReferential *referential) {
93 Rectangle registered_window_a;
95 referential->register_rectangle(_registration_a,
97 ®istered_window_a);
99 int tag = referential->register_edge(_registration_a, _tag);
101 int xmin_a = int(registered_window_a.xmin) >> _edge_scale;
102 int ymin_a = int(registered_window_a.ymin) >> _edge_scale;
103 int xmax_a = int(registered_window_a.xmax) >> _edge_scale;
104 int ymax_a = int(registered_window_a.ymax) >> _edge_scale;
106 int scale = referential->common_scale() + _edge_scale * global.nb_scales_per_power_of_two;
110 ASSERT((tag >= RichImage::first_edge_tag &&
111 tag < RichImage::first_edge_tag + RichImage::nb_edge_tags) ||
112 tag == RichImage::variance_tag);
114 if(tag != RichImage::variance_tag) {
115 ne = scalar_t(image->nb_tags_in_window(scale, tag,
116 xmin_a, ymin_a, xmax_a, ymax_a));
117 nt = scalar_t(image->nb_tags_in_window(scale, RichImage::variance_tag,
118 xmin_a, ymin_a, xmax_a, ymax_a) + 1);
120 ne = scalar_t(image->nb_tags_in_window(scale, RichImage::variance_tag,
121 xmin_a, ymin_a, xmax_a, ymax_a));
122 nt = scalar_t((xmax_a - xmin_a) * (ymax_a - ymin_a)) + 1;
128 void PiFeature::draw_edge_thresholding(RGBImage *image,
130 PiReferential *referential) {
132 (*global.log_stream) << "draw_edge_thresholding" << endl;
134 Rectangle registered_window_a;
136 referential->register_rectangle(_registration_a,
138 ®istered_window_a);
140 referential->draw_window(image, _registration_a, ®istered_window_a, 0);
143 void PiFeature::print_edge_thresholding(ostream *os) {
144 (*os) << "_tag " << _tag << endl;
145 (*os) << "_edge_scale " << _edge_scale << endl;
146 (*os) << "_window_a.xmin " << _window_a.xmin << endl;
147 (*os) << "_window_a.ymin " << _window_a.ymin << endl;
148 (*os) << "_window_a.xmax " << _window_a.xmax << endl;
149 (*os) << "_window_a.ymax " << _window_a.ymax << endl;
152 //////////////////////////////////////////////////////////////////////
153 // PF_EDGE_HISTOGRAM_COMPARISON
155 scalar_t PiFeature::response_edge_histogram_comparison(RichImage *image,
156 PiReferential *referential) {
158 Rectangle registered_window_a;
160 referential->register_rectangle(_registration_a,
162 ®istered_window_a);
164 int xmin_a = int(registered_window_a.xmin) >> _edge_scale;
165 int ymin_a = int(registered_window_a.ymin) >> _edge_scale;
166 int xmax_a = int(registered_window_a.xmax) >> _edge_scale;
167 int ymax_a = int(registered_window_a.ymax) >> _edge_scale;
169 Rectangle registered_window_b;
171 referential->register_rectangle(_registration_b,
173 ®istered_window_b);
175 int xmin_b = int(registered_window_b.xmin) >> _edge_scale;
176 int ymin_b = int(registered_window_b.ymin) >> _edge_scale;
177 int xmax_b = int(registered_window_b.xmax) >> _edge_scale;
178 int ymax_b = int(registered_window_b.ymax) >> _edge_scale;
180 int scale = referential->common_scale() + _edge_scale * global.nb_scales_per_power_of_two;
182 scalar_t result = 0.0;
184 scalar_t ne_a = scalar_t(image->nb_tags_in_window(scale, RichImage::variance_tag,
186 xmax_a, ymax_a) + 1);
188 scalar_t ne_b = scalar_t(image->nb_tags_in_window(scale, RichImage::variance_tag,
190 xmax_b, ymax_b) + 1);
192 for(int t = RichImage::first_edge_tag; t < RichImage::first_edge_tag + RichImage::nb_edge_tags; t++)
193 result += sq(scalar_t(image->nb_tags_in_window(scale, t,
195 xmax_a, ymax_a)) / ne_a
197 scalar_t(image->nb_tags_in_window(scale, t,
199 xmax_b, ymax_b)) / ne_b);
201 ASSERT(!isnan(result));
206 void PiFeature::draw_edge_histogram_comparison(RGBImage *image,
208 PiReferential *referential) {
210 (*global.log_stream) << "draw_edge_histogram_comparison" << endl;
212 Rectangle registered_window;
215 referential->register_rectangle(_registration_a,
218 referential->draw_window(image, _registration_a, ®istered_window, 0);
222 referential->register_rectangle(_registration_b,
226 referential->draw_window(image, _registration_b, ®istered_window, 0);
230 void PiFeature::print_edge_histogram_comparison(ostream *os) {
231 (*os) << "_edge_scale " << _edge_scale << endl;
232 (*os) << "_window_a.xmin " << _window_a.xmin << endl;
233 (*os) << "_window_a.ymin " << _window_a.ymin << endl;
234 (*os) << "_window_a.xmax " << _window_a.xmax << endl;
235 (*os) << "_window_a.ymax " << _window_a.ymax << endl;
236 (*os) << "_window_b.xmin " << _window_a.xmin << endl;
237 (*os) << "_window_b.ymin " << _window_a.ymin << endl;
238 (*os) << "_window_b.xmax " << _window_a.xmax << endl;
239 (*os) << "_window_b.ymax " << _window_a.ymax << endl;
242 //////////////////////////////////////////////////////////////////////
243 // PF_GRAYSCALE_HISTOGRAM_COMPARISON
245 scalar_t PiFeature::response_grayscale_histogram_comparison(RichImage *image,
246 PiReferential *referential) {
247 Rectangle registered_window_a;
249 referential->register_rectangle(_registration_a,
251 ®istered_window_a);
253 int xmin_a = int(registered_window_a.xmin);
254 int ymin_a = int(registered_window_a.ymin);
255 int xmax_a = int(registered_window_a.xmax);
256 int ymax_a = int(registered_window_a.ymax);
258 Rectangle registered_window_b;
260 referential->register_rectangle(_registration_b,
262 ®istered_window_b);
264 int xmin_b = int(registered_window_b.xmin);
265 int ymin_b = int(registered_window_b.ymin);
266 int xmax_b = int(registered_window_b.xmax);
267 int ymax_b = int(registered_window_b.ymax);
269 int scale = referential->common_scale();
271 scalar_t result = 0.0;
273 scalar_t ne_a = scalar_t((xmax_a - xmin_a) * (ymax_a - ymin_a));
274 scalar_t ne_b = scalar_t((xmax_b - xmin_b) * (ymax_b - ymin_b));
276 for(int t = RichImage::first_gray_tag; t < RichImage::first_gray_tag + RichImage::nb_gray_tags; t++)
277 result += sq(scalar_t(image->nb_tags_in_window(scale, t,
279 xmax_a, ymax_a))/ne_a
281 scalar_t(image->nb_tags_in_window(scale, t,
283 xmax_b, ymax_b))/ne_b);
284 ASSERT(!isnan(result));
289 void PiFeature::draw_grayscale_histogram_comparison(RGBImage *image,
291 PiReferential *referential) {
293 (*global.log_stream) << "draw_grayscale_histogram_comparison" << endl;
295 Rectangle registered_window;
298 referential->register_rectangle(_registration_a,
302 referential->draw_window(image, _registration_a, ®istered_window, 0);
306 referential->register_rectangle(_registration_b,
310 referential->draw_window(image, _registration_b, ®istered_window, 0);
314 void PiFeature::print_grayscale_histogram_comparison(ostream *os) {
315 (*os) << "_window_a.xmin " << _window_a.xmin << endl;
316 (*os) << "_window_a.ymin " << _window_a.ymin << endl;
317 (*os) << "_window_a.xmax " << _window_a.xmax << endl;
318 (*os) << "_window_a.ymax " << _window_a.ymax << endl;
319 (*os) << "_window_b.xmin " << _window_a.xmin << endl;
320 (*os) << "_window_b.ymin " << _window_a.ymin << endl;
321 (*os) << "_window_b.xmax " << _window_a.xmax << endl;
322 (*os) << "_window_b.ymax " << _window_a.ymax << endl;
325 //////////////////////////////////////////////////////////////////////
326 //////////////////////////////////////////////////////////////////////
327 //////////////////////////////////////////////////////////////////////
329 void PiFeature::randomize(int level) {
331 // We randomize all parameters, even those which will not be used
332 // due to the feature type
334 _tag = int(drand48() * (RichImage::nb_edge_tags + 1));
336 if(_tag < RichImage::nb_edge_tags)
337 _tag += RichImage::first_edge_tag;
339 _tag = RichImage::variance_tag;
341 _edge_scale = int(drand48() * 3);
343 // Windows can not be defined in different frames unless we allow
344 // head-belly registration
346 if(global.force_head_belly_independence) {
348 _registration_a = PiReferential::RM_HEAD_NO_POLARITY;
349 _registration_b = PiReferential::RM_HEAD_NO_POLARITY;
350 } else if(level == 1) {
351 _registration_a = PiReferential::RM_BELLY_NO_POLARITY;
352 _registration_b = PiReferential::RM_BELLY_NO_POLARITY;
357 _registration_a = random_registration_mode(level);
358 _registration_b = random_registration_mode(level);
361 randomize_window(_registration_a, &_window_a);
362 randomize_window(_registration_b, &_window_b);
364 switch(int(drand48() * 3)) {
367 _type = PF_EDGE_THRESHOLDING;
371 _type = PF_EDGE_HISTOGRAM_COMPARISON;
375 _type = PF_GRAYSCALE_HISTOGRAM_COMPARISON;
383 scalar_t PiFeature::response(RichImage *image, PiReferential *referential) {
388 case PF_EDGE_THRESHOLDING:
389 r = response_edge_thresholding(image, referential);
392 case PF_EDGE_HISTOGRAM_COMPARISON:
393 r = response_edge_histogram_comparison(image, referential);
396 case PF_GRAYSCALE_HISTOGRAM_COMPARISON:
397 r = response_grayscale_histogram_comparison(image, referential);
409 void PiFeature::draw(RGBImage *image,
410 int r, int g, int b, PiReferential *referential) {
414 case PF_EDGE_THRESHOLDING:
415 draw_edge_thresholding(image, r, g, b, referential);
418 case PF_EDGE_HISTOGRAM_COMPARISON:
419 draw_edge_histogram_comparison(image, r, g, b, referential);
422 case PF_GRAYSCALE_HISTOGRAM_COMPARISON:
423 draw_grayscale_histogram_comparison(image, r, g, b, referential);
431 void PiFeature::print(ostream *os) {
433 (*os) << "registration_a ";
434 PiReferential::print_registration_mode(os, _registration_a);
437 (*os) << "registration_b ";
438 PiReferential::print_registration_mode(os, _registration_b);
443 case PF_EDGE_THRESHOLDING:
444 print_edge_thresholding(os);
447 case PF_EDGE_HISTOGRAM_COMPARISON:
448 print_edge_histogram_comparison(os);
451 case PF_GRAYSCALE_HISTOGRAM_COMPARISON:
452 print_grayscale_histogram_comparison(os);