Removed the definition of basename, which confuses an existing system one.
[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 version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "materials.h"
26 #include "boosted_classifier.h"
27 #include "parsing_pool.h"
28 #include "rgb_image_subpixel.h"
29
30 void write_referential_png(char *filename,
31                            int level,
32                            RichImage *image,
33                            PiReferential *referential,
34                            PiFeature *pf) {
35
36   scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
37
38   RGBImage result(int(image->width() * s), int(image->height() * s));
39
40   for(int y = 0; y < result.height(); y++) {
41     for(int x = 0; x < result.width(); x++) {
42       int c = 0;
43
44       // GRAYSCALES
45
46       for(int b = 0; b < RichImage::nb_gray_tags; b++) {
47         c += (b * 256)/RichImage::nb_gray_tags *
48           image->nb_tags_in_window(referential->common_scale(),
49                                    RichImage::first_gray_tag + b,
50                                    x, y,
51                                    x + 1, y + 1);
52       }
53
54       // EDGES
55
56       //       for(int b = 0; b < RichImage::nb_edge_tags; b++) {
57       //         c += image->nb_tags_in_window(referential->common_scale(),
58       //                                       RichImage::first_edge_tag + b,
59       //                                       x, y,
60       //                                       x + 1, y + 1);
61       //       }
62       //       c = 255 - (c * 255)/RichImage::nb_edge_tags;
63
64       // THRESHOLDED VARIANCE
65
66       //       c = image->nb_tags_in_window(referential->common_scale(),
67       //                                    RichImage::variance_tag,
68       //                                    x, y,
69       //                                    x + 1, y + 1);
70       //       c = (1 - c) * 255;
71
72       result.set_pixel(x, y, c, c, c);
73     }
74   }
75
76   RGBImageSubpixel result_sp(&result);
77
78   if(pf) {
79     pf->draw(&result_sp, 255, 255, 0, referential);
80   } else {
81     referential->draw(&result_sp, level);
82   }
83
84   cout << "Writing " << filename << endl;
85   result_sp.write_png(filename);
86 }
87
88 void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
89                                                    Detector *detector) {
90   LabelledImage *image;
91   char buffer[buffer_size];
92
93   PoseCell target_cell;
94   Pose p;
95   PiFeature *pf;
96
97   PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
98
99   if(global.material_feature_nb < 0) {
100     for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
101       image = pool->grab_image(i);
102       RGBImage result(image->width(), image->height());
103       image->to_rgb(&result);
104       RGBImageSubpixel result_sp(&result);
105
106       if(global.pictures_for_article) {
107         for(int t = 0; t < image->nb_targets(); t++) {
108           image->get_target_pose(t)->draw(8, 255, 255, 255,
109                                           hierarchy->nb_levels() - 1, &result_sp);
110
111         }
112         for(int t = 0; t < image->nb_targets(); t++) {
113           image->get_target_pose(t)->draw(4, 0, 0, 0,
114                                           hierarchy->nb_levels() - 1, &result_sp);
115         }
116       } else {
117         for(int t = 0; t < image->nb_targets(); t++) {
118           image->get_target_pose(t)->draw(4, 255, 128, 0,
119                                           hierarchy->nb_levels() - 1, &result_sp);
120         }
121       }
122
123       sprintf(buffer, "/tmp/truth-%05d.png", i);
124       cout << "Writing " << buffer << endl;
125       result_sp.write_png(buffer);
126       pool->release_image(i);
127     }
128   }
129
130   for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
131     image = pool->grab_image(i);
132
133     RGBImage result(image->width(), image->height());
134     image->to_rgb(&result);
135     RGBImageSubpixel result_sp(&result);
136
137     // image->compute_rich_structure();
138
139     for(int t = 0; t < image->nb_targets(); t++) {
140
141       image->get_target_pose(t)->draw(4, 255, 0, 0,
142                                       hierarchy->nb_levels() - 1, &result_sp);
143
144       hierarchy->get_containing_cell(image,
145                                      hierarchy->nb_levels() - 1,
146                                      image->get_target_pose(t), &target_cell);
147
148       target_cell.get_centroid(&p);
149
150       p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
151
152       PiReferential referential(&target_cell);
153
154       image->compute_rich_structure();
155
156       if(global.material_feature_nb < 0) {
157         sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, t);
158         write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
159       } else if(detector) {
160         int n_family = 0;
161         int n_feature = global.material_feature_nb;
162         while(n_feature > detector->_pi_feature_families[n_family]->nb_features()) {
163           n_family++;
164           n_feature -= detector->_pi_feature_families[n_family]->nb_features();
165         }
166         pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
167         sprintf(buffer, "/tmp/pf-%05d-%02d-%05d.png", i, t, global.material_feature_nb);
168         write_referential_png(buffer,
169                               hierarchy->nb_levels() - 1,
170                               image,
171                               &referential,
172                               pf);
173       }
174     }
175
176     pool->release_image(i);
177   }
178
179   delete hierarchy;
180 }
181
182 void write_image_with_detections(const char *filename,
183                                  LabelledImage *image,
184                                  PoseCellSet *detections,
185                                  int level) {
186
187   RGBImage result(image->width(), image->height());
188
189   for(int y = 0; y < result.height(); y++) {
190     for(int x = 0; x < result.width(); x++) {
191       int c = image->value(x, y);
192       result.set_pixel(x, y, c, c, c);
193     }
194   }
195
196   RGBImageSubpixel result_sp(&result);
197
198   if(global.pictures_for_article) {
199     for(int a = 0; a < detections->nb_cells(); a++) {
200       Pose pose;
201       detections->get_cell(a)->get_centroid(&pose);
202       pose.draw(8, 255, 255, 255, level, &result_sp);
203     }
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,   0,   0,   0, level, &result_sp);
208     }
209   } else {
210     for(int a = 0; a < detections->nb_cells(); a++) {
211       Pose pose;
212       detections->get_cell(a)->get_centroid(&pose);
213       pose.draw(4, 255, 128, 0, level, &result_sp);
214     }
215   }
216
217   cout << "Writing " << filename << endl;
218   result_sp.write_png(filename);
219 }