21 #ifndef _HOG_PREPROCS_H
22 #define _HOG_PREPROCS_H
24 #include "Descriptor/RawData.h"
32 unsigned int width, height;
36 inline bool operator == (
const HoGParams & b)
const
45 unsigned int cell_width, cell_height;
46 unsigned int block_width, block_height;
47 unsigned int h_step, v_step;
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) { }
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; }
73 void Configure(std::istream & in) { }
75 void Configure(
const char* str)
81 static const unsigned int filter_clearance = 1;
83 unsigned int block_real_width = cell_width * block_width;
84 unsigned int block_real_height = cell_height * block_height;
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 <<
" | ";
91 unsigned int rows, cols;
94 cols = 1 + (width - 2*filter_clearance - block_real_width) / h_step;
95 rows = 1 + (height - 2*filter_clearance - block_real_height) / v_step;
97 std::cerr <<
"Blocks: " << cols <<
'x' << rows <<
" | ";
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 <<
")" <<
" | ";
102 int desc_size = rows * cols * n_cells_per_block * n_bins;
104 std::cerr <<
"Descriptor size: " << desc_size <<
" elements" << std::endl;
108 bool Process(
ReturnType & out,
const T *image,
unsigned int width,
unsigned int height,
long stride)
const
110 static const unsigned int filter_clearance = 1;
112 unsigned int block_real_width = cell_width * block_width;
113 unsigned int block_real_height = cell_height * block_height;
123 cols = 1 + (width - 2*filter_clearance - block_real_width) / h_step;
124 rows = 1 + (height - 2*filter_clearance - block_real_height) / v_step;
128 std::cerr <<
"invalid input image:" << width <<
'x' << height << std::endl;
143 double phase_factor = (sign) ? ((
double)n_bins / (2.0*M_PI) ) : ((double)n_bins / (M_PI) );
146 double *hog =
new double[desc_size];
147 for(
int l=0;l<desc_size;++l)
153 float *module =
new float [width * height];
154 int *bin =
new int [width * height];
156 for(
int j=filter_clearance;j<(int)(height-filter_clearance);++j)
158 int kin = j * stride;
159 int kout = j * width;
160 for(
int i=filter_clearance;i<(int)(width-filter_clearance);++i)
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];
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;
168 bin[kout + i] = ((int)(phase * phase_factor)) % n_bins;
182 for(
int j=0;j<rows;++j)
183 for(
int i=0;i<cols;++i)
188 for(
unsigned int j1 = 0;j1<block_height;++j1)
189 for(
unsigned int i1 = 0;i1<block_width;++i1)
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;
196 for(
unsigned int n=y0;n<y0+cell_height;++n)
197 for(
unsigned int m=x0;m<x0+cell_width;++m)
199 int k = m + n * width;
202 hog[h + bin[k]] += module[k];
217 out.first.data = hog;
218 out.second.width = width;
219 out.second.height = height;
220 out.second.desc_size = desc_size;
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
unsigned int desc_size
dimensione del descrittore
Definition: HoGPreprocessor.h:30