automatic commit
[folded-ctf.git] / materials.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 as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
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.
20  *
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/>.
23  *
24  */
25
26 #include "materials.h"
27 #include "boosted_classifier.h"
28 #include "parsing_pool.h"
29 #include "rgb_image_subpixel.h"
30
31 void write_referential_png(char *filename,
32                            int level,
33                            RichImage *image,
34                            PiReferential *referential,
35                            PiFeature *pf) {
36
37   scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
38
39   RGBImage result(int(image->width() * s), int(image->height() * s));
40
41   for(int y = 0; y < result.height(); y++) {
42     for(int x = 0; x < result.width(); x++) {
43       int c = 0;
44
45       // GRAYSCALES
46
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,
51                                    x, y,
52                                    x + 1, y + 1);
53       }
54
55       // EDGES
56
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,
60       //                                       x, y,
61       //                                       x + 1, y + 1);
62       //       }
63       //       c = 255 - (c * 255)/RichImage::nb_edge_tags;
64
65       // THRESHOLDED VARIANCE
66
67       //       c = image->nb_tags_in_window(referential->common_scale(),
68       //                                    RichImage::variance_tag,
69       //                                    x, y,
70       //                                    x + 1, y + 1);
71       //       c = (1 - c) * 255;
72
73       result.set_pixel(x, y, c, c, c);
74     }
75   }
76
77   RGBImageSubpixel result_sp(&result);
78
79   if(pf) {
80     pf->draw(&result_sp, 255, 255, 0, referential);
81   } else {
82     referential->draw(&result_sp, level);
83   }
84
85   cout << "Writing " << filename << endl;
86   result_sp.write_png(filename);
87 }
88
89 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
90                                                    Detector *detector) {
91   LabelledImage *image;
92   char buffer[buffer_size];
93
94   PoseCell target_cell;
95   Pose p;
96   PiFeature *pf;
97
98   PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
99
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);
106
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);
111
112         }
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);
116         }
117       } else {
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);
121         }
122       }
123
124       sprintf(buffer, "/tmp/truth-%05d.png", i);
125       cout << "Writing " << buffer << endl;
126       result_sp.write_png(buffer);
127       pool->release_image(i);
128     }
129   }
130
131   for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
132     image = pool->grab_image(i);
133
134     RGBImage result(image->width(), image->height());
135     image->to_rgb(&result);
136     RGBImageSubpixel result_sp(&result);
137
138     // image->compute_rich_structure();
139
140     for(int t = 0; t < image->nb_targets(); t++) {
141
142       image->get_target_pose(t)->draw(4, 255, 0, 0,
143                                       hierarchy->nb_levels() - 1, &result_sp);
144
145       hierarchy->get_containing_cell(image,
146                                      hierarchy->nb_levels() - 1,
147                                      image->get_target_pose(t), &target_cell);
148
149       target_cell.get_centroid(&p);
150
151       p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
152
153       PiReferential referential(&target_cell);
154
155       image->compute_rich_structure();
156
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) {
161         int n_family = 0;
162         int n_feature = global.material_feature_nb;
163         while(n_feature > detector->_pi_feature_families[n_family]->nb_features()) {
164           n_family++;
165           n_feature -= detector->_pi_feature_families[n_family]->nb_features();
166         }
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,
171                               image,
172                               &referential,
173                               pf);
174       }
175     }
176
177     pool->release_image(i);
178   }
179
180   delete hierarchy;
181 }
182
183 void write_image_with_detections(const char *filename,
184                                  LabelledImage *image,
185                                  PoseCellSet *detections,
186                                  int level) {
187
188   RGBImage result(image->width(), image->height());
189
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);
194     }
195   }
196
197   RGBImageSubpixel result_sp(&result);
198
199   if(global.pictures_for_article) {
200     for(int a = 0; a < detections->nb_cells(); a++) {
201       Pose pose;
202       detections->get_cell(a)->get_centroid(&pose);
203       pose.draw(8, 255, 255, 255, level, &result_sp);
204     }
205     for(int a = 0; a < detections->nb_cells(); a++) {
206       Pose pose;
207       detections->get_cell(a)->get_centroid(&pose);
208       pose.draw(4,   0,   0,   0, level, &result_sp);
209     }
210   } else {
211     for(int a = 0; a < detections->nb_cells(); a++) {
212       Pose pose;
213       detections->get_cell(a)->get_centroid(&pose);
214       pose.draw(4, 255, 128, 0, level, &result_sp);
215     }
216   }
217
218   cout << "Writing " << filename << endl;
219   result_sp.write_png(filename);
220 }