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   cout << "Writing " << filename << endl;
79   result_sp.write_png(filename);
80 }
81
82 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
83                                                    Detector *detector) {
84   LabelledImage *image;
85   char buffer[buffer_size];
86
87   PoseCell target_cell;
88   Pose p;
89   PiFeature *pf;
90
91   PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
92
93   if(global.material_feature_nb < 0) {
94     for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
95       image = pool->grab_image(i);
96       RGBImage result(image->width(), image->height());
97       image->to_rgb(&result);
98       RGBImageSubpixel result_sp(&result);
99
100       if(global.pictures_for_article) {
101         for(int t = 0; t < image->nb_targets(); t++) {
102           image->get_target_pose(t)->draw(8, 255, 255, 255,
103                                           hierarchy->nb_levels() - 1, &result_sp);
104
105         }
106         for(int t = 0; t < image->nb_targets(); t++) {
107           image->get_target_pose(t)->draw(4, 0, 0, 0,
108                                           hierarchy->nb_levels() - 1, &result_sp);
109         }
110       } else {
111         for(int t = 0; t < image->nb_targets(); t++) {
112           image->get_target_pose(t)->draw(4, 255, 128, 0,
113                                           hierarchy->nb_levels() - 1, &result_sp);
114         }
115       }
116
117       sprintf(buffer, "/tmp/truth-%05d.png", i);
118       cout << "Writing " << buffer << endl;
119       result_sp.write_png(buffer);
120       pool->release_image(i);
121     }
122   }
123
124   for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
125     image = pool->grab_image(i);
126
127     RGBImage result(image->width(), image->height());
128     image->to_rgb(&result);
129     RGBImageSubpixel result_sp(&result);
130
131     // image->compute_rich_structure();
132
133     for(int t = 0; t < image->nb_targets(); t++) {
134
135       image->get_target_pose(t)->draw(4, 255, 0, 0,
136                                       hierarchy->nb_levels() - 1, &result_sp);
137
138       hierarchy->get_containing_cell(image,
139                                      hierarchy->nb_levels() - 1,
140                                      image->get_target_pose(t), &target_cell);
141
142       target_cell.get_centroid(&p);
143
144       p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
145
146       PiReferential referential(&target_cell);
147
148       image->compute_rich_structure();
149
150       if(global.material_feature_nb < 0) {
151         sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, t);
152         write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
153       } else if(detector) {
154         int n_family = 0;
155         int n_feature = global.material_feature_nb;
156         while(n_feature > detector->_pi_feature_families[n_family]->nb_features()) {
157           n_family++;
158           n_feature -= detector->_pi_feature_families[n_family]->nb_features();
159         }
160         pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
161         sprintf(buffer, "/tmp/pf-%05d-%02d-%05d.png", i, t, global.material_feature_nb);
162         write_referential_png(buffer,
163                               hierarchy->nb_levels() - 1,
164                               image,
165                               &referential,
166                               pf);
167       }
168     }
169
170     pool->release_image(i);
171   }
172
173   delete hierarchy;
174 }
175
176 void write_image_with_detections(const char *filename,
177                                  LabelledImage *image,
178                                  PoseCellSet *detections,
179                                  int level) {
180
181   RGBImage result(image->width(), image->height());
182
183   for(int y = 0; y < result.height(); y++) {
184     for(int x = 0; x < result.width(); x++) {
185       int c = image->value(x, y);
186       result.set_pixel(x, y, c, c, c);
187     }
188   }
189
190   RGBImageSubpixel result_sp(&result);
191
192   if(global.pictures_for_article) {
193     for(int a = 0; a < detections->nb_cells(); a++) {
194       Pose pose;
195       detections->get_cell(a)->get_centroid(&pose);
196       pose.draw(8, 255, 255, 255, level, &result_sp);
197     }
198     for(int a = 0; a < detections->nb_cells(); a++) {
199       Pose pose;
200       detections->get_cell(a)->get_centroid(&pose);
201       pose.draw(4,   0,   0,   0, level, &result_sp);
202     }
203   } else {
204     for(int a = 0; a < detections->nb_cells(); a++) {
205       Pose pose;
206       detections->get_cell(a)->get_centroid(&pose);
207       pose.draw(4, 255, 128, 0, level, &result_sp);
208     }
209   }
210
211   cout << "Writing " << filename << endl;
212   result_sp.write_png(filename);
213 }