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