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 as published
13 * by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
16 * folded-ctf is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with folded-ctf. If not, see <http://www.gnu.org/licenses/>.
26 #include "materials.h"
27 #include "boosted_classifier.h"
28 #include "parsing_pool.h"
29 #include "rgb_image_subpixel.h"
31 void write_referential_png(char *filename,
34 PiReferential *referential,
37 scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
39 RGBImage result(int(image->width() * s), int(image->height() * s));
41 for(int y = 0; y < result.height(); y++) {
42 for(int x = 0; x < result.width(); x++) {
47 for(int b = 0; b < RichImage::nb_gray_tags; b++) {
48 c += (b * 256)/RichImage::nb_gray_tags *
49 image->nb_tags_in_window(referential->common_scale(),
50 RichImage::first_gray_tag + b,
57 // for(int b = 0; b < RichImage::nb_edge_tags; b++) {
58 // c += image->nb_tags_in_window(referential->common_scale(),
59 // RichImage::first_edge_tag + b,
63 // c = 255 - (c * 255)/RichImage::nb_edge_tags;
65 // THRESHOLDED VARIANCE
67 // c = image->nb_tags_in_window(referential->common_scale(),
68 // RichImage::variance_tag,
73 result.set_pixel(x, y, c, c, c);
77 RGBImageSubpixel result_sp(&result);
80 pf->draw(&result_sp, 255, 255, 0, referential);
82 referential->draw(&result_sp, level);
85 cout << "Writing " << filename << endl;
86 result_sp.write_png(filename);
89 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
92 char buffer[buffer_size];
98 PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
100 if(global.material_feature_nb < 0) {
101 for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
102 image = pool->grab_image(i);
103 RGBImage result(image->width(), image->height());
104 image->to_rgb(&result);
105 RGBImageSubpixel result_sp(&result);
107 if(global.pictures_for_article) {
108 for(int t = 0; t < image->nb_targets(); t++) {
109 image->get_target_pose(t)->draw(8, 255, 255, 255,
110 hierarchy->nb_levels() - 1, &result_sp);
113 for(int t = 0; t < image->nb_targets(); t++) {
114 image->get_target_pose(t)->draw(4, 0, 0, 0,
115 hierarchy->nb_levels() - 1, &result_sp);
118 for(int t = 0; t < image->nb_targets(); t++) {
119 image->get_target_pose(t)->draw(4, 255, 128, 0,
120 hierarchy->nb_levels() - 1, &result_sp);
124 sprintf(buffer, "/tmp/truth-%05d.png", i);
125 cout << "Writing " << buffer << endl;
126 result_sp.write_png(buffer);
127 pool->release_image(i);
131 for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
132 image = pool->grab_image(i);
134 RGBImage result(image->width(), image->height());
135 image->to_rgb(&result);
136 RGBImageSubpixel result_sp(&result);
138 // image->compute_rich_structure();
140 for(int t = 0; t < image->nb_targets(); t++) {
142 image->get_target_pose(t)->draw(4, 255, 0, 0,
143 hierarchy->nb_levels() - 1, &result_sp);
145 hierarchy->get_containing_cell(image,
146 hierarchy->nb_levels() - 1,
147 image->get_target_pose(t), &target_cell);
149 target_cell.get_centroid(&p);
151 p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
153 PiReferential referential(&target_cell);
155 image->compute_rich_structure();
157 if(global.material_feature_nb < 0) {
158 sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, t);
159 write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
160 } else if(detector) {
162 int n_feature = global.material_feature_nb;
163 while(n_feature > detector->_pi_feature_families[n_family]->nb_features()) {
165 n_feature -= detector->_pi_feature_families[n_family]->nb_features();
167 pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
168 sprintf(buffer, "/tmp/pf-%05d-%02d-%05d.png", i, t, global.material_feature_nb);
169 write_referential_png(buffer,
170 hierarchy->nb_levels() - 1,
177 pool->release_image(i);
183 void write_image_with_detections(const char *filename,
184 LabelledImage *image,
185 PoseCellSet *detections,
188 RGBImage result(image->width(), image->height());
190 for(int y = 0; y < result.height(); y++) {
191 for(int x = 0; x < result.width(); x++) {
192 int c = image->value(x, y);
193 result.set_pixel(x, y, c, c, c);
197 RGBImageSubpixel result_sp(&result);
199 if(global.pictures_for_article) {
200 for(int a = 0; a < detections->nb_cells(); a++) {
202 detections->get_cell(a)->get_centroid(&pose);
203 pose.draw(8, 255, 255, 255, level, &result_sp);
205 for(int a = 0; a < detections->nb_cells(); a++) {
207 detections->get_cell(a)->get_centroid(&pose);
208 pose.draw(4, 0, 0, 0, level, &result_sp);
211 for(int a = 0; a < detections->nb_cells(); a++) {
213 detections->get_cell(a)->get_centroid(&pose);
214 pose.draw(4, 255, 128, 0, level, &result_sp);
218 cout << "Writing " << filename << endl;
219 result_sp.write_png(filename);