X-Boost  2.3.8
HoGPreprocessor.h
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _HOG_PREPROCS_H
22 #define _HOG_PREPROCS_H
23 
24 #include "Descriptor/RawData.h"
25 #include <cmath> // M_PI
26 #include <iostream> // debug
27 
28 struct HoGParams {
30  unsigned int desc_size;
31 
32  unsigned int width, height;
33 
34 public:
35 
36  inline bool operator == (const HoGParams & b) const
37  {
38  return desc_size == b.desc_size;
39  }
40 };
41 
45  unsigned int cell_width, cell_height;
46  unsigned int block_width, block_height;
47  unsigned int h_step, v_step;
48  unsigned int n_bins;
49  bool sign;
50 public:
52  unsigned int n_cells_per_block;
53 
54 public:
55 
56  typedef RawData<double> DataType;
57 
60 
62  typedef std::pair<DataType, ParamType> ReturnType;
63 
64 public:
65  HoGPreprocessor() : cell_width(8), cell_height(8), block_width(3), block_height(3), h_step(8), v_step(8), n_bins(8), sign(false) { }
66 
67  void SetCellGeometry(unsigned int width, unsigned int height) { cell_width = width; cell_height = height; }
68  void SetBlockGeometry(unsigned int width, unsigned int height) { block_width = width; block_height = height; n_cells_per_block = block_width * block_height; }
69  void SetHystogramBins(int n) { n_bins = n; }
70  void SetBlockStep(int h, int v) { h_step = h; v_step = v; }
71  void EnableSign(bool s) { sign = s; }
72 
73  void Configure(std::istream & in) { }
74 
75  void Configure(const char* str)
76  {
77  }
78 
79  void debug_geometry(unsigned int width, unsigned int height) const
80  {
81  static const unsigned int filter_clearance = 1; // derivative filter
82 
83  unsigned int block_real_width = cell_width * block_width;
84  unsigned int block_real_height = cell_height * block_height;
85 
86  std::cerr << "Cell Geometry: " << cell_width << 'x' << cell_height << " | ";
87  std::cerr << "Block Geometry: " << block_real_width << 'x' << block_real_height << " | ";
88  std::cerr << "Step: " << h_step << 'x' << v_step << " | ";
89 
91  unsigned int rows, cols;
92 
93  // numero di blocchi per colonna
94  cols = 1 + (width - 2*filter_clearance - block_real_width) / h_step;
95  rows = 1 + (height - 2*filter_clearance - block_real_height) / v_step;
96 
97  std::cerr << "Blocks: " << cols << 'x' << rows << " | ";
98  std::cerr << "Cells per Block: " << n_cells_per_block << " | ";
99  std::cerr << "area: (" << filter_clearance << "," << filter_clearance<<") - ("
100  << filter_clearance + (cols - 1) * h_step + block_real_width << ',' << filter_clearance + (rows - 1) * v_step + block_real_height << ")" << " | ";
101 
102  int desc_size = rows * cols * n_cells_per_block * n_bins;
103 
104  std::cerr << "Descriptor size: " << desc_size << " elements" << std::endl;
105  }
106 
107 template<class T>
108 bool Process(ReturnType & out, const T *image, unsigned int width, unsigned int height, long stride) const
109 {
110  static const unsigned int filter_clearance = 1; // derivative filter
111 
112  unsigned int block_real_width = cell_width * block_width;
113  unsigned int block_real_height = cell_height * block_height;
114 
115 // std::cerr << "Cell Geometry: " << cell_width << 'x' << cell_height << " | ";
116 // std::cerr << "Block Geometry: " << block_real_width << 'x' << block_real_height << " | ";
117 // std::cerr << "Step: " << h_step << 'x' << v_step << " | ";
118 
120  int rows, cols;
121 
122  // numero di blocchi per colonna
123  cols = 1 + (width - 2*filter_clearance - block_real_width) / h_step;
124  rows = 1 + (height - 2*filter_clearance - block_real_height) / v_step;
125 
126  if(rows<1 || cols<1)
127  {
128  std::cerr <<"invalid input image:" << width << 'x' << height << std::endl;
129  return false;
130  }
131 
132 // std::cerr << "Blocks: " << cols << 'x' << rows << " | ";
133 // std::cerr << "Cells per Block: " << n_cells_per_block << " | ";
134 // std::cerr << "area: (" << filter_clearance << "," << filter_clearance<<") - ("
135 // << filter_clearance + (cols - 1) * h_step + block_real_width << ',' << filter_clearance + (rows - 1) * v_step + block_real_height << ")" << " | ";
136 
137  int desc_size = rows * cols * n_cells_per_block * n_bins;
138 
139 // std::cerr << "Descriptor size: " << desc_size << " elements" << std::endl;
140 
141  // precalcola il fattore di conversione da angolo a BIN
142  // TODO: generare LUT
143  double phase_factor = (sign) ? ((double)n_bins / (2.0*M_PI) ) : ((double)n_bins / (M_PI) );
144 
145  // riserva il descrittore per HOG
146  double *hog = new double[desc_size];
147  for(int l=0;l<desc_size;++l)
148  hog[l] = 0.0;
149 
150  // compute derivate
151 
152  // calcolo modulo e fase
153  float *module = new float [width * height];
154  int *bin = new int [width * height];
155 
156  for(int j=filter_clearance;j<(int)(height-filter_clearance);++j)
157  {
158  int kin = j * stride;
159  int kout = j * width;
160  for(int i=filter_clearance;i<(int)(width-filter_clearance);++i)
161  {
162  int dx = (int) image[kin+i+1] - (int) image[kin+i-1];
163  int dy = (int) image[kin+i+stride] - (int) image[kin+i-stride];
164 
165  module[kout + i] = std::sqrt(dx*dx+dy*dy);
166  if (dx!=0 || dy!=0) {
167  double phase = std::atan2(dy, dx) + M_PI; // [0, +2pi]
168  bin[kout + i] = ((int)(phase * phase_factor)) % n_bins;
169  }
170  else
171  {
172  bin[kout + i]=0;
173  }
174  }
175  }
176 
177  // TODO: compute integral image to boost the HOG computation
178 
179 
180  // compute Hogs
181 
182  for(int j=0;j<rows;++j)
183  for(int i=0;i<cols;++i)
184  {
185  const int h0 = (i + j * cols) * n_cells_per_block*n_bins;
186  double factor = 0.0;
187 
188  for(unsigned int j1 = 0;j1<block_height;++j1)
189  for(unsigned int i1 = 0;i1<block_width;++i1)
190  {
191  int x0 = filter_clearance + i * h_step + i1 * cell_width;
192  int y0 = filter_clearance + j * v_step + j1 * cell_height;
193  int h = h0 + (i1 + j1 * block_width) * n_bins;
194 
195 // std::cout << '\n' << h << " = ";
196  for(unsigned int n=y0;n<y0+cell_height;++n)
197  for(unsigned int m=x0;m<x0+cell_width;++m)
198  {
199  int k = m + n * width;
200 // std::cout << k << ':' << bin[k] << ' ';
201 // std::cout.flush();
202  hog[h + bin[k]] += module[k];
203  factor += module[k];
204  }
205  }
206 
207 
208  // normalize per block
209  for(unsigned int k=h0;k<h0+n_bins*n_cells_per_block;++k)
210  hog[k] /= factor;
211 
212  }
213 
214  delete [] bin;
215  delete [] module;
216 
217  out.first.data = hog;
218  out.second.width = width;
219  out.second.height = height;
220  out.second.desc_size = desc_size;
221 
222  return true;
223 }
224 
225 };
226 
227 #endif
std::pair< DataType, ParamType > ReturnType
Data provided by this preprocessor.
Definition: HoGPreprocessor.h:62
unsigned int n_cells_per_block
numero di celle per blocco
Definition: HoGPreprocessor.h:52
bool Process(ReturnType &out, const T *image, unsigned int width, unsigned int height, long stride) const
Definition: HoGPreprocessor.h:108
HoGParams ParamType
Common data provided by this preprocessor.
Definition: HoGPreprocessor.h:59
Definition: HoGPreprocessor.h:28
void debug_geometry(unsigned int width, unsigned int height) const
Definition: HoGPreprocessor.h:79
Policy to convert an Image in a HoG descriptor Input: BW Image | Output: RawData n scalar...
Definition: HoGPreprocessor.h:44
Definition: RawData.h:35
unsigned int desc_size
dimensione del descrittore
Definition: HoGPreprocessor.h:30