automatic commit
[folded-ctf.git] / rich_image.h
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 /*
22
23   A RichImage is an Image with all the necessary pre-computation to
24   compute the feature responses: Edge detection and local variance
25   thresholding at multiple scale, with integral images. The heavy
26   machinery and ugly coding style is motivated by performance.
27
28 */
29
30 #ifndef RICH_IMAGE_H
31 #define RICH_IMAGE_H
32
33 #include "image.h"
34 #include "rgb_image.h"
35 #include "misc.h"
36 #include "global.h"
37
38 class RichImage : public Image {
39
40   static const int _image_size_min = 8;
41
42   int _nb_scales;
43
44   int *_width_at_scale, *_height_at_scale;
45
46   unsigned int *_edge_map;
47
48   unsigned int **_line_edge_map;
49   unsigned int ***_tag_edge_map;
50   unsigned int ****_scale_edge_map;
51
52   void free();
53
54   void compute_one_scale_edge_maps(int width, int height,
55                                    unsigned int ***scale_edge_map,
56                                    unsigned char *pixel_map,
57                                    unsigned int *sum_pixel_map,
58                                    unsigned int *sum_sq_pixel_map);
59
60 public:
61
62   // We have 8 edge orientations
63   static const int nb_edge_tags = 8;
64   static const int first_edge_tag = 0;
65
66   // The variance tag is 1 if the variance of the gray levels in the
67   // 16x16 square is greater than 10
68   static const int variance_tag = 8;
69
70   // We quantify the grayscales into 8 bins
71   static const int nb_gray_tags = 8;
72   static const int first_gray_tag = 9;
73
74   static const int _nb_tags = nb_edge_tags + 1 + nb_gray_tags;
75
76   inline int nb_tags() { return _nb_tags; }
77   inline int nb_scales() { return _nb_scales; }
78
79   inline int nb_tags_in_window(int scale, int tag, int xmin, int ymin, int xmax, int ymax) {
80     if(scale < 0 || scale >= _nb_scales) return 0;
81     if(xmin < 0) xmin = 0;
82     if(xmax >= _width_at_scale[scale]) xmax = _width_at_scale[scale] - 1;
83     if(ymin < 0) ymin = 0;
84     if(ymax >= _height_at_scale[scale]) ymax = _height_at_scale[scale] - 1;
85     if(xmin < xmax && ymin < ymax) {
86       unsigned int **tmp = _scale_edge_map[scale][tag];
87       return tmp[ymax][xmax] + tmp[ymin][xmin] - tmp[ymax][xmin] - tmp[ymin][xmax];
88     } else
89       return 0;
90   }
91
92   RichImage();
93   RichImage(int width, int height);
94   virtual ~RichImage();
95
96   virtual void compute_rich_structure();
97
98   virtual void write(ostream *out);
99
100   // read does _NOT_ build the rich structure. One has to call
101   // compute_rich_structure() for that!
102   virtual void read(istream *in);
103
104   virtual void write_tag_png(const char *filename);
105 };
106
107 #endif