automatic commit
[folded-ctf.git] / materials.cc
1
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.                         //
6 //                                                                       //
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.                              //
11 //                                                                       //
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/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include "materials.h"
20 #include "boosted_classifier.h"
21 #include "parsing_pool.h"
22 #include "rgb_image_subpixel.h"
23
24 void write_referential_png(char *filename,
25                            int level,
26                            RichImage *image,
27                            PiReferential *referential,
28                            PiFeature *pf) {
29
30   scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
31
32   RGBImage result(int(image->width() * s), int(image->height() * s));
33
34   for(int y = 0; y < result.height(); y++) {
35     for(int x = 0; x < result.width(); x++) {
36       int c = 0;
37
38       // GRAYSCALES
39
40       for(int b = 0; b < RichImage::nb_gray_tags; b++) {
41         c += (b * 256)/RichImage::nb_gray_tags *
42           image->nb_tags_in_window(referential->common_scale(),
43                                    RichImage::first_gray_tag + b,
44                                    x, y,
45                                    x + 1, y + 1);
46       }
47
48       // EDGES
49
50       //       for(int b = 0; b < RichImage::nb_edge_tags; b++) {
51       //         c += image->nb_tags_in_window(referential->common_scale(),
52       //                                       RichImage::first_edge_tag + b,
53       //                                       x, y,
54       //                                       x + 1, y + 1);
55       //       }
56       //       c = 255 - (c * 255)/RichImage::nb_edge_tags;
57
58       // THRESHOLDED VARIANCE
59
60       //       c = image->nb_tags_in_window(referential->common_scale(),
61       //                                    RichImage::variance_tag,
62       //                                    x, y,
63       //                                    x + 1, y + 1);
64       //       c = (1 - c) * 255;
65
66       result.set_pixel(x, y, c, c, c);
67     }
68   }
69
70   RGBImageSubpixel result_sp(&result);
71
72   if(pf) {
73     pf->draw(&result_sp, 255, 255, 0, referential);
74   } else {
75     referential->draw(&result_sp, level);
76   }
77
78   (*global.log_stream) << "Writing " << filename << endl;
79   result_sp.write_png(filename);
80 }
81
82 void write_one_pi_feature_png(char *filename,
83                               LabelledImage *image,
84                               PoseCellHierarchy *hierarchy,
85                               int nb_target,
86                               int level,
87                               PiFeature *pf) {
88
89   PoseCell target_cell;
90   hierarchy->get_containing_cell(image, level,
91                                  image->get_target_pose(nb_target), &target_cell);
92   PiReferential referential(&target_cell);
93   RGBImage result(image->width(), image->height());
94   image->to_rgb(&result);
95   RGBImageSubpixel result_sp(&result);
96   referential.draw(&result_sp, level);
97   //   pf->draw(&result_sp, 255, 255, 0, &referential);
98   result_sp.write_png(filename);
99 }
100
101 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
102                                                    Detector *detector) {
103   LabelledImage *image;
104   char buffer[buffer_size];
105
106   PoseCell target_cell;
107   Pose p;
108   PiFeature *pf;
109
110   PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
111
112   for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
113     image = pool->grab_image(i);
114     RGBImage result(image->width(), image->height());
115     image->to_rgb(&result);
116     RGBImageSubpixel result_sp(&result);
117
118     if(global.pictures_for_article) {
119       for(int t = 0; t < image->nb_targets(); t++) {
120         image->get_target_pose(t)->draw(8, 255, 255, 255,
121                                         hierarchy->nb_levels() - 1, &result_sp);
122
123       }
124       for(int t = 0; t < image->nb_targets(); t++) {
125         image->get_target_pose(t)->draw(4, 0, 0, 0,
126                                         hierarchy->nb_levels() - 1, &result_sp);
127       }
128     } else {
129       for(int t = 0; t < image->nb_targets(); t++) {
130         image->get_target_pose(t)->draw(4, 255, 128, 0,
131                                         hierarchy->nb_levels() - 1, &result_sp);
132       }
133     }
134
135     sprintf(buffer, "/tmp/truth-%05d.png", i);
136     cout << "Writing " << buffer << endl;
137     result_sp.write_png(buffer);
138     pool->release_image(i);
139   }
140
141   for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
142     image = pool->grab_image(i);
143
144     RGBImage result(image->width(), image->height());
145     image->to_rgb(&result);
146     RGBImageSubpixel result_sp(&result);
147
148     int u = 0;
149
150     // image->compute_rich_structure();
151
152     for(int t = 0; t < image->nb_targets(); t++) {
153
154       image->get_target_pose(t)->draw(4, 255, 0, 0,
155                                       hierarchy->nb_levels() - 1, &result_sp);
156
157       hierarchy->get_containing_cell(image,
158                                      hierarchy->nb_levels() - 1,
159                                      image->get_target_pose(t), &target_cell);
160
161       target_cell.get_centroid(&p);
162
163       p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
164
165       PiReferential referential(&target_cell);
166
167       sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, u);
168       image->compute_rich_structure();
169       write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
170
171       if(detector) {
172         int nb_features = 100;
173         for(int f = 0; f < nb_features; f++) 
174           if(f == 0 || f ==50 || f  == 53) {
175             int n_family, n_feature;
176             if(f < nb_features/2) {
177               n_family = 0;
178               n_feature = f;
179             } else {
180               n_family = detector->_nb_classifiers_per_level;
181               n_feature = f - nb_features/2;
182             }
183             pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
184             sprintf(buffer, "/tmp/pf-%05d-%02d-%03d.png", i, u, f);
185             write_referential_png(buffer,
186                                   hierarchy->nb_levels() - 1,
187                                   image,
188                                   &referential,
189                                   pf);
190           }
191       }
192       u++;
193     }
194
195     //         sprintf(buffer, "/tmp/image-%05d.png", i);
196     //         cout << "Writing " << buffer << endl;
197     //         result_sp.write_png(buffer);
198
199     //         if(global.write_tag_images) {
200     //           sprintf(buffer, "/tmp/image-%05d_tags.png", i);
201     //           cout << "Writing " << buffer << endl;
202     //           image->compute_rich_structure();
203     //           image->write_tag_png(buffer);
204     //         }
205
206     pool->release_image(i);
207   }
208
209   delete hierarchy;
210 }
211
212 void write_image_with_detections(const char *filename,
213                                  LabelledImage *image,
214                                  PoseCellSet *detections,
215                                  int level) {
216
217   RGBImage result(image->width(), image->height());
218
219   for(int y = 0; y < result.height(); y++) {
220     for(int x = 0; x < result.width(); x++) {
221       int c = image->value(x, y);
222       result.set_pixel(x, y, c, c, c);
223     }
224   }
225
226   RGBImageSubpixel result_sp(&result);
227
228   if(global.pictures_for_article) {
229     for(int a = 0; a < detections->nb_cells(); a++) {
230       Pose pose;
231       detections->get_cell(a)->get_centroid(&pose);
232       pose.draw(8, 255, 255, 255, level, &result_sp);
233     }
234     for(int a = 0; a < detections->nb_cells(); a++) {
235       Pose pose;
236       detections->get_cell(a)->get_centroid(&pose);
237       pose.draw(4,   0,   0,   0, level, &result_sp);
238     }
239   } else {
240     for(int a = 0; a < detections->nb_cells(); a++) {
241       Pose pose;
242       detections->get_cell(a)->get_centroid(&pose);
243       pose.draw(4, 255, 128, 0, level, &result_sp);
244     }
245   }
246
247   (*global.log_stream) << "Writing " << filename << endl;
248
249   result_sp.write_png(filename);
250 }