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