summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/mser/src/matlab/mser.mex.c
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/benchmarks/mser/src/matlab/mser.mex.c')
-rwxr-xr-xSD-VBS/benchmarks/mser/src/matlab/mser.mex.c815
1 files changed, 815 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/mser/src/matlab/mser.mex.c b/SD-VBS/benchmarks/mser/src/matlab/mser.mex.c
new file mode 100755
index 0000000..8473afe
--- /dev/null
+++ b/SD-VBS/benchmarks/mser/src/matlab/mser.mex.c
@@ -0,0 +1,815 @@
1/* file: mser.mex.c
2** description: Maximally Stable Extremal Regions
3** author: Andrea Vedaldi
4**/
5
6/* AUTORIGHTS
7Copyright (C) 2006 Regents of the University of California
8All rights reserved
9
10Written by Andrea Vedaldi (UCLA VisionLab).
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met
14
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of the University of California, Berkeley nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36/** @file
37 ** @brief Maximally Stable Extremal Regions - MEX implementation
38 **/
39
40#include<mexutils.c>
41#include<stdio.h>
42#include<stdlib.h>
43#include<math.h>
44#include<string.h>
45#include<assert.h>
46
47#define MIN(x,y) (((x)<(y))?(x):(y))
48#define MAX(x,y) (((x)>(y))?(x):(y))
49
50#define BUCKETS 256
51
52#define USE_BUCKET_SORT
53/*#define USE_RANK_UNION
54*/
55
56typedef char unsigned val_t ;
57typedef int unsigned idx_t ;
58typedef long long int unsigned acc_t ;
59
60/* pairs are used to sort the pixels */
61typedef struct
62{
63 val_t value ;
64 idx_t index ;
65} pair_t ;
66
67/* forest node */
68typedef struct
69{
70 idx_t parent ; /**< parent pixel */
71 idx_t shortcut ; /**< shortcut to the root */
72 idx_t region ; /**< index of the region */
73 int area ; /**< area of the region */
74#ifdef USE_RANK_UNION
75 int height ; /**< node height */
76#endif
77} node_t ;
78
79/* extremal regions */
80typedef struct
81{
82 idx_t parent ; /**< parent region */
83 idx_t index ; /**< index of root pixel */
84 val_t value ; /**< value of root pixel */
85 int area ; /**< area of the region */
86 int area_top ; /**< area of the region DELTA levels above */
87 int area_bot ; /**< area of the region DELTA levels below */
88 float variation ; /**< variation */
89 int maxstable ; /**< max stable number (=0 if not maxstable) */
90} region_t ;
91
92/* predicate used to sort pixels by increasing intensity */
93int
94cmp_pair(void const* a, void const* b)
95{
96 pair_t* pa = (pair_t*) a;
97 pair_t* pb = (pair_t*) b;
98 return pa->value - pb->value ;
99}
100
101/* advance N-dimensional subscript */
102void
103adv(int const* dims, int ndims, int* subs_pt)
104{
105 int d = 0 ;
106 while(d < ndims) {
107 if( ++subs_pt[d] < dims[d] ) return ;
108 subs_pt[d++] = 0 ;
109 }
110}
111
112/* driver */
113void
114mexFunction(int nout, mxArray *out[],
115 int nin, const mxArray *in[])
116{
117 enum {IN_I=0, IN_DELTA} ;
118 enum {OUT_REGIONS=0, OUT_ELL, OUT_PARENTS, OUT_AREA} ;
119
120 idx_t i ;
121 idx_t rindex = 0 ;
122 int k ;
123
124 /* configuration */
125 int verbose = 0 ; /* be verbose */
126 int small_cleanup= 1 ; /* remove very small regions */
127 int big_cleanup = 1 ; /* remove very big regions */
128 int bad_cleanup = 0 ; /* remove very bad regions */
129 int dup_cleanup = 1 ; /* remove duplicates */
130 val_t delta ; /* stability delta */
131
132 /* node value denoting a void node */
133 idx_t const node_is_void = 0xffffffff ;
134
135 int* subs_pt ; /* N-dimensional subscript */
136 int* nsubs_pt ; /* diff-subscript to point to neigh. */
137 idx_t* strides_pt ; /* strides to move in image array */
138 idx_t* visited_pt ; /* flag */
139
140 int nel ; /* number of image elements (pixels) */
141 int ner = 0 ; /* number of extremal regions */
142 int nmer = 0 ; /* number of maximally stable */
143 int ndims ; /* number of dimensions */
144 int const* dims ; /* dimensions */
145 int njoins = 0 ; /* number of join ops */
146
147 val_t const* I_pt ; /* source image */
148 pair_t* pairs_pt ; /* scratch buffer to sort pixels */
149 node_t* forest_pt ; /* the extremal regions forest */
150 region_t* regions_pt ; /* list of extremal regions found */
151
152 /* ellipses fitting */
153 acc_t* acc_pt ; /* accumulator to integrate region moments */
154 acc_t* ell_pt ; /* ellipses parameters */
155 int gdl ; /* number of parameters of an ellipse */
156 idx_t* joins_pt ; /* sequence of joins */
157
158 /** -----------------------------------------------------------------
159 ** Check the arguments
160 ** -------------------------------------------------------------- */
161 if (nin != 2) {
162 mexErrMsgTxt("Two arguments required.") ;
163 } else if (nout > 4) {
164 mexErrMsgTxt("Too many output arguments.");
165 }
166
167 if(mxGetClassID(in[IN_I]) != mxUINT8_CLASS) {
168 mexErrMsgTxt("I must be of class UINT8") ;
169 }
170
171 if(!uIsScalar(in[IN_DELTA])) {
172 mexErrMsgTxt("DELTA must be scalar") ;
173 }
174
175 delta = 0 ;
176 switch(mxGetClassID(in[IN_DELTA])) {
177 case mxUINT8_CLASS :
178 delta = * (val_t*) mxGetData(in[IN_DELTA]) ;
179 break ;
180
181 case mxDOUBLE_CLASS :
182 {
183 double x = *mxGetPr(in[IN_DELTA]) ;
184 if(x < 0.0) {
185 mexErrMsgTxt("DELTA must be non-negative") ;
186 }
187 delta = (val_t) x ;
188 }
189 break ;
190