2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
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.
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.
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/>.
25 #include "materials.h"
26 #include "boosted_classifier.h"
27 #include "parsing_pool.h"
28 #include "rgb_image_subpixel.h"
30 void write_referential_png(char *filename,
33 PiReferential *referential,
36 scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
38 RGBImage result(int(image->width() * s), int(image->height() * s));
40 for(int y = 0; y < result.height(); y++) {
41 for(int x = 0; x < result.width(); x++) {
46 for(int b = 0; b < RichImage::nb_gray_tags; b++) {
47 c += (b * 256)/RichImage::nb_gray_tags *
48 image->nb_tags_in_window(referential->common_scale(),
49 RichImage::first_gray_tag + b,
56 // for(int b = 0; b < RichImage::nb_edge_tags; b++) {
57 // c += image->nb_tags_in_window(referential->common_scale(),
58 // RichImage::first_edge_tag + b,
62 // c = 255 - (c * 255)/RichImage::nb_edge_tags;
64 // THRESHOLDED VARIANCE
66 // c = image->nb_tags_in_window(referential->common_scale(),
67 // RichImage::variance_tag,
72 result.set_pixel(x, y, c, c, c);
76 RGBImageSubpixel result_sp(&result);
79 pf->draw(&result_sp, 255, 255, 0, referential);
81 referential->draw(&result_sp, level);
84 cout << "Writing " << filename << endl;
85 result_sp.write_png(filename);
88 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
91 char buffer[buffer_size];
97 PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
99 if(global.material_feature_nb < 0) {
100 for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
101 image = pool->grab_image(i);
102 RGBImage result(image->width(), image->height());
103 image->to_rgb(&result);
104 RGBImageSubpixel result_sp(&result);
106 if(global.pictures_for_article) {
107 for(int t = 0; t < image->nb_targets(); t++) {
108 image->get_target_pose(t)->draw(8, 255, 255, 255,
109 hierarchy->nb_levels() - 1, &result_sp);
112 for(int t = 0; t < image->nb_targets(); t++) {
113 image->get_target_pose(t)->draw(4, 0, 0, 0,
114 hierarchy->nb_levels() - 1, &result_sp);
117 for(int t = 0; t < image->nb_targets(); t++) {
118 image->get_target_pose(t)->draw(4, 255, 128, 0,
119 hierarchy->nb_levels() - 1, &result_sp);
123 sprintf(buffer, "/tmp/truth-%05d.png", i);
124 cout << "Writing " << buffer << endl;
125 result_sp.write_png(buffer);
126 pool->release_image(i);
130 for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
131 image = pool->grab_image(i);
133 RGBImage result(image->width(), image->height());
134 image->to_rgb(&result);
135 RGBImageSubpixel result_sp(&result);
137 // image->compute_rich_structure();
139 for(int t = 0; t < image->nb_targets(); t++) {
141 image->get_target_pose(t)->draw(4, 255, 0, 0,
142 hierarchy->nb_levels() - 1, &result_sp);
144 hierarchy->get_containing_cell(image,
145 hierarchy->nb_levels() - 1,
146 image->get_target_pose(t), &target_cell);
148 target_cell.get_centroid(&p);
150 p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
152 PiReferential referential(&target_cell);
154 image->compute_rich_structure();
156 if(global.material_feature_nb < 0) {
157 sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, t);
158 write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
159 } else if(detector) {
161 int n_feature = global.material_feature_nb;
162 while(n_feature > detector->_pi_feature_families[n_family]->nb_features()) {
164 n_feature -= detector->_pi_feature_families[n_family]->nb_features();
166 pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
167 sprintf(buffer, "/tmp/pf-%05d-%02d-%05d.png", i, t, global.material_feature_nb);
168 write_referential_png(buffer,
169 hierarchy->nb_levels() - 1,
176 pool->release_image(i);
182 void write_image_with_detections(const char *filename,
183 LabelledImage *image,
184 PoseCellSet *detections,
187 RGBImage result(image->width(), image->height());
189 for(int y = 0; y < result.height(); y++) {
190 for(int x = 0; x < result.width(); x++) {
191 int c = image->value(x, y);
192 result.set_pixel(x, y, c, c, c);
196 RGBImageSubpixel result_sp(&result);
198 if(global.pictures_for_article) {
199 for(int a = 0; a < detections->nb_cells(); a++) {
201 detections->get_cell(a)->get_centroid(&pose);
202 pose.draw(8, 255, 255, 255, level, &result_sp);
204 for(int a = 0; a < detections->nb_cells(); a++) {
206 detections->get_cell(a)->get_centroid(&pose);
207 pose.draw(4, 0, 0, 0, level, &result_sp);
210 for(int a = 0; a < detections->nb_cells(); a++) {
212 detections->get_cell(a)->get_centroid(&pose);
213 pose.draw(4, 255, 128, 0, level, &result_sp);
217 cout << "Writing " << filename << endl;
218 result_sp.write_png(filename);