automatic commit
[folded-ctf.git] / rich_image.h
diff --git a/rich_image.h b/rich_image.h
new file mode 100644 (file)
index 0000000..c8f7a74
--- /dev/null
@@ -0,0 +1,105 @@
+
+///////////////////////////////////////////////////////////////////////////
+// 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        //
+///////////////////////////////////////////////////////////////////////////
+
+/*
+
+  This class implements the multi-scale basic edge features on the
+  images. The heavy machinery and ugly coding style is motivated by
+  performance.
+
+*/
+
+#ifndef RICH_IMAGE_H
+#define RICH_IMAGE_H
+
+#include "image.h"
+#include "rgb_image.h"
+#include "misc.h"
+#include "global.h"
+
+class RichImage : public Image {
+
+  static const int _image_size_min = 8;
+
+  int _nb_scales;
+
+  int *_width_at_scale, *_height_at_scale;
+
+  unsigned int *_edge_map;
+
+  unsigned int **_line_edge_map;
+  unsigned int ***_tag_edge_map;
+  unsigned int ****_scale_edge_map;
+
+  void free();
+
+  void compute_one_scale_edge_maps(int width, int height,
+                                   unsigned int ***scale_edge_map,
+                                   unsigned char *pixel_map,
+                                   unsigned int *sum_pixel_map,
+                                   unsigned int *sum_sq_pixel_map);
+
+public:
+
+  // We have 8 edge orientations
+  static const int nb_edge_tags = 8;
+  static const int first_edge_tag = 0;
+
+  // The variance tag is 1 if the variance of the gray levels in the
+  // 16x16 square is greater than 10
+  static const int variance_tag = 8;
+
+  // We quantify the grayscales into 8 bins
+  static const int nb_gray_tags = 8;
+  static const int first_gray_tag = 9;
+
+  static const int _nb_tags = nb_edge_tags + 1 + nb_gray_tags;
+
+  inline int nb_tags() { return _nb_tags; }
+  inline int nb_scales() { return _nb_scales; }
+
+  inline int nb_tags_in_window(int scale, int tag, int xmin, int ymin, int xmax, int ymax) {
+    if(scale < 0 || scale >= _nb_scales) return 0;
+    if(xmin < 0) xmin = 0;
+    if(xmax >= _width_at_scale[scale]) xmax = _width_at_scale[scale] - 1;
+    if(ymin < 0) ymin = 0;
+    if(ymax >= _height_at_scale[scale]) ymax = _height_at_scale[scale] - 1;
+    if(xmin < xmax && ymin < ymax) {
+      unsigned int **tmp = _scale_edge_map[scale][tag];
+      return tmp[ymax][xmax] + tmp[ymin][xmin] - tmp[ymax][xmin] - tmp[ymin][xmax];
+    } else
+      return 0;
+  }
+
+  RichImage();
+  RichImage(int width, int height);
+  virtual ~RichImage();
+
+  virtual void compute_rich_structure();
+  virtual void crop(int xmin, int ymin, int width, int height);
+
+  virtual void write(ostream *out);
+
+  // read does _NOT_ build the rich structure. One has to call
+  // compute_rich_structure() for that!
+  virtual void read(istream *in);
+
+  virtual void write_tag_png(const char *filename);
+};
+
+#endif