-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmexc_CollectSUM1Maps.cpp
439 lines (379 loc) · 13.8 KB
/
mexc_CollectSUM1Maps.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
Collect SUM1 maps for learning. Deal with a single image.
usage:
[alignedS1 sampleSize] = mexc_CollectSUM1Maps( S1, M1, initializeOrNot, posteriorMap, searchInterior, numCluster, windowSize );
S1 is the SUM1 map, and posteriorMap is the associated posterior cluster probabilities over scanned locations.
searchInterior contains the information for the scanned positions inside the M1 map.
Main steps: (this is done separately for each cluster)
1) Compute the sum of posterior probabilities (subject to thresholding);
2) Using the random seeds, find the locations by scanning over the posterior map again.
3) Collect both SUM1 and MAX1.
*/
# include <cstdlib>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "mex.h"
# include "math.h"
# define PI 3.1415926
# define ABS(x) ((x)>0? (x):(-(x)))
# define MAX(x, y) ((x)>(y)? (x):(y))
# define MIN(x, y) ((x)<(y)? (x):(y))
# define ROUND(x) (floor((x)+.5))
# define NEGMAX -1e10
/* Generating float vector */
float *float_vector(int n)
{
float *v;
v = (float*) mxCalloc (n, sizeof(float));
return v;
}
/* Generating integer vector */
int *int_vector(int n)
{
int *v;
v = (int*) mxCalloc (n, sizeof(int));
return v;
}
/* Generating float matrix */
float **float_matrix(int m, int n)
{
float **mat;
int i;
mat = (float**) mxCalloc(m, sizeof(float*));
for (i=0; i<m; i++)
mat[i] = float_vector(n);
return mat;
}
/* Generating integer matrix */
int **int_matrix(int m, int n)
{
int **mat;
int i;
mat = (int**) mxCalloc(m, sizeof(int*));
for (i=0; i<m; i++)
mat[i] = int_vector(n);
return mat;
}
/* Free matrix space */
void free_matrix(void **mat, int m, int n)
{
int i;
for (i=0; i<m; i++)
mxFree(mat[i]);
mxFree(mat);
}
/* Compute pixel index in the vector that stores image */
inline int px(int x, int y, int lengthx, int lengthy) /* the image is lengthx*lengthy */
{
return (x + (y-1)*lengthx - 1);
}
/* key input and output variables */
int numOrient, nGaborFilter, nGaborScale;
int numCluster;
const float **SUM1map, **MAX1map; /* MAX1 maps */
int halfFilterSize; /* filter size = 2*halfFilterSize + 1 */
int width, height; /* width and height of the input MAX1 map for the input image */
int verticalMargin, horizontalMargin, stepSize; /* parameters for the search interior */
int winWidth, winHeight; /* size of scanning window (template window size) */
int sampleSize; /* number of scanned locations */
float** posteriorMap; /* map of posterior probabilities at scanned positions, normalized per position */
int subsampledWidth, subsampledHeight; /* size of the posterior maps */
float* searchInterior;
const float backgroundPrior = 0.5;
float membershipThreshold;
// const float membershipThreshold = 1e-1; // examples with posterior probability smaller than this will not be considered
bool display = false;
const mxArray* p_allS1, *p_allM1, *p_weights;
const mxArray* p_patches;
float* srcIm;
int *currentIndPerCluster;
int *numExamplePerCluster;
float* currentMixingProb;
float** seedsPerCluster; /* require that seeds are ranked from low to high in each cluster */
/* Its numerical range is determined by the sum of posterior probability maps, which is computed before calling this function. */
void Compute()
{
// ========================================================================
// Compute the sum of posterior probabilities (subject to thresholding) in this image
// ========================================================================
/*
for( int c = 0; c < numCluster; ++c )
{
// the 2D size of the search interior:
subsampledWidth = (int)floor( (double)( width - horizontalMargin * 2 ) / stepSize );
subsampledHeight = (int)floor( (double)( height - verticalMargin * 2 ) / stepSize );
for( int iColPost = 0; iColPost < subsampledWidth; ++iColPost ) // position in the posterior map
{
int iColM1 = horizontalMargin + iColPost * stepSize;
if( iColM1 < 0 || iColM1 >= width )
{
mexErrMsgTxt( "iColM1 out of bound" );
}
for( int iRowPost = 0; iRowPost < subsampledHeight; ++iRowPost )
{
int iRowM1 = verticalMargin + iRowPost * stepSize;
if( iRowM1 < 0 || iRowM1 >= width )
{
mexErrMsgTxt( "iRowM1 out of bound" );
}
for( int cc = 0; cc < numCluster; ++cc )
{
float val = posteriorMap[cc][iRowPost+iColPost*subsampledHeight];
if( val >= membershipThreshold )
{
currentMixingProb[cc] += val;
}
}
}
} // up till now, mixingProb[] is not normalized
}
*/
// ========================================================
// Scan the posterior map and collect examples
// ========================================================
// allocate space for the output aligned S1, M1 maps for all clusters
mwSize dimsOutput[2];
for( int c = 0; c < numCluster; ++c )
{
if(display)
{
mexPrintf("cluster=%d, %d\n",c,p_allM1);
}
mxArray* p_M1maps = mxGetCell(p_allM1,c);
mxArray* p_S1maps = mxGetCell(p_allS1,c);
float* weights = (float*)mxGetPr( mxGetCell(p_weights,c) );
if( currentIndPerCluster[c] >= numExamplePerCluster[c] )
{
if( display )
mexPrintf("cluster %d is full with %d examples.",c,numExamplePerCluster[c]);
continue;
}
float currentSeed = seedsPerCluster[c][currentIndPerCluster[c]];
bool allSeedsAreFound = false;
/* scan over the posterior maps */
// the 2D size of the search interior:
subsampledWidth = (int)floor( (double)( width - horizontalMargin * 2 ) / stepSize );
subsampledHeight = (int)floor( (double)( height - verticalMargin * 2 ) / stepSize );
for( int iColPost = 0; iColPost < subsampledWidth && !allSeedsAreFound; ++iColPost ) // position in the posterior map
{
int iColM1 = horizontalMargin + iColPost * stepSize;
if( iColM1 < 0 || iColM1 >= width )
{
mexErrMsgTxt( "iColM1 out of bound" );
}
for( int iRowPost = 0; iRowPost < subsampledHeight && !allSeedsAreFound; ++iRowPost )
{
int iRowM1 = verticalMargin + iRowPost * stepSize;
if( iRowM1 < 0 || iRowM1 >= height )
{
mexErrMsgTxt( "iRowM1 out of bound" );
}
float thisWeight = posteriorMap[c][iRowPost+iColPost*subsampledHeight];
if( thisWeight >= membershipThreshold )
{
if( currentMixingProb[c] < currentSeed )
{
float previousMixing = currentMixingProb[c];
currentMixingProb[c] += thisWeight;
while( currentMixingProb[c] >= currentSeed && !allSeedsAreFound)
{
if(display)
mexPrintf("1\n");
/* found the bin ! */
/* collect this example */
weights[currentIndPerCluster[c]] = thisWeight;
if( thisWeight < 1e-3 )
{
mexPrintf("weight too small\n");
}
dimsOutput[0] = winHeight; dimsOutput[1] = winWidth;
mxArray* p_patch = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL );
mxSetCell( mxGetCell(p_patches,c), currentIndPerCluster[c], p_patch );
float* patch = (float*)mxGetPr(p_patch);
for( int ori3 = 0; ori3 < numOrient; ++ori3 ) // ori3, row3 and col3 is the index inside the window
{
mxArray* p_s1 = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL );
mxSetCell( p_S1maps, ori3*numExamplePerCluster[c] + currentIndPerCluster[c], p_s1 );
mxArray* p_m1 = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL );
mxSetCell( p_M1maps, ori3*numExamplePerCluster[c] + currentIndPerCluster[c], p_m1 );
float* s1 = (float*)mxGetPr(p_s1);
float* m1 = (float*)mxGetPr(p_m1);
for( int col3 = 0; col3 < winWidth; ++col3 )
{
int ind3 = iRowM1 + 0 - floor(winHeight/2.0) +
height * ( iColM1 + col3 - floor(winWidth/2.0) );
for( int row3 = 0; row3 < winHeight; ++row3 )
{
if( ind3 < 0 || ind3 >= width * height )
{
mexErrMsgTxt( "ind3 out of bound" );
}
int jj = row3+col3*winHeight;
s1[jj] = SUM1map[ori3][ind3];
m1[jj] = MAX1map[ori3][ind3];
if( ori3 == 0 )
{
patch[jj] = srcIm[ind3];
}
++ind3;
}
}
}
/* go on to the next seed */
float oldSeed = currentSeed;
currentIndPerCluster[c]++;
if( currentIndPerCluster[c] >= numExamplePerCluster[c] )
{
allSeedsAreFound = true;
break;
}
currentSeed = seedsPerCluster[c][currentIndPerCluster[c]];
if (display)
mexPrintf("newSeed: %.3f, oldSeed: %.3f, previousMixing: %.3f, thisWeight: %.3f\n",currentSeed,oldSeed,previousMixing,thisWeight);
if( currentSeed < oldSeed)
{
mexPrintf("The random seeds should be sorted from low to high!\n");
mexPrintf("previous: %.3f, current: %.3f, ind: %d, cluster: %d\n",oldSeed,currentSeed,currentIndPerCluster[c], c);
mexErrMsgTxt("Error !");
}
}
}
}
}
}
}
}
/* read in input variables and run the algorithm */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mxArray *f;
mxClassID datatype;
const mwSize* dims;
mwSize dimsOutput[2];
void* start_of_pr;
mxArray* pA;
int bytes_to_copy;
/*
* input variable 0: numOrient
*/
numOrient = (int)mxGetScalar(prhs[0]);
/*
* input variable 1: S1 maps
*/
const mxArray* pAS1Map = prhs[1];
dims = mxGetDimensions(pAS1Map);
nGaborFilter = dims[0] * dims[1];
nGaborScale = nGaborFilter / numOrient;
SUM1map = (const float**)mxCalloc( nGaborFilter, sizeof(*SUM1map) ); /* SUM1 maps */
for ( int i=0; i<nGaborFilter; ++i )
{
f = mxGetCell(pAS1Map, i);
datatype = mxGetClassID(f);
if ( datatype != mxSINGLE_CLASS )
mexErrMsgTxt("warning !! single precision required for MAX1map");
SUM1map[i] = (const float*)mxGetPr(f); /* get the pointer to cell content */
height = mxGetM(f); /* overwriting is ok, since it is constant over Gabor orientations and scales */
width = mxGetN(f);
}
/*
* input variable 2: M1 maps
*/
const mxArray* pAM1Map = prhs[2];
dims = mxGetDimensions(pAM1Map);
int nGaborFilter = dims[0] * dims[1];
int nGaborScale = nGaborFilter / numOrient;
MAX1map = (const float**)mxCalloc( nGaborFilter, sizeof(*MAX1map) ); /* MAX1 maps */
for ( int i=0; i<nGaborFilter; ++i )
{
f = mxGetCell(pAM1Map, i);
datatype = mxGetClassID(f);
if ( datatype != mxSINGLE_CLASS )
mexErrMsgTxt("warning !! single precision required for MAX1map");
MAX1map[i] = (const float*)mxGetPr(f); /* get the pointer to cell content */
}
/*
* input variable 3: posteriorMap: for multiple images (scales, rotations are marginalized out as latent variables)
*/
numCluster = mxGetM( prhs[3] ) * mxGetN( prhs[3] );
posteriorMap = (float**) mxCalloc( numCluster, sizeof(*posteriorMap) );
for( int j = 0; j < numCluster; ++j )
{
f = mxGetCell(prhs[3], j);
datatype = mxGetClassID( f );
if (datatype != mxSINGLE_CLASS)
mexErrMsgTxt("warning !! single precision required for posteriorMap.");
posteriorMap[j] = (float*)mxGetPr( f );
}
/*
* input variable 4: ARGMAX2 maps
*/
// currently not in use
/*
* input variable 5: searchInterior
*/
searchInterior = (float*) mxGetPr( prhs[5] );
datatype = mxGetClassID( prhs[5] );
if (datatype != mxSINGLE_CLASS)
mexErrMsgTxt("warning !! single precision required for search interior.");
/* parse searchInterior */
verticalMargin = (int)searchInterior[0];
horizontalMargin = (int)searchInterior[1];
stepSize = (int)searchInterior[2];
/*
* input variable 6: windowSize
*/
datatype = mxGetClassID( prhs[6] );
if (datatype != mxSINGLE_CLASS)
mexErrMsgTxt("warning !! single precision required for windowSize.");
float* windowSize = (float*) mxGetPr( prhs[6] );
winWidth = windowSize[1];
winHeight = windowSize[0];
if (display)
mexPrintf("numCluster=%d\n",numCluster);
/*
* input variable 7: random seeds
*/
seedsPerCluster = (float**)mxCalloc(numCluster,sizeof(*seedsPerCluster));
numExamplePerCluster = (int*)mxCalloc(numCluster,sizeof(*numExamplePerCluster));
for( int c = 0; c < numCluster; ++c )
{
f = mxGetCell( prhs[7], c );
numExamplePerCluster[c] = mxGetM(f) * mxGetN(f);
seedsPerCluster[c] = (float*)mxGetPr(f);
}
/*
* input variable 8: currentIndPerCluster[c] (also as output)
*/
currentIndPerCluster = (int*)mxGetPr(prhs[8]);
/*
* input variable 9: currentMixingProb (also as output)
*/
currentMixingProb = (float*)mxGetPr(prhs[9]);
/*
* input variable 10: data weights (also as output)
*/
p_weights = prhs[10];
/*
* input variable 11: collected S1 maps (also as output)
*/
p_allS1 = prhs[11];
/*
* input variable 12: collected M1 maps (also as output)
*/
p_allM1 = prhs[12];
/*
* input variable 13: input image (gray)
*/
srcIm = (float*)mxGetPr( prhs[13] );
/*
* input variable 14: collected image patches (gray, also as output)
*/
p_patches = prhs[14];
/*
* input variable 15: membershipThreshold
*/
membershipThreshold = (float)mxGetScalar(prhs[15]);
Compute();
}