automatic commit
[folded-ctf.git] / materials.cc
diff --git a/materials.cc b/materials.cc
new file mode 100644 (file)
index 0000000..bbb6cbf
--- /dev/null
@@ -0,0 +1,250 @@
+
+///////////////////////////////////////////////////////////////////////////
+// This program is free software: you can redistribute it and/or modify  //
+// it under the terms of the version 3 of the GNU General Public License //
+// as published by the Free Software Foundation.                         //
+//                                                                       //
+// This program is distributed in the hope that it will be useful, but   //
+// WITHOUT ANY WARRANTY; without even the implied warranty of            //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
+// General Public License for more details.                              //
+//                                                                       //
+// You should have received a copy of the GNU General Public License     //
+// along with this program. If not, see <http://www.gnu.org/licenses/>.  //
+//                                                                       //
+// Written by Francois Fleuret, (C) IDIAP                                //
+// Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
+///////////////////////////////////////////////////////////////////////////
+
+#include "materials.h"
+#include "boosted_classifier.h"
+#include "parsing_pool.h"
+#include "rgb_image_subpixel.h"
+
+void write_referential_png(char *filename,
+                           int level,
+                           RichImage *image,
+                           PiReferential *referential,
+                           PiFeature *pf) {
+
+  scalar_t s = global.discrete_log_scale_to_scale(referential->common_scale());
+
+  RGBImage result(int(image->width() * s), int(image->height() * s));
+
+  for(int y = 0; y < result.height(); y++) {
+    for(int x = 0; x < result.width(); x++) {
+      int c = 0;
+
+      // GRAYSCALES
+
+      for(int b = 0; b < RichImage::nb_gray_tags; b++) {
+        c += (b * 256)/RichImage::nb_gray_tags *
+          image->nb_tags_in_window(referential->common_scale(),
+                                   RichImage::first_gray_tag + b,
+                                   x, y,
+                                   x + 1, y + 1);
+      }
+
+      // EDGES
+
+      //       for(int b = 0; b < RichImage::nb_edge_tags; b++) {
+      //         c += image->nb_tags_in_window(referential->common_scale(),
+      //                                       RichImage::first_edge_tag + b,
+      //                                       x, y,
+      //                                       x + 1, y + 1);
+      //       }
+      //       c = 255 - (c * 255)/RichImage::nb_edge_tags;
+
+      // THRESHOLDED VARIANCE
+
+      //       c = image->nb_tags_in_window(referential->common_scale(),
+      //                                    RichImage::variance_tag,
+      //                                    x, y,
+      //                                    x + 1, y + 1);
+      //       c = (1 - c) * 255;
+
+      result.set_pixel(x, y, c, c, c);
+    }
+  }
+
+  RGBImageSubpixel result_sp(&result);
+
+  if(pf) {
+    pf->draw(&result_sp, 255, 255, 0, referential);
+  } else {
+    referential->draw(&result_sp, level);
+  }
+
+  (*global.log_stream) << "Writing " << filename << endl;
+  result_sp.write_png(filename);
+}
+
+void write_one_pi_feature_png(char *filename,
+                              LabelledImage *image,
+                              PoseCellHierarchy *hierarchy,
+                              int nb_target,
+                              int level,
+                              PiFeature *pf) {
+
+  PoseCell target_cell;
+  hierarchy->get_containing_cell(image, level,
+                                 image->get_target_pose(nb_target), &target_cell);
+  PiReferential referential(&target_cell);
+  RGBImage result(image->width(), image->height());
+  image->to_rgb(&result);
+  RGBImageSubpixel result_sp(&result);
+  referential.draw(&result_sp, level);
+  //   pf->draw(&result_sp, 255, 255, 0, &referential);
+  result_sp.write_png(filename);
+}
+
+void write_pool_images_with_poses_and_referentials(LabelledImagePool *pool,
+                                                   Detector *detector) {
+  LabelledImage *image;
+  char buffer[buffer_size];
+
+  PoseCell target_cell;
+  Pose p;
+  PiFeature *pf;
+
+  PoseCellHierarchy *hierarchy = new PoseCellHierarchy(pool);
+
+  for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
+    image = pool->grab_image(i);
+    RGBImage result(image->width(), image->height());
+    image->to_rgb(&result);
+    RGBImageSubpixel result_sp(&result);
+
+    if(global.pictures_for_article) {
+      for(int t = 0; t < image->nb_targets(); t++) {
+        image->get_target_pose(t)->draw(8, 255, 255, 255,
+                                        hierarchy->nb_levels() - 1, &result_sp);
+
+      }
+      for(int t = 0; t < image->nb_targets(); t++) {
+        image->get_target_pose(t)->draw(4, 0, 0, 0,
+                                        hierarchy->nb_levels() - 1, &result_sp);
+      }
+    } else {
+      for(int t = 0; t < image->nb_targets(); t++) {
+        image->get_target_pose(t)->draw(4, 255, 128, 0,
+                                        hierarchy->nb_levels() - 1, &result_sp);
+      }
+    }
+
+    sprintf(buffer, "/tmp/truth-%05d.png", i);
+    cout << "Writing " << buffer << endl;
+    result_sp.write_png(buffer);
+    pool->release_image(i);
+  }
+
+  for(int i = 0; i < min(global.nb_images, pool->nb_images()); i++) {
+        image = pool->grab_image(i);
+
+        RGBImage result(image->width(), image->height());
+        image->to_rgb(&result);
+        RGBImageSubpixel result_sp(&result);
+
+        int u = 0;
+
+        // image->compute_rich_structure();
+
+        for(int t = 0; t < image->nb_targets(); t++) {
+
+          image->get_target_pose(t)->draw(4, 255, 0, 0,
+                                          hierarchy->nb_levels() - 1, &result_sp);
+
+          hierarchy->get_containing_cell(image,
+                                         hierarchy->nb_levels() - 1,
+                                         image->get_target_pose(t), &target_cell);
+
+          target_cell.get_centroid(&p);
+
+          p.draw(4, 0, 255, 0, hierarchy->nb_levels() - 1, &result_sp);
+
+          PiReferential referential(&target_cell);
+
+          sprintf(buffer, "/tmp/referential-%05d-%02d.png", i, u);
+          image->compute_rich_structure();
+          write_referential_png(buffer, hierarchy->nb_levels() - 1, image, &referential, 0);
+
+          if(detector) {
+            int nb_features = 100;
+            for(int f = 0; f < nb_features; f++) 
+              if(f == 0 || f ==50 || f  == 53) {
+              int n_family, n_feature;
+              if(f < nb_features/2) {
+                n_family = 0;
+                n_feature = f;
+              } else {
+                n_family = detector->_nb_classifiers_per_level;
+                n_feature = f - nb_features/2;
+              }
+              pf = detector->_pi_feature_families[n_family]->get_feature(n_feature);
+              sprintf(buffer, "/tmp/pf-%05d-%02d-%03d.png", i, u, f);
+              write_referential_png(buffer,
+                                    hierarchy->nb_levels() - 1,
+                                    image,
+                                    &referential,
+                                    pf);
+            }
+          }
+          u++;
+        }
+
+        //         sprintf(buffer, "/tmp/image-%05d.png", i);
+        //         cout << "Writing " << buffer << endl;
+        //         result_sp.write_png(buffer);
+
+        //         if(global.write_tag_images) {
+        //           sprintf(buffer, "/tmp/image-%05d_tags.png", i);
+        //           cout << "Writing " << buffer << endl;
+        //           image->compute_rich_structure();
+        //           image->write_tag_png(buffer);
+        //         }
+
+        pool->release_image(i);
+      }
+
+  delete hierarchy;
+}
+
+void write_image_with_detections(const char *filename,
+                                 LabelledImage *image,
+                                 PoseCellSet *detections,
+                                 int level) {
+
+  RGBImage result(image->width(), image->height());
+
+  for(int y = 0; y < result.height(); y++) {
+    for(int x = 0; x < result.width(); x++) {
+      int c = image->value(x, y);
+      result.set_pixel(x, y, c, c, c);
+    }
+  }
+
+  RGBImageSubpixel result_sp(&result);
+
+  if(global.pictures_for_article) {
+    for(int a = 0; a < detections->nb_cells(); a++) {
+      Pose pose;
+      detections->get_cell(a)->get_centroid(&pose);
+      pose.draw(8, 255, 255, 255, level, &result_sp);
+    }
+    for(int a = 0; a < detections->nb_cells(); a++) {
+      Pose pose;
+      detections->get_cell(a)->get_centroid(&pose);
+      pose.draw(4,   0,   0,   0, level, &result_sp);
+    }
+  } else {
+    for(int a = 0; a < detections->nb_cells(); a++) {
+      Pose pose;
+      detections->get_cell(a)->get_centroid(&pose);
+      pose.draw(4, 255, 128, 0, level, &result_sp);
+    }
+  }
+
+  (*global.log_stream) << "Writing " << filename << endl;
+
+  result_sp.write_png(filename);
+}