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
|
/* file: siftlocalmax.c
** author: Andrea Vedaldi
** description: Find local maximizer of multi-dimensional array.
**/
/* AUTORIGHTS
Copyright (c) 2006 The Regents of the University of California.
All Rights Reserved.
Created by Andrea Vedaldi
UCLA Vision Lab - Department of Computer Science
Permission to use, copy, modify, and distribute this software and its
documentation for educational, research and non-profit purposes,
without fee, and without a written agreement is hereby granted,
provided that the above copyright notice, this paragraph and the
following three paragraphs appear in all copies.
This software program and documentation are copyrighted by The Regents
of the University of California. The software program and
documentation are supplied "as is", without any accompanying services
from The Regents. The Regents does not warrant that the operation of
the program will be uninterrupted or error-free. The end-user
understands that the program was developed for research purposes and
is advised not to rely exclusively on the program for any reason.
This software embodies a method for which the following patent has
been issued: "Method and apparatus for identifying scale invariant
features in an image and use of same for locating an object in an
image," David G. Lowe, US Patent 6,711,293 (March 23,
2004). Provisional application filed March 8, 1999. Asignee: The
University of British Columbia.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND
ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF
CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include"mex.h"
#include<mexutils.c>
#include<stdlib.h>
/** Matlab driver.
**/
#define greater(a,b) ((a) > (b)+threshold)
void
mexFunction(int nout, mxArray *out[],
int nin, const mxArray *in[])
{
int M, N ;
const double* F_pt ;
int ndims ;
int pdims = -1 ;
int* offsets ;
int* midx ;
int* neighbors ;
int nneighbors ;
int* dims ;
enum {F=0,THRESHOLD,P} ;
enum {MAXIMA=0} ;
double threshold = - mxGetInf() ;
/* ------------------------------------------------------------------
* Check the arguments
* --------------------------------------------------------------- */
if(nin > 1) {
if(!uIsRealScalar(in[THRESHOLD])) {
mexErrMsgTxt("THRESHOLD must be a real scalar.") ;
}
threshold = *mxGetPr(in[THRESHOLD]) ;
}
ndims = mxGetNumberOfDimensions(in[F]) ;
if(ndims !=3)
printf("Error Error: NDIMS not equal to 3. Not handled by C version\n");
{
/* We need to make a copy because in one special case (see below)
we need to adjust dims[].
*/
int d ;
const int* const_dims = (int*) mxGetDimensions(in[F]) ;
dims = mxMalloc(sizeof(int)*ndims) ;
for(d=0 ; d < ndims ; ++d)
{
/* printf("Const Dimensions = %d\n", const_dims[d]);
*/
dims[d] = const_dims[d] ;
}
}
M = dims[0] ;
N = dims[1] ;
F_pt = mxGetPr(in[F]) ;
/*
If there are only two dimensions and if one is singleton, then
assume that a vector has been provided as input (and treat this
as a COLUMN matrix with p=1). We do this because Matlab does not
distinguish between vectors and 1xN or Mx1 matrices and because
the cases 1xN and Mx1 are trivial (the result is alway empty).
*/
if((ndims == 2) && (pdims < 0) && (M == 1 || N == 1)) {
printf("ERROR ERROR: Entered a different loop here. Not handled by C version");
pdims = 1 ;
M = (M>N)?M:N ;
N = 1 ;
dims[0]=M ;
dims[1]=N ;
}
/* search the local maxima along the first p dimensions only */
if(pdims < 0)
{
pdims = ndims ;
}
if(pdims > ndims) {
mxFree(dims) ;
mexErrMsgTxt("P must not be greater than the number of dimensions") ;
}
/* ------------------------------------------------------------------
* Do the job
* --------------------------------------------------------------- */
{
int maxima_size = M*N ;
int* maxima_start = (int*) mxMalloc(sizeof(int) * maxima_size) ;
int* maxima_iterator = maxima_start ;
int* maxima_end = maxima_start + maxima_size ;
int i,j,h,o ;
const double* pt = F_pt ;
/* Compute the offsets between dimensions. */
offsets = (int*) mxMalloc(sizeof(int) * ndims) ;
offsets[0] = 1 ;
for(h = 1 ; h < ndims ; ++h)
{
offsets[h] = offsets[h-1]*dims[h-1] ;
/* printf("%d:%d\t%d\n", h, offsets[h], dims[h-1]);
*/
}
/* Multi-index. */
midx = (int*) mxMalloc(sizeof(int) * ndims) ;
for(h = 0 ; h < ndims ; ++h)
midx[h] = 1 ;
/* Neighbors. */
nneighbors = 1 ;
o=0 ;
for(h = 0 ; h < pdims ; ++h) {
nneighbors *= 3 ;
midx[h] = -1 ;
o -= offsets[h] ;
}
nneighbors -= 1 ;
neighbors = (int*) mxMalloc(sizeof(int) * nneighbors) ;
/* Precompute offsets from offset(-1,...,-1,0,...0) to
* offset(+1,...,+1,0,...,0). */
i = 0 ;
while(true) {
if(o != 0)
neighbors[i++] = o ;
h = 0 ;
while( o += offsets[h], (++midx[h]) > 1 ) {
o -= 3*offsets[h] ;
midx[h] = -1 ;
if(++h >= pdims)
goto stop ;
}
}
stop: ;
/* Starts at the corner (1,1,...,1,0,0,...0) */
for(h = 0 ; h < pdims ; ++h) {
midx[h] = 1 ;
pt += offsets[h] ;
/* printf("%d:%x\t%d\n", h, pt, offsets[h]);
*/
}
for(h = pdims ; h < ndims ; ++h) {
midx[h] = 0 ;
}
/* ---------------------------------------------------------------
* Loop
* ------------------------------------------------------------ */
/*
If any dimension in the first P is less than 3 elements wide
then just return the empty matrix (if we proceed without doing
anything we break the carry reporting algorithm below).
*/
for(h=0 ; h < pdims ; ++h)
if(dims[h] < 3) goto end ;
while(true) {
/* Propagate carry along multi index midx */
h = 0 ;
while((midx[h]) >= dims[h] - 1) {
/* pt += 2*offsets[h] ; skip first and last el. */
midx[h] = 1 ;
if(++h >= pdims)
goto next_layer ;
++midx[h] ;
}
/* Scan neighbors */
{
double v = *pt ;
bool is_greater = (v >= threshold) ;
/* printf("%f\n", v);
*/
i = 0 ;
while(is_greater && i < nneighbors)
{
is_greater &= v > *(pt + neighbors[i++]) ;
}
/* Add the local maximum */
if(is_greater) {
/* Need more space? */
if(maxima_iterator == maxima_end) {
maxima_size += M*N ;
maxima_start = (int*) mxRealloc(maxima_start,
maxima_size*sizeof(int)) ;
maxima_end = maxima_start + maxima_size ;
maxima_iterator = maxima_end - M*N ;
}
*maxima_iterator++ = pt - F_pt + 1 ;
}
/* Go to next element */
pt += 1 ;
++midx[0] ;
continue ;
next_layer: ;
if( h >= ndims )
goto end ;
while((++midx[h]) >= dims[h]) {
midx[h] = 0 ;
if(++h >= ndims)
goto end ;
}
}
}
end:;
/* Return. */
{
double* M_pt ;
/* printf("%x\t%x\t%d\n", maxima_iterator, maxima_start, maxima_iterator-maxima_start);
*/
out[MAXIMA] = mxCreateDoubleMatrix
(1, maxima_iterator-maxima_start, mxREAL) ;
maxima_end = maxima_iterator ;
maxima_iterator = maxima_start ;
M_pt = mxGetPr(out[MAXIMA]) ;
while(maxima_iterator != maxima_end)
{
*M_pt++ = *maxima_iterator++ ;
}
}
/* Release space. */
mxFree(offsets) ;
mxFree(neighbors) ;
mxFree(midx) ;
mxFree(maxima_start) ;
}
mxFree(dims) ;
}
|