-
Notifications
You must be signed in to change notification settings - Fork 2
/
features.cc
249 lines (219 loc) · 7.22 KB
/
features.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// AUTORIGHTS
// -------------------------------------------------------
// Copyright (C) 2011-2012 Ross Girshick
// Copyright (C) 2008, 2009, 2010 Pedro Felzenszwalb, Ross Girshick
// Copyright (C) 2007 Pedro Felzenszwalb, Deva Ramanan
//
// This file is part of the voc-releaseX code
// (http://people.cs.uchicago.edu/~rbg/latent/)
// and is available under the terms of an MIT-like license
// provided in COPYING. Please retain this notice and
// COPYING if you use this file (or a portion of it) in
// your project.
// -------------------------------------------------------
#include <math.h>
#include "mex.h"
// small value, used to avoid division by zero
#define eps 0.0001
// unit vectors used to compute gradient orientation
double uu[9] = {1.0000,
0.9397,
0.7660,
0.500,
0.1736,
-0.1736,
-0.5000,
-0.7660,
-0.9397};
double vv[9] = {0.0000,
0.3420,
0.6428,
0.8660,
0.9848,
0.9848,
0.8660,
0.6428,
0.3420};
static inline float min(float x, float y) { return (x <= y ? x : y); }
static inline float max(float x, float y) { return (x <= y ? y : x); }
static inline int min(int x, int y) { return (x <= y ? x : y); }
static inline int max(int x, int y) { return (x <= y ? y : x); }
// main function:
// takes a double color image and a bin size
// returns HOG features
mxArray *process(const mxArray *mximage, const mxArray *mxsbin) {
double *im = (double *)mxGetPr(mximage);
const int *dims = mxGetDimensions(mximage);
if (mxGetNumberOfDimensions(mximage) != 3 ||
dims[2] != 3 ||
mxGetClassID(mximage) != mxDOUBLE_CLASS)
mexErrMsgTxt("Invalid input");
int sbin = (int)mxGetScalar(mxsbin);
// memory for caching orientation histograms & their norms
int cells[2];
cells[0] = (int)round((double)dims[0]/(double)sbin);
cells[1] = (int)round((double)dims[1]/(double)sbin);
float *hist = (float *)mxCalloc(cells[0]*cells[1]*18, sizeof(float));
float *norm = (float *)mxCalloc(cells[0]*cells[1], sizeof(float));
// memory for HOG features
int out[3];
out[0] = max(cells[0]-2, 0);
out[1] = max(cells[1]-2, 0);
out[2] = 27+4+1;
mxArray *mxfeat = mxCreateNumericArray(3, out, mxSINGLE_CLASS, mxREAL);
float *feat = (float *)mxGetPr(mxfeat);
int visible[2];
visible[0] = cells[0]*sbin;
visible[1] = cells[1]*sbin;
for (int x = 1; x < visible[1]-1; x++) {
for (int y = 1; y < visible[0]-1; y++) {
// first color channel
double *s = im + min(x, dims[1]-2)*dims[0] + min(y, dims[0]-2);
double dy = *(s+1) - *(s-1);
double dx = *(s+dims[0]) - *(s-dims[0]);
double v = dx*dx + dy*dy;
// second color channel
s += dims[0]*dims[1];
double dy2 = *(s+1) - *(s-1);
double dx2 = *(s+dims[0]) - *(s-dims[0]);
double v2 = dx2*dx2 + dy2*dy2;
// third color channel
s += dims[0]*dims[1];
double dy3 = *(s+1) - *(s-1);
double dx3 = *(s+dims[0]) - *(s-dims[0]);
double v3 = dx3*dx3 + dy3*dy3;
// pick channel with strongest gradient
if (v2 > v) {
v = v2;
dx = dx2;
dy = dy2;
}
if (v3 > v) {
v = v3;
dx = dx3;
dy = dy3;
}
// snap to one of 18 orientations
double best_dot = 0;
int best_o = 0;
for (int o = 0; o < 9; o++) {
double dot = uu[o]*dx + vv[o]*dy;
if (dot > best_dot) {
best_dot = dot;
best_o = o;
} else if (-dot > best_dot) {
best_dot = -dot;
best_o = o+9;
}
}
// add to 4 histograms around pixel using bilinear interpolation
double xp = ((double)x+0.5)/(double)sbin - 0.5;
double yp = ((double)y+0.5)/(double)sbin - 0.5;
int ixp = (int)floor(xp);
int iyp = (int)floor(yp);
double vx0 = xp-ixp;
double vy0 = yp-iyp;
double vx1 = 1.0-vx0;
double vy1 = 1.0-vy0;
v = sqrt(v);
if (ixp >= 0 && iyp >= 0) {
*(hist + ixp*cells[0] + iyp + best_o*cells[0]*cells[1]) +=
vx1*vy1*v;
}
if (ixp+1 < cells[1] && iyp >= 0) {
*(hist + (ixp+1)*cells[0] + iyp + best_o*cells[0]*cells[1]) +=
vx0*vy1*v;
}
if (ixp >= 0 && iyp+1 < cells[0]) {
*(hist + ixp*cells[0] + (iyp+1) + best_o*cells[0]*cells[1]) +=
vx1*vy0*v;
}
if (ixp+1 < cells[1] && iyp+1 < cells[0]) {
*(hist + (ixp+1)*cells[0] + (iyp+1) + best_o*cells[0]*cells[1]) +=
vx0*vy0*v;
}
}
}
// compute energy in each block by summing over orientations
for (int o = 0; o < 9; o++) {
float *src1 = hist + o*cells[0]*cells[1];
float *src2 = hist + (o+9)*cells[0]*cells[1];
float *dst = norm;
float *end = norm + cells[1]*cells[0];
while (dst < end) {
*(dst++) += (*src1 + *src2) * (*src1 + *src2);
src1++;
src2++;
}
}
// compute features
for (int x = 0; x < out[1]; x++) {
for (int y = 0; y < out[0]; y++) {
float *dst = feat + x*out[0] + y;
float *src, *p, n1, n2, n3, n4;
p = norm + (x+1)*cells[0] + y+1;
n1 = 1.0 / sqrt(*p + *(p+1) + *(p+cells[0]) + *(p+cells[0]+1) + eps);
p = norm + (x+1)*cells[0] + y;
n2 = 1.0 / sqrt(*p + *(p+1) + *(p+cells[0]) + *(p+cells[0]+1) + eps);
p = norm + x*cells[0] + y+1;
n3 = 1.0 / sqrt(*p + *(p+1) + *(p+cells[0]) + *(p+cells[0]+1) + eps);
p = norm + x*cells[0] + y;
n4 = 1.0 / sqrt(*p + *(p+1) + *(p+cells[0]) + *(p+cells[0]+1) + eps);
float t1 = 0;
float t2 = 0;
float t3 = 0;
float t4 = 0;
// contrast-sensitive features
src = hist + (x+1)*cells[0] + (y+1);
for (int o = 0; o < 18; o++) {
float h1 = min(*src * n1, 0.2);
float h2 = min(*src * n2, 0.2);
float h3 = min(*src * n3, 0.2);
float h4 = min(*src * n4, 0.2);
*dst = 0.5 * (h1 + h2 + h3 + h4);
t1 += h1;
t2 += h2;
t3 += h3;
t4 += h4;
dst += out[0]*out[1];
src += cells[0]*cells[1];
}
// contrast-insensitive features
src = hist + (x+1)*cells[0] + (y+1);
for (int o = 0; o < 9; o++) {
float sum = *src + *(src + 9*cells[0]*cells[1]);
float h1 = min(sum * n1, 0.2);
float h2 = min(sum * n2, 0.2);
float h3 = min(sum * n3, 0.2);
float h4 = min(sum * n4, 0.2);
*dst = 0.5 * (h1 + h2 + h3 + h4);
dst += out[0]*out[1];
src += cells[0]*cells[1];
}
// texture features
*dst = 0.2357 * t1;
dst += out[0]*out[1];
*dst = 0.2357 * t2;
dst += out[0]*out[1];
*dst = 0.2357 * t3;
dst += out[0]*out[1];
*dst = 0.2357 * t4;
// truncation feature
dst += out[0]*out[1];
*dst = 0;
}
}
mxFree(hist);
mxFree(norm);
return mxfeat;
}
// matlab entry point
// F = features(image, bin)
// image should be color with double values
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 2)
mexErrMsgTxt("Wrong number of inputs");
if (nlhs != 1)
mexErrMsgTxt("Wrong number of outputs");
plhs[0] = process(prhs[0], prhs[1]);
}