diff options
Diffstat (limited to 'SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.c')
-rwxr-xr-x | SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.c | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.c b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.c new file mode 100755 index 0000000..e8a37d6 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.c | |||
@@ -0,0 +1,294 @@ | |||
1 | /*================================================================ | ||
2 | * function [i,j] = cimgnbmap_star([nr,nc], nb_r, sample_rate) | ||
3 | * computes the neighbourhood index matrix of an image, | ||
4 | * with each neighbourhood sampled. | ||
5 | * Input: | ||
6 | * [nr,nc] = image size | ||
7 | * nb_r = neighbourhood radius, could be [r_i,r_j] for i,j | ||
8 | * sample_rate = sampling rate, default = 1 | ||
9 | |||
10 | * Output: | ||
11 | |||
12 | * [i,j] = each is a column vector, give indices of neighbour pairs | ||
13 | |||
14 | * UINT32 type | ||
15 | |||
16 | * i is of total length of valid elements, 0 for first row | ||
17 | |||
18 | * j is of length nr * nc + 1 | ||
19 | |||
20 | * | ||
21 | |||
22 | * See also: imgnbmap.c, id2cind.m | ||
23 | |||
24 | * | ||
25 | * Examples: | ||
26 | * [i,j] = imgnbmap(10, 20); % [10,10] are assumed | ||
27 | * | ||
28 | * Stella X. Yu, Nov 12, 2001. | ||
29 | |||
30 | % test sequence: | ||
31 | |||
32 | nr = 15; | ||
33 | |||
34 | nc = 15; | ||
35 | |||
36 | nbr = 1; | ||
37 | |||
38 | [i,j] = cimgnbmap([nr,nc], nbr); | ||
39 | |||
40 | mask = csparse(i,j,ones(length(i),1),nr*nc); | ||
41 | |||
42 | show_dist_w(rand(nr,nc),mask) | ||
43 | |||
44 | |||
45 | *=================================================================*/ | ||
46 | |||
47 | # include "mex.h" | ||
48 | |||
49 | # include "math.h" | ||
50 | |||
51 | void mexFunction( | ||
52 | int nargout, | ||
53 | mxArray *out[], | ||
54 | int nargin, | ||
55 | const mxArray *in[] | ||
56 | ) | ||
57 | { | ||
58 | /* declare variables */ | ||
59 | int nr, nc, np, nb, total; | ||
60 | |||
61 | double *dim, sample_rate; | ||
62 | int r_i, r_j, a1, a2, b1, b2, self, neighbor; | ||
63 | int i, j, k, s, t, nsamp, th_rand, no_sample; | ||
64 | /* unsigned long *p, *qi, *qj; */ | ||
65 | unsigned int *p, *qi, *qj; | ||
66 | |||
67 | /* check argument */ | ||
68 | if (nargin < 2) { | ||
69 | mexErrMsgTxt("Two input arguments required"); | ||
70 | } | ||
71 | if (nargout> 2) { | ||
72 | mexErrMsgTxt("Too many output arguments."); | ||
73 | } | ||
74 | |||
75 | |||
76 | /* get image size */ | ||
77 | |||
78 | i = mxGetM(in[0]); | ||
79 | j = mxGetN(in[0]); | ||
80 | dim = mxGetData(in[0]); | ||
81 | nr = (int)dim[0]; | ||
82 | if (j>1 || i>1) { | ||
83 | nc = (int)dim[1]; | ||
84 | } else { | ||
85 | nc = nr; | ||
86 | } | ||
87 | np = nr * nc; | ||
88 | |||
89 | |||
90 | /* get neighbourhood size */ | ||
91 | i = mxGetM(in[1]); | ||
92 | j = mxGetN(in[1]); | ||
93 | dim = mxGetData(in[1]); | ||
94 | r_i = (int)dim[0]; | ||
95 | |||
96 | if (j>1 || i>1) { | ||
97 | r_j = (int)dim[1]; | ||
98 | } else { | ||
99 | r_j = r_i; | ||
100 | } | ||
101 | |||
102 | if (r_i<0) { r_i = 0; } | ||
103 | |||
104 | if (r_j<0) { r_j = 0; } | ||
105 | |||
106 | |||
107 | |||
108 | /* get sample rate */ | ||
109 | |||
110 | if (nargin==3) { | ||
111 | |||
112 | sample_rate = (mxGetM(in[2])==0) ? 1: mxGetScalar(in[2]); | ||
113 | |||
114 | } else { | ||
115 | |||
116 | sample_rate = 1; | ||
117 | |||
118 | } | ||
119 | |||
120 | /* prepare for random number generator */ | ||
121 | if (sample_rate<1) { | ||
122 | srand( (unsigned)time( NULL ) ); | ||
123 | |||
124 | th_rand = (int)ceil((double)RAND_MAX * sample_rate); | ||
125 | no_sample = 0; | ||
126 | } else { | ||
127 | |||
128 | sample_rate = 1; | ||
129 | th_rand = RAND_MAX; | ||
130 | no_sample = 1; | ||
131 | } | ||
132 | |||
133 | |||
134 | /* figure out neighbourhood size */ | ||
135 | |||
136 | nb = (4*r_i) + (4*r_j)+1; | ||
137 | if (nb>np) { | ||
138 | nb = np; | ||
139 | } | ||
140 | nb = (int)ceil((double)nb * sample_rate); | ||
141 | |||
142 | /*printf("nb=%d\n",nb);*/ | ||
143 | |||
144 | /* intermediate data structure */ | ||
145 | |||
146 | /* p = mxCalloc(np * (nb+1), sizeof(unsigned long));*/ | ||
147 | p = mxCalloc(np * (nb+1), sizeof(unsigned int)); | ||
148 | |||
149 | if (p==NULL) { | ||
150 | |||
151 | mexErrMsgTxt("Not enough space for my computation."); | ||
152 | |||
153 | } | ||
154 | |||
155 | |||
156 | |||
157 | /* computation */ | ||
158 | |||
159 | total = 0; | ||
160 | for (j=0; j<nc; j++) { | ||
161 | /*printf("j=%d\n",j);*/ | ||
162 | for (i=0; i<nr; i++) { | ||
163 | |||
164 | |||
165 | self = i + j * nr; | ||
166 | /* put self in, otherwise the index is not ordered */ | ||
167 | p[self] = p[self] + 1; | ||
168 | p[self+p[self]*np] = self; | ||
169 | |||
170 | /* j range */ | ||
171 | b1 = j; | ||
172 | b2 = j + r_j; | ||
173 | if (b2>=nc) { b2 = nc-1; } | ||
174 | |||
175 | |||
176 | /* i range */ | ||
177 | /*a1 = i - r_i; | ||
178 | |||
179 | if (a1<0) { a1 = 0; }*/ | ||
180 | a2 = i + r_i; | ||
181 | if (a2>=nr) { a2 = nr-1; } | ||
182 | |||
183 | |||
184 | /* number of more samples needed */ | ||
185 | |||
186 | nsamp = nb - p[self]; | ||
187 | |||
188 | /*if (nsamp<0) | ||
189 | {printf("nsamp=%d\n",nsamp);}*/ | ||
190 | k = 0; | ||
191 | t = b1; | ||
192 | s = i + 1; | ||
193 | |||
194 | if (s>a2) { | ||
195 | s = i; | ||
196 | t = t + 1; | ||
197 | } | ||
198 | |||
199 | |||
200 | |||
201 | while (k<nsamp && t<=b2) { | ||
202 | |||
203 | |||
204 | if (no_sample || (rand()<th_rand)) { | ||
205 | k = k + 1; | ||
206 | neighbor = s + t * nr; | ||
207 | |||
208 | p[self] = p[self] + 1; | ||
209 | |||
210 | p[self+p[self]*np] = neighbor; | ||
211 | p[neighbor] = p[neighbor] + 1; | ||
212 | |||
213 | p[neighbor+p[neighbor]*np] = self; | ||
214 | } | ||
215 | |||
216 | if (t==b1){ | ||
217 | s = s + 1; | ||
218 | if (s>a2) { | ||
219 | t = t + 1; | ||
220 | if (i+j-t>=0) | ||
221 | {s = i+j-t;} | ||
222 | else | ||
223 | {s=i;} | ||
224 | } | ||
225 | } | ||
226 | else { | ||
227 | if (s==i+j-t) {s=i;} | ||
228 | else{ if (s==i && s+t-j<nr) {s=s+t-j;} | ||
229 | else { | ||
230 | t = t + 1; | ||
231 | if (i+j-t>=0) | ||
232 | {s = i+j-t;} | ||
233 | else | ||
234 | {s=i;} | ||
235 | } | ||
236 | |||
237 | } | ||
238 | } | ||
239 | |||
240 | } /* k */ | ||
241 | |||
242 | total = total + p[self]; | ||
243 | } /* i */ | ||
244 | |||
245 | } /* j */ | ||
246 | |||
247 | |||
248 | |||
249 | /* i, j */ | ||
250 | |||
251 | out[0] = mxCreateNumericMatrix(total, 1, mxUINT32_CLASS, mxREAL); | ||
252 | |||
253 | out[1] = mxCreateNumericMatrix(np+1, 1, mxUINT32_CLASS, mxREAL); | ||
254 | |||
255 | qi = mxGetData(out[0]); | ||
256 | |||
257 | qj = mxGetData(out[1]); | ||
258 | |||
259 | |||
260 | if (out[0]==NULL || out[1]==NULL) { | ||
261 | |||
262 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
263 | |||
264 | } | ||
265 | |||
266 | |||
267 | |||
268 | total = 0; | ||
269 | |||
270 | for (j=0; j<np; j++) { | ||
271 | |||
272 | qj[j] = total; | ||
273 | |||
274 | s = j + np; | ||
275 | |||
276 | for (t=0; t<p[j]; t++) { | ||
277 | |||
278 | qi[total] = p[s]; | ||
279 | |||
280 | total = total + 1; | ||
281 | |||
282 | s = s + np; | ||
283 | |||
284 | } | ||
285 | |||
286 | } | ||
287 | |||
288 | qj[np] = total; | ||
289 | |||
290 | |||
291 | |||
292 | mxFree(p); | ||
293 | |||
294 | } | ||