automatic commit
[folded-ctf.git] / rich_image.h
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 as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
16  *  folded-ctf is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 /*
27
28   A RichImage is an Image with all the necessary pre-computation to
29   compute the feature responses: Edge detection and local variance
30   thresholding at multiple scale, with integral images. The heavy
31   machinery and ugly coding style is motivated by performance.
32
33 */
34
35 #ifndef RICH_IMAGE_H
36 #define RICH_IMAGE_H
37
38 #include "image.h"
39 #include "rgb_image.h"
40 #include "misc.h"
41 #include "global.h"
42
43 class RichImage : public Image {
44
45   static const int _image_size_min = 8;
46
47   int _nb_scales;
48
49   int *_width_at_scale, *_height_at_scale;
50
51   unsigned int *_edge_map;
52
53   unsigned int **_line_edge_map;
54   unsigned int ***_tag_edge_map;
55   unsigned int ****_scale_edge_map;
56
57   void free();
58
59   void compute_one_scale_edge_maps(int width, int height,
60                                    unsigned int ***scale_edge_map,
61                                    unsigned char *pixel_map,
62                                    unsigned int *sum_pixel_map,
63                                    unsigned int *sum_sq_pixel_map);
64
65 public:
66
67   // We have 8 edge orientations
68   static const int nb_edge_tags = 8;
69   static const int first_edge_tag = 0;
70
71   // The variance tag is 1 if the variance of the gray levels in the
72   // 16x16 square is greater than 10
73   static const int variance_tag = 8;
74
75   // We quantify the grayscales into 8 bins
76   static const int nb_gray_tags = 8;
77   static const int first_gray_tag = 9;
78
79   static const int _nb_tags = nb_edge_tags + 1 + nb_gray_tags;
80
81   inline int nb_tags() { return _nb_tags; }
82   inline int nb_scales() { return _nb_scales; }
83
84   inline int nb_tags_in_window(int scale, int tag, int xmin, int ymin, int xmax, int ymax) {
85     if(scale < 0 || scale >= _nb_scales) return 0;
86     if(xmin < 0) xmin = 0;
87     if(xmax >= _width_at_scale[scale]) xmax = _width_at_scale[scale] - 1;
88     if(ymin < 0) ymin = 0;
89     if(ymax >= _height_at_scale[scale]) ymax = _height_at_scale[scale] - 1;
90     if(xmin < xmax && ymin < ymax) {
91       unsigned int **tmp = _scale_edge_map[scale][tag];
92       return tmp[ymax][xmax] + tmp[ymin][xmin] - tmp[ymax][xmin] - tmp[ymin][xmax];
93     } else
94       return 0;
95   }
96
97   RichImage();
98   RichImage(int width, int height);
99   virtual ~RichImage();
100
101   virtual void compute_rich_structure();
102
103   virtual void write(ostream *out);
104
105   // read does _NOT_ build the rich structure. One has to call
106   // compute_rich_structure() for that!
107   virtual void read(istream *in);
108
109   virtual void write_tag_png(const char *filename);
110 };
111
112 #endif