diff options
Diffstat (limited to 'SD-VBS/common/toolbox/MultiNcut')
75 files changed, 4657 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/MultiNcut/MNcut.m b/SD-VBS/common/toolbox/MultiNcut/MNcut.m new file mode 100755 index 0000000..5486080 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/MNcut.m | |||
@@ -0,0 +1,93 @@ | |||
1 | function [NcutDiscretes,eigenVectors,eigenValues] = MNcut(I,nsegs); | ||
2 | % | ||
3 | % [NcutDiscrete,eigenVectors,eigenValues] = MNcut(I,nsegs); | ||
4 | % | ||
5 | % | ||
6 | |||
7 | [nr,nc,nb] = size(I); | ||
8 | |||
9 | max_image_size = max(nr,nc); | ||
10 | |||
11 | % modified by song, 06/13/2005 | ||
12 | % test parameters | ||
13 | if (1) % original settings | ||
14 | if (max_image_size>120) & (max_image_size<=500), | ||
15 | % use 3 levels, | ||
16 | data.layers.number=3; | ||
17 | data.layers.dist=3; | ||
18 | data.layers.weight=[3000,4000,10000]; | ||
19 | data.W.scales=[1,2,3];%[1,2,3]; | ||
20 | data.W.radius=[2,3,7];%[2,3,7]; | ||
21 | elseif (max_image_size >500), | ||
22 | % use 4 levels, | ||
23 | data.layers.number=4; | ||
24 | data.layers.dist=3; | ||
25 | data.layers.weight=[3000,4000,10000,20000]; | ||
26 | data.W.scales=[1,2,3,3]; | ||
27 | data.W.radius=[2,3,4,6]; | ||
28 | elseif (max_image_size <=120) | ||
29 | data.layers.number=2; | ||
30 | data.layers.dist=3; | ||
31 | data.layers.weight=[3000,10000]; | ||
32 | data.W.scales=[1,2]; | ||
33 | data.W.radius=[2,6]; | ||
34 | end | ||
35 | else % test setting | ||
36 | if (max_image_size>200) & (max_image_size<=500), | ||
37 | % use 3 levels, | ||
38 | data.layers.number=3; | ||
39 | data.layers.dist=3; | ||
40 | data.layers.weight=[3000,4000,10000]; | ||
41 | data.W.scales=[1,2,3];%[1,2,3]; | ||
42 | data.W.radius=[2,3,7];%[2,3,7]; | ||
43 | elseif (max_image_size >500), | ||
44 | % use 4 levels, | ||
45 | data.layers.number=4; | ||
46 | data.layers.dist=3; | ||
47 | data.layers.weight=[3000,4000,10000,20000]; | ||
48 | data.W.scales=[1,2,3,3]; | ||
49 | data.W.radius=[2,3,4,6]; | ||
50 | elseif (max_image_size <=200) | ||
51 | data.layers.number=2; | ||
52 | data.layers.dist=3; | ||
53 | data.layers.weight=[3000,10000]; | ||
54 | data.W.scales=[1,2]; | ||
55 | data.W.radius=[2,4]; | ||
56 | end | ||
57 | |||
58 | end; | ||
59 | |||
60 | |||
61 | data.W.edgeVariance=0.1; %0.1 | ||
62 | data.W.gridtype='square'; | ||
63 | data.W.sigmaI=0.12;%0.12 | ||
64 | data.W.sigmaX=1000; | ||
65 | data.W.mode='mixed'; | ||
66 | data.W.p=0; | ||
67 | data.W.q=0; | ||
68 | |||
69 | %eigensolver | ||
70 | data.dataGraphCut.offset = 100;% 10; %valeur sur diagonale de W (mieux vaut 10 pour valeurs negatives de W) | ||
71 | data.dataGraphCut.maxiterations=50;% voir | ||
72 | data.dataGraphCut.eigsErrorTolerance=1e-2;%1e-6; | ||
73 | data.dataGraphCut.valeurMin=1e-6;%1e-5;% utilise pour tronquer des valeurs et sparsifier des matrices | ||
74 | data.dataGraphCut.verbose = 0; | ||
75 | |||
76 | data.dataGraphCut.nbEigenValues=max(nsegs); | ||
77 | |||
78 | disp('computeEdge'); | ||
79 | [multiWpp,ConstraintMat, Wind,data,emag,ephase]= computeMultiW (I,data); | ||
80 | |||
81 | disp('Ncut'); | ||
82 | [eigenVectors,eigenValues]= eigSolve (multiWpp,ConstraintMat,data); | ||
83 | |||
84 | %NcutDiscretes = zeros(nr,nc,length(nsegs)); | ||
85 | NcutDiscretes = zeros(nr,nc,(nsegs)); | ||
86 | |||
87 | for j=1:length(nsegs), | ||
88 | nseg = nsegs(j); | ||
89 | [nr,nc,nb] = size(eigenVectors(:,:,1:nseg)); | ||
90 | [NcutDiscrete,evrotated] =discretisation(reshape(eigenVectors(:,:,1:nb),nr*nc,nb),nr,nc); | ||
91 | NcutDiscretes(:,:,j) = NcutDiscrete; | ||
92 | end | ||
93 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/MNcutDemo.m b/SD-VBS/common/toolbox/MultiNcut/MNcutDemo.m new file mode 100755 index 0000000..972a4eb --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/MNcutDemo.m | |||
@@ -0,0 +1,34 @@ | |||
1 | % MNcutDemo.m | ||
2 | % created by song, 06/13/2005 | ||
3 | % an exmaple of how to use and display MNcut | ||
4 | |||
5 | num_segs = [20]; | ||
6 | imageSize = 800; | ||
7 | |||
8 | img_filename = '/u/ikkjin/Benchmark/stitch/data/test/capitol/img1.jpg'; | ||
9 | |||
10 | I=readimage(img_filename,imageSize); | ||
11 | |||
12 | [SegLabel,eigenVectors,eigenValues]= MNcut(I,num_segs); | ||
13 | |||
14 | for j=1:size(SegLabel,3), | ||
15 | [gx,gy] = gradient(SegLabel(:,:,j)); | ||
16 | bw = (abs(gx)>0.1) + (abs(gy) > 0.1); | ||
17 | |||
18 | figure(1);clf; J1=showmask(double(I),bw); imagesc(J1);axis image; axis off; | ||
19 | set(gca, 'Position', [0 0 1 1]); | ||
20 | |||
21 | % cm = sprintf('print -djpeg %s/file%.4d-%.2d.jpg',OutputDir,id,num_segs(j)); disp(cm);eval(cm); | ||
22 | |||
23 | |||
24 | % figure(10);imagesc(SegLabel(:,:,j));axis image; axis off; | ||
25 | % set(gca, 'Position', [0 0 1 1]); | ||
26 | % cm = sprintf('print -djpeg %s/Seg%.4d-%.2d.jpg',OutputDir,id,num_segs(j));disp(cm);eval(cm); | ||
27 | |||
28 | % pause; | ||
29 | end | ||
30 | |||
31 | % fname = files(id).name; | ||
32 | %cm = sprintf('save %s/SegLabl%.4d.mat I SegLabel fname',OutputDir,id); disp(cm); eval(cm); | ||
33 | %cm = sprintf('save %s/SegEig%.4d.mat eigenVectors eigenValues',OutputDir,id);disp(cm); eval(cm); | ||
34 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/README.tex b/SD-VBS/common/toolbox/MultiNcut/README.tex new file mode 100755 index 0000000..5970fb2 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/README.tex | |||
@@ -0,0 +1,9 @@ | |||
1 | 1) You need to first compile the .c files,type | ||
2 | |||
3 | >> compileAll('.'); | ||
4 | |||
5 | 2) the top level function is called MNcut.m | ||
6 | |||
7 | |||
8 | |||
9 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.c b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.c new file mode 100755 index 0000000..25def92 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.c | |||
@@ -0,0 +1,405 @@ | |||
1 | /*================================================================ | ||
2 | a_times_b_cmplx.c = used by a couple of mex functions | ||
3 | provide Matrix vector multiplications, | ||
4 | and solve triangular systems | ||
5 | (sparse matrix and full vector) | ||
6 | |||
7 | CSC_CmplxVecMult_CAB_double, CSR_CmplxVecMult_CAB_double, | ||
8 | CSCsymm_CmplxVecMult_CAB_double added by Mirko Visontai (10/24/2003) | ||
9 | |||
10 | *=================================================================*/ | ||
11 | # include "math.h" | ||
12 | |||
13 | |||
14 | /*C<-a*A*B+C*/ | ||
15 | void CSC_VecMult_CaABC_double( | ||
16 | const int m, const int k, const double alpha, | ||
17 | const double *val, const int *indx, | ||
18 | const int *pntrb, | ||
19 | const double *b, | ||
20 | double *c) | ||
21 | { | ||
22 | int i,j,jb,je; | ||
23 | |||
24 | for (i=0;i!=k;i++){ | ||
25 | jb = pntrb[i]; | ||
26 | je = pntrb[i+1]; | ||
27 | for (j=jb;j!=je;j++) | ||
28 | c[indx[j]] += alpha * b[i] * val[j]; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | /*C<-a*A'*B+C*/ | ||
33 | void CSR_VecMult_CaABC_double( | ||
34 | const int k, const int m, const double alpha, | ||
35 | const double *val, const int *indx, | ||
36 | const int *pntrb, | ||
37 | const double *b, | ||
38 | double *c) | ||
39 | { | ||
40 | double t; | ||
41 | const double *pval; | ||
42 | int i,j,jb,je; | ||
43 | |||
44 | pval = val; | ||
45 | for (i=0;i!=m;i++) { | ||
46 | t = 0; | ||
47 | jb = pntrb[i]; | ||
48 | je = pntrb[i+1]; | ||
49 | for (j=jb;j!=je;j++) | ||
50 | t += alpha * b[indx[j]] * (*pval++); | ||
51 | c[i] += t; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | |||
56 | /*C<-A*b */ | ||
57 | void CSC_VecMult_CAB_double( | ||
58 | const int m, const int k, /*nb_rows, nb_columns*/ | ||
59 | const double *val, const int *indx, | ||
60 | const int *pntrb, | ||
61 | const double *b, | ||
62 | double *c | ||
63 | ) | ||
64 | { | ||
65 | int i,j,jb,je; | ||
66 | double *pc=c; | ||
67 | for (i=0;i!=m;i++) *pc++ = 0; | ||
68 | |||
69 | for (i=0;i!=k;i++){ | ||
70 | jb = pntrb[i]; | ||
71 | je = pntrb[i+1]; | ||
72 | for (j=jb;j!=je;j++) | ||
73 | c[indx[j]] += b[i] * val[j]; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | /*C<-A*b (complex)*/ | ||
78 | void CSC_CmplxVecMult_CAB_double( | ||
79 | const int m, const int k, | ||
80 | const double *valr, const double *vali, | ||
81 | const int *indx, | ||
82 | const int *pntrb, | ||
83 | const double *br, const double *bi, | ||
84 | double *cr, double *ci | ||
85 | ) | ||
86 | { | ||
87 | int i,j,jb,je; | ||
88 | double *pcr=cr; | ||
89 | double *pci=ci; | ||
90 | for (i=0;i!=m;i++){ | ||
91 | *pcr++ = 0.0; | ||
92 | *pci++ = 0.0; | ||
93 | } | ||
94 | |||
95 | for (i=0;i!=k;i++){ | ||
96 | jb = pntrb[i]; | ||
97 | je = pntrb[i+1]; | ||
98 | for (j=jb;j!=je;j++){ | ||
99 | cr[indx[j]] += (br[i] * valr[j]) - (bi[i] * vali[j]); | ||
100 | ci[indx[j]] += (br[i] * vali[j]) + (bi[i] * valr[j]); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | /*C<-A'*b | ||
106 | plus rapide que CSC_VecMult_CAB_double */ | ||
107 | void CSR_VecMult_CAB_double( | ||
108 | const int k, const int m, | ||
109 | const double *val, const int *indx, | ||
110 | const int *pntrb, | ||
111 | const double *b, | ||
112 | double *c | ||
113 | ) | ||
114 | { | ||
115 | double t; | ||
116 | const double *pval; | ||
117 | double *pc=c; | ||
118 | int i,j,jb,je; | ||
119 | |||
120 | for (i=0;i!=m;i++) *pc++ = 0; | ||
121 | |||
122 | pval = val; | ||
123 | for (i=0;i!=m;i++) { | ||
124 | t = 0; | ||
125 | jb = pntrb[i]; | ||
126 | je = pntrb[i+1]; | ||
127 | for (j=jb;j!=je;j++) | ||
128 | t += b[indx[j]] * (*pval++); | ||
129 | c[i] += t; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | /*C<-A'*b (complex) | ||
134 | plus rapide que CSC_VecMult_CAB_double */ | ||
135 | void CSR_CmplxVecMult_CAB_double( | ||
136 | const int k, const int m, | ||
137 | const double *valr, const double *vali, | ||
138 | const int *indx, | ||
139 | const int *pntrb, | ||
140 | const double *br, const double *bi, | ||
141 | double *cr, double *ci | ||
142 | ) | ||
143 | { | ||
144 | double tr, ti; | ||
145 | const double *pvalr; | ||
146 | const double *pvali; | ||
147 | double *pcr=cr; | ||
148 | double *pci=ci; | ||
149 | int i,j,jb,je; | ||
150 | |||
151 | for (i=0;i!=m;i++){ | ||
152 | *pcr++ = 0.0; | ||
153 | *pci++ = 0.0; | ||
154 | } | ||
155 | |||
156 | pvalr = valr; | ||
157 | pvali = vali; | ||
158 | for (i=0;i!=m;i++) { | ||
159 | tr = 0.0; | ||
160 | ti = 0.0; | ||
161 | jb = pntrb[i]; | ||
162 | je = pntrb[i+1]; | ||
163 | for (j=jb;j!=je;j++){ | ||
164 | tr += (br[indx[j]] * (*pvalr)) - (bi[indx[j]] * (*pvali)); | ||
165 | ti += (br[indx[j]] * (*pvali++)) + (bi[indx[j]] * (*pvalr++)); | ||
166 | } | ||
167 | cr[i] += tr; | ||
168 | ci[i] += ti; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | |||
173 | |||
174 | /* C<-A*b (A is symmetric) */ | ||
175 | void CSRsymm_VecMult_CAB_double( | ||
176 | const int k, const int m, | ||
177 | const double *val, const int *indx, | ||
178 | const int *pntrb, | ||
179 | const double *b, | ||
180 | double *c | ||
181 | ) | ||
182 | { | ||
183 | const double *pval; | ||
184 | double *pc=c; | ||
185 | int i,j; | ||
186 | int jj; | ||
187 | int rpntrb, rpntre; | ||
188 | int index, nvals; | ||
189 | |||
190 | |||
191 | for (i=0;i!=m;i++) *pc++ = 0; | ||
192 | pval = val; | ||
193 | for (j=0;j!=k;j++){ | ||
194 | rpntrb = pntrb[j]; | ||
195 | rpntre = pntrb[j+1]; | ||
196 | for (jj=rpntrb;jj!=rpntre;jj++) { | ||
197 | index = indx[jj]; | ||
198 | if ( index == j ) { | ||
199 | c[j] += b[j] * (*pval++); | ||
200 | continue; | ||
201 | } | ||
202 | if ( index > j ) { | ||
203 | c[index] += b[j] * (*pval); | ||
204 | |||
205 | c[j] += b[index] * (*pval++); | ||
206 | } | ||
207 | else { | ||
208 | pval++; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | |||
215 | /* C<-A*b (A is symmetric and complex) */ | ||
216 | void CSRsymm_CmplxVecMult_CAB_double( | ||
217 | const int k, const int m, | ||
218 | const double *valr, const double *vali, | ||
219 | const int *indx, | ||
220 | const int *pntrb, | ||
221 | const double *br, const double *bi, | ||
222 | double *cr, double *ci | ||
223 | ) | ||
224 | { | ||
225 | const double *pvalr, *pvali; | ||
226 | double *pcr=cr; | ||
227 | double *pci=ci; | ||
228 | int i,j; | ||
229 | int jj; | ||
230 | int rpntrb, rpntre; | ||
231 | int index, nvals; | ||
232 | |||
233 | |||
234 | for (i=0;i!=m;i++){ | ||
235 | *pcr++ = 0.0; | ||
236 | *pci++ = 0.0; | ||
237 | } | ||
238 | |||
239 | pvalr = valr; | ||
240 | pvali = vali; | ||
241 | for (j=0;j!=k;j++){ | ||
242 | rpntrb = pntrb[j]; | ||
243 | rpntre = pntrb[j+1]; | ||
244 | for (jj=rpntrb;jj!=rpntre;jj++) { | ||
245 | index = indx[jj]; | ||
246 | if ( index == j ) { | ||
247 | cr[j] += (br[j] * (*pvalr)) - (bi[j] * (*pvali)); | ||
248 | ci[j] += (br[j] * (*pvali++)) + (bi[j] * (*pvalr++)); | ||
249 | continue; | ||
250 | } | ||
251 | if ( index > j ) { | ||
252 | cr[index] += (br[j] * (*pvalr)) - (bi[j] * (*pvali)); | ||
253 | ci[index] += (br[j] * (*pvali)) + (bi[j] * (*pvalr)); | ||
254 | |||
255 | cr[j] += (br[index] * (*pvalr)) - (bi[index] * (*pvali)); | ||
256 | ci[j] += (br[index] * (*pvali++)) + (bi[index] * (*pvalr++)); | ||
257 | } | ||
258 | else { | ||
259 | pvalr++; | ||
260 | pvali++; | ||
261 | } | ||
262 | |||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | |||
268 | /*C<-A\B; with Lower triangular A*/ | ||
269 | void CSC_VecTriangSlvLD_CAB_double( | ||
270 | const int m, | ||
271 | const double *val, | ||
272 | const int *indx, const int *pntrb, | ||
273 | const double *b, | ||
274 | double *c) | ||
275 | { | ||
276 | int i, j, jb, je; | ||
277 | double *pc=c; | ||
278 | double z; | ||
279 | |||
280 | for (i=0;i!=m;i++){ | ||
281 | *pc = b[i]; | ||
282 | pc++; | ||
283 | } | ||
284 | |||
285 | pc=c; | ||
286 | for (i=0;i!=m;i++) { | ||
287 | jb = pntrb[i]; | ||
288 | je = pntrb[i+1]; | ||
289 | z = pc[i] / val[jb]; | ||
290 | pc[i] = z; | ||
291 | for (j=jb+1;j<je;j++) { | ||
292 | c[indx[j]] -= z*val[j]; | ||
293 | } | ||
294 | } | ||
295 | } | ||
296 | |||
297 | /*C<-A\B; with Upper triangular A*/ | ||
298 | void CSC_VecTriangSlvUD_CAB_double( | ||
299 | const int m, | ||
300 | const double *val, | ||
301 | const int *indx, const int *pntrb, | ||
302 | const double *b, | ||
303 | double *c) | ||
304 | { | ||
305 | int i, j, jb, je, index; | ||
306 | double *pc=c; | ||
307 | double z; | ||
308 | |||
309 | for (i=0;i!=m;i++){ | ||
310 | *pc = b[i]; | ||
311 | pc++; | ||
312 | } | ||
313 | |||
314 | pc=c; | ||
315 | for (i=m-1;i!=-1;i--) { | ||
316 | jb = pntrb[i]; | ||
317 | je = pntrb[i+1]-1; | ||
318 | z = pc[i] /val[je]; | ||
319 | pc[i] = z; | ||
320 | for (j=jb;j<je;j++) { | ||
321 | c[indx[j]] -= z * val[j]; | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | /*C<-A'\B; where A is upper (little slower than CSC)*/ | ||
326 | void CSR_VecTriangSlvLD_CAB_double( | ||
327 | const int m, | ||
328 | const double *val, | ||
329 | const int *indx, const int *pntrb, | ||
330 | const double *b, | ||
331 | double *c) | ||
332 | { | ||
333 | int i, j, jb, je, index; | ||
334 | double *pc=c; | ||
335 | double z; | ||
336 | double valtmp; | ||
337 | |||
338 | pc=c; | ||
339 | for (i=0;i!=m;i++) { | ||
340 | z = 0; | ||
341 | jb = pntrb[i]; | ||
342 | je = pntrb[i+1]; | ||
343 | for (j=jb;j<je;j++) { | ||
344 | index = indx[j]; | ||
345 | if ( index == i ) { | ||
346 | valtmp = val[j]; | ||
347 | } else { | ||
348 | z += c[index] * val[j]; | ||
349 | } | ||
350 | } | ||
351 | pc[i] = (b[i] - z) / valtmp; | ||
352 | } | ||
353 | } | ||
354 | |||
355 | /*C<-A'\B; where A is lower (little slower than CSC)*/ | ||
356 | void CSR_VecTriangSlvUD_CAB_double( | ||
357 | const int m, | ||
358 | const double *val, | ||
359 | const int *indx, const int *pntrb, | ||
360 | const double *b, | ||
361 | double *c) | ||
362 | { | ||
363 | int i, j, jb, je, index; | ||
364 | double *pc=c; | ||
365 | double valtmp; | ||
366 | double z; | ||
367 | |||
368 | pc=c; | ||
369 | for (i=m-1;i!=-1; i--) { | ||
370 | z = 0; | ||
371 | jb = pntrb[i]; | ||
372 | je = pntrb[i+1]; | ||
373 | for (j=jb+1; j<je; j++) { | ||
374 | z += c[indx[j]] * val[j]; | ||
375 | } | ||
376 | pc[i] = (b[i] - z) / val[jb]; | ||
377 | } | ||
378 | } | ||
379 | |||
380 | /*C<-A*B, where A is (m,k) and B is (k,n)*/ | ||
381 | void CSC_MatMult_CAB_double( | ||
382 | const int m, const int n, const int k, | ||
383 | const double *val, const int *indx, | ||
384 | const int *pntrb, | ||
385 | const double *b, const int ldb, | ||
386 | double *c, const int ldc) | ||
387 | { | ||
388 | int i,j,jb,je; | ||
389 | double *pc=c; | ||
390 | int l; | ||
391 | |||
392 | for (l=0;l!=n;l++) | ||
393 | for (i=0;i!=m;i++) *pc++ = 0; | ||
394 | |||
395 | for (l=0;l!=n;l++) { | ||
396 | for (i=0;i!=k;i++){ | ||
397 | jb = pntrb[i]; | ||
398 | je = pntrb[i+1]; | ||
399 | for (j=jb;j!=je;j++) | ||
400 | c[indx[j]] += b[i] * val[j]; | ||
401 | } | ||
402 | /*c += ldc; b += ldb; */ | ||
403 | c += m; b += m; | ||
404 | } | ||
405 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.dll b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.dll new file mode 100755 index 0000000..5656f92 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexa64 b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexa64 new file mode 100755 index 0000000..1ab8e8c --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexglx b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexglx new file mode 100755 index 0000000..4138128 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexmac b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexmac new file mode 100755 index 0000000..65232e8 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/a_times_b_cmplx.mexmac | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/affinityic.c b/SD-VBS/common/toolbox/MultiNcut/affinityic.c new file mode 100755 index 0000000..d78761b --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/affinityic.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /*================================================================ | ||
2 | * function w = affinityic(emag,ephase,pi,pj,sigma) | ||
3 | * Input: | ||
4 | * emag = edge strength at each pixel | ||
5 | * ephase = edge phase at each pixel | ||
6 | * [pi,pj] = index pair representation for MALTAB sparse matrices | ||
7 | * sigma = sigma for IC energy | ||
8 | * Output: | ||
9 | * w = affinity with IC at [pi,pj] | ||
10 | * | ||
11 | |||
12 | % test sequence | ||
13 | f = synimg(10); | ||
14 | [i,j] = cimgnbmap(size(f),2); | ||
15 | [ex,ey,egx,egy] = quadedgep(f); | ||
16 | a = affinityic(ex,ey,egx,egy,i,j) | ||
17 | show_dist_w(f,a); | ||
18 | |||
19 | * Stella X. Yu, Nov 19, 2001. | ||
20 | *=================================================================*/ | ||
21 | |||
22 | # include "mex.h" | ||
23 | # include "math.h" | ||
24 | |||
25 | void mexFunction( | ||
26 | int nargout, | ||
27 | mxArray *out[], | ||
28 | int nargin, | ||
29 | const mxArray *in[] | ||
30 | ) | ||
31 | { | ||
32 | /* declare variables */ | ||
33 | int nr, nc, np, total; | ||
34 | int i, j, k, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step; | ||
35 | double sigma, di, dj, a, z, maxori, phase1, phase2, slope; | ||
36 | int *ir, *jc; | ||
37 | unsigned int *pi,*pj; | ||
38 | /* unsigned long *pi, *pj; */ | ||
39 | double *emag, *ephase, *w; | ||
40 | |||
41 | /* check argument */ | ||
42 | if (nargin<4) { | ||
43 | mexErrMsgTxt("Four input arguments required"); | ||
44 | } | ||
45 | if (nargout>1) { | ||
46 | mexErrMsgTxt("Too many output arguments"); | ||
47 | } | ||
48 | |||
49 | /* get edgel information */ | ||
50 | nr = mxGetM(in[0]); | ||
51 | nc = mxGetN(in[0]); | ||
52 | if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) { | ||
53 | mexErrMsgTxt("Edge magnitude and phase shall be of the same image size"); | ||
54 | } | ||
55 | emag = mxGetPr(in[0]); | ||
56 | ephase = mxGetPr(in[1]); | ||
57 | np = nr * nc; | ||
58 | |||
59 | /* get new index pair */ | ||
60 | if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) { | ||
61 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
62 | } | ||
63 | if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) { | ||
64 | mexErrMsgTxt("Wrong index representation"); | ||
65 | } | ||
66 | pi = mxGetData(in[2]); | ||
67 | pj = mxGetData(in[3]); | ||
68 | |||
69 | /* create output */ | ||
70 | out[0] = mxCreateSparse(np,np,pj[np],mxREAL); | ||
71 | if (out[0]==NULL) { | ||
72 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
73 | } | ||
74 | w = mxGetPr(out[0]); | ||
75 | ir = mxGetIr(out[0]); | ||
76 | jc = mxGetJc(out[0]); | ||
77 | |||
78 | /* find my sigma */ | ||
79 | if (nargin<5) { | ||
80 | sigma = 0; | ||
81 | for (k=0; k<np; k++) { | ||
82 | if (emag[k]>sigma) { sigma = emag[k]; } | ||
83 | } | ||
84 | sigma = sigma / 6; | ||
85 | /* printf("sigma = %6.5f",sigma); */ | ||
86 | } else { | ||
87 | sigma = mxGetScalar(in[4]); | ||
88 | } | ||
89 | a = 0.5 / (sigma * sigma); | ||
90 | |||
91 | /* computation */ | ||
92 | total = 0; | ||
93 | for (j=0; j<np; j++) { | ||
94 | |||
95 | jc[j] = total; | ||
96 | jx = j / nr; /* col */ | ||
97 | jy = j % nr; /* row */ | ||
98 | |||
99 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
100 | |||
101 | i = pi[k]; | ||
102 | |||
103 | if (i==j) { | ||
104 | maxori = 1; | ||
105 | |||
106 | } else { | ||
107 | |||
108 | ix = i / nr; | ||
109 | iy = i % nr; | ||
110 | |||
111 | /* scan */ | ||
112 | di = (double) (iy - jy); | ||
113 | dj = (double) (ix - jx); | ||
114 | |||
115 | maxori = 0.; | ||
116 | phase1 = ephase[j]; | ||
117 | |||
118 | |||
119 | /* sample in i direction */ | ||
120 | if (abs(di) >= abs(dj)) { | ||
121 | slope = dj / di; | ||
122 | step = (iy>=jy) ? 1 : -1; | ||
123 | |||
124 | iip1 = jy; | ||
125 | jjp1 = jx; | ||
126 | |||
127 | |||
128 | for (ii=0;ii<abs(di);ii++){ | ||
129 | iip2 = iip1 + step; | ||
130 | jjp2 = (int)(0.5 + slope*(iip2-jy) + jx); | ||
131 | |||
132 | phase2 = ephase[iip2+jjp2*nr]; | ||
133 | |||
134 | if (phase1 != phase2) { | ||
135 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
136 | if (z > maxori){ | ||
137 | maxori = z; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | iip1 = iip2; | ||
142 | jjp1 = jjp2; | ||
143 | phase1 = phase2; | ||
144 | } | ||
145 | |||
146 | /* sample in j direction */ | ||
147 | } else { | ||
148 | slope = di / dj; | ||
149 | step = (ix>=jx) ? 1: -1; | ||
150 | |||
151 | jjp1 = jx; | ||
152 | iip1 = jy; | ||
153 | |||
154 | |||
155 | for (jj=0;jj<abs(dj);jj++){ | ||
156 | jjp2 = jjp1 + step; | ||
157 | iip2 = (int)(0.5+ slope*(jjp2-jx) + jy); | ||
158 | |||
159 | phase2 = ephase[iip2+jjp2*nr]; | ||
160 | |||
161 | if (phase1 != phase2){ | ||
162 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
163 | if (z > maxori){ | ||
164 | maxori = z; | ||
165 | } | ||
166 | |||
167 | } | ||
168 | |||
169 | iip1 = iip2; | ||
170 | jjp1 = jjp2; | ||
171 | phase1 = phase2; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | maxori = 0.5 * maxori; | ||
176 | maxori = exp(-maxori * maxori * a); | ||
177 | } | ||
178 | ir[total] = i; | ||
179 | |||
180 | w[total] = maxori; | ||
181 | total = total + 1; | ||
182 | |||
183 | } /* i */ | ||
184 | } /* j */ | ||
185 | |||
186 | jc[np] = total; | ||
187 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/affinityic.dll b/SD-VBS/common/toolbox/MultiNcut/affinityic.dll new file mode 100755 index 0000000..67e4d64 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/affinityic.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/affinityic.mexa64 b/SD-VBS/common/toolbox/MultiNcut/affinityic.mexa64 new file mode 100755 index 0000000..2648387 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/affinityic.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/affinityic.mexglx b/SD-VBS/common/toolbox/MultiNcut/affinityic.mexglx new file mode 100755 index 0000000..c296845 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/affinityic.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/batch_MNcut.m b/SD-VBS/common/toolbox/MultiNcut/batch_MNcut.m new file mode 100755 index 0000000..d4dcb3c --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/batch_MNcut.m | |||
@@ -0,0 +1,48 @@ | |||
1 | data_dir = '/data/insecure/qihuizhu/baseball/Gray/train/'; | ||
2 | save_dir = '/home/songgang/project/MultiNcut/batch_result_MNcut'; | ||
3 | |||
4 | num_segs = [20]; | ||
5 | imageSize = 200; | ||
6 | |||
7 | |||
8 | filelist = dir(fullfile(data_dir, '*.tif')); | ||
9 | |||
10 | nb_file = max(size(filelist)); | ||
11 | |||
12 | |||
13 | tic; | ||
14 | for ii = 1:nb_file | ||
15 | fprintf(2, 'Segmenting image: %s ...\n', filelist(ii).name); | ||
16 | |||
17 | img_filename = fullfile(data_dir, filelist(ii).name); | ||
18 | I=readimage(img_filename,imageSize); | ||
19 | |||
20 | |||
21 | [SegLabel,eigenVectors,eigenValues]= MNcut(I,num_segs); | ||
22 | |||
23 | for j=1:size(SegLabel,3), | ||
24 | [gx,gy] = gradient(SegLabel(:,:,j)); | ||
25 | bw = (abs(gx)>0.1) + (abs(gy) > 0.1); | ||
26 | |||
27 | figure(1);clf; J1=showmask(double(I),bw); imagesc(J1);axis image; axis off; | ||
28 | set(gca, 'Position', [0 0 1 1]); | ||
29 | set(gca, 'Position', [0 0 1 1]); | ||
30 | [PATHSTR,NAME,EXT,VERSN] = fileparts(filelist(ii).name); | ||
31 | print('-f1', '-djpeg90', fullfile(save_dir, sprintf('%s%s-%d.jpg', NAME,'-out', num_segs(j)))); | ||
32 | |||
33 | |||
34 | % cm = sprintf('print -djpeg %s/file%.4d-%.2d.jpg',OutputDir,id,num_segs(j)); disp(cm);eval(cm); | ||
35 | |||
36 | |||
37 | % figure(10);imagesc(SegLabel(:,:,j));axis image; axis off; | ||
38 | % set(gca, 'Position', [0 0 1 1]); | ||
39 | % cm = sprintf('print -djpeg %s/Seg%.4d-%.2d.jpg',OutputDir,id,num_segs(j));disp(cm);eval(cm); | ||
40 | |||
41 | % keyboard; | ||
42 | end | ||
43 | |||
44 | |||
45 | |||
46 | end; | ||
47 | toc; | ||
48 | fprintf(2, ' %d files done\n', nb_file); | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.c b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.c new file mode 100755 index 0000000..44af715 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.c | |||
@@ -0,0 +1,198 @@ | |||
1 | /*================================================================ | ||
2 | * function [i,j] = cimgnbmap([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 | * Output: | ||
10 | * [i,j] = each is a column vector, give indices of neighbour pairs | ||
11 | * UINT32 type | ||
12 | * i is of total length of valid elements, 0 for first row | ||
13 | * j is of length nr * nc + 1 | ||
14 | * | ||
15 | * See also: imgnbmap.c, id2cind.m | ||
16 | * | ||
17 | * Examples: | ||
18 | * [i,j] = imgnbmap(10, 20); % [10,10] are assumed | ||
19 | * | ||
20 | * Stella X. Yu, Nov 12, 2001. | ||
21 | |||
22 | % test sequence: | ||
23 | nr = 15; | ||
24 | nc = 15; | ||
25 | nbr = 1; | ||
26 | [i,j] = cimgnbmap([nr,nc], nbr); | ||
27 | mask = csparse(i,j,ones(length(i),1),nr*nc); | ||
28 | show_dist_w(rand(nr,nc),mask) | ||
29 | |||
30 | *=================================================================*/ | ||
31 | |||
32 | # include "mex.h" | ||
33 | # include "math.h" | ||
34 | |||
35 | void mexFunction( | ||
36 | int nargout, | ||
37 | mxArray *out[], | ||
38 | int nargin, | ||
39 | const mxArray *in[] | ||
40 | ) | ||
41 | { | ||
42 | /* declare variables */ | ||
43 | int nr, nc, np, nb, total; | ||
44 | double *dim, sample_rate; | ||
45 | int r_i, r_j, a1, a2, b1, b2, self, neighbor; | ||
46 | int i, j, k, s, t, nsamp, th_rand, no_sample; | ||
47 | /* unsigned long *p, *qi, *qj; */ | ||
48 | unsigned int *p, *qi, *qj; | ||
49 | |||
50 | /* check argument */ | ||
51 | if (nargin < 2) { | ||
52 | mexErrMsgTxt("Two input arguments required"); | ||
53 | } | ||
54 | if (nargout> 2) { | ||
55 | mexErrMsgTxt("Too many output arguments."); | ||
56 | } | ||
57 | |||
58 | /* get image size */ | ||
59 | i = mxGetM(in[0]); | ||
60 | j = mxGetN(in[0]); | ||
61 | dim = mxGetData(in[0]); | ||
62 | nr = (int)dim[0]; | ||
63 | if (j>1 || i>1) { | ||
64 | nc = (int)dim[1]; | ||
65 | } else { | ||
66 | nc = nr; | ||
67 | } | ||
68 | np = nr * nc; | ||
69 | |||
70 | /* get neighbourhood size */ | ||
71 | i = mxGetM(in[1]); | ||
72 | j = mxGetN(in[1]); | ||
73 | dim = mxGetData(in[1]); | ||
74 | r_i = (int)dim[0]; | ||
75 | if (j>1 || i>1) { | ||
76 | r_j = (int)dim[1]; | ||
77 | } else { | ||
78 | r_j = r_i; | ||
79 | } | ||
80 | if (r_i<0) { r_i = 0; } | ||
81 | if (r_j<0) { r_j = 0; } | ||
82 | |||
83 | /* get sample rate */ | ||
84 | if (nargin==3) { | ||
85 | sample_rate = (mxGetM(in[2])==0) ? 1: mxGetScalar(in[2]); | ||
86 | } else { | ||
87 | sample_rate = 1; | ||
88 | } | ||
89 | /* prepare for random number generator */ | ||
90 | if (sample_rate<1) { | ||
91 | srand( (unsigned)time( NULL ) ); | ||
92 | th_rand = (int)ceil((double)RAND_MAX * sample_rate); | ||
93 | no_sample = 0; | ||
94 | } else { | ||
95 | sample_rate = 1; | ||
96 | th_rand = RAND_MAX; | ||
97 | no_sample = 1; | ||
98 | } | ||
99 | |||
100 | /* figure out neighbourhood size */ | ||
101 | |||
102 | nb = (r_i + r_i + 1) * (r_j + r_j + 1); | ||
103 | if (nb>np) { | ||
104 | nb = np; | ||
105 | } | ||
106 | nb = (int)ceil((double)nb * sample_rate); | ||
107 | /*printf("nb=%d\n",nb);*/ | ||
108 | /* intermediate data structure */ | ||
109 | /* p = mxCalloc(np * (nb+1), sizeof(unsigned long)); */ | ||
110 | p = mxCalloc(np * (nb+1), sizeof(unsigned int)); | ||
111 | if (p==NULL) { | ||
112 | mexErrMsgTxt("Not enough space for my computation."); | ||
113 | } | ||
114 | |||
115 | /* computation */ | ||
116 | total = 0; | ||
117 | for (j=0; j<nc; j++) { | ||
118 | /*printf("j=%d\n",j);*/ | ||
119 | for (i=0; i<nr; i++) { | ||
120 | |||
121 | self = i + j * nr; | ||
122 | |||
123 | /* put self in, otherwise the index is not ordered */ | ||
124 | p[self] = p[self] + 1; | ||
125 | p[self+p[self]*np] = self; | ||
126 | |||
127 | /* j range */ | ||
128 | b1 = j; | ||
129 | b2 = j + r_j; | ||
130 | if (b2>=nc) { b2 = nc-1; } | ||
131 | |||
132 | /* i range */ | ||
133 | a1 = i - r_i; | ||
134 | if (a1<0) { a1 = 0; } | ||
135 | a2 = i + r_i; | ||
136 | if (a2>=nr) { a2 = nr-1; } | ||
137 | |||
138 | /* number of more samples needed */ | ||
139 | nsamp = nb - p[self]; | ||
140 | /*if (nsamp<0) | ||
141 | {printf("nsamp=%d\n",nsamp);}*/ | ||
142 | k = 0; | ||
143 | t = b1; | ||
144 | s = i + 1; | ||
145 | if (s>a2) { | ||
146 | s = a1; | ||
147 | t = t + 1; | ||
148 | } | ||
149 | |||
150 | |||
151 | while (k<nsamp && t<=b2) { | ||
152 | |||
153 | if (no_sample || (rand()<th_rand)) { | ||
154 | k = k + 1; | ||
155 | neighbor = s + t * nr; | ||
156 | p[self] = p[self] + 1; | ||
157 | p[self+p[self]*np] = neighbor; | ||
158 | p[neighbor] = p[neighbor] + 1; | ||
159 | p[neighbor+p[neighbor]*np] = self; | ||
160 | } | ||
161 | |||
162 | s = s + 1; | ||
163 | if (s>a2) { | ||
164 | s = a1; | ||
165 | t = t + 1; | ||
166 | } | ||
167 | } /* k */ | ||
168 | total = total + p[self]; | ||
169 | } /* i */ | ||
170 | |||
171 | } /* j */ | ||
172 | |||
173 | /* i, j */ | ||
174 | |||
175 | out[0] = mxCreateNumericMatrix(total, 1, mxUINT32_CLASS, mxREAL); | ||
176 | out[1] = mxCreateNumericMatrix(np+1, 1, mxUINT32_CLASS, mxREAL); | ||
177 | qi = mxGetData(out[0]); | ||
178 | qj = mxGetData(out[1]); | ||
179 | |||
180 | if (out[0]==NULL || out[1]==NULL) { | ||
181 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
182 | } | ||
183 | |||
184 | total = 0; | ||
185 | for (j=0; j<np; j++) { | ||
186 | qj[j] = total; | ||
187 | s = j + np; | ||
188 | for (t=0; t<p[j]; t++) { | ||
189 | qi[total] = p[s]; | ||
190 | total = total + 1; | ||
191 | s = s + np; | ||
192 | } | ||
193 | } | ||
194 | qj[np] = total; | ||
195 | |||
196 | mxFree(p); | ||
197 | |||
198 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.dll b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.dll new file mode 100755 index 0000000..368d76c --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexa64 b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexa64 new file mode 100755 index 0000000..6564652 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexglx b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexglx new file mode 100755 index 0000000..655849a --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.c b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.c new file mode 100755 index 0000000..7b1434c --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.c | |||
@@ -0,0 +1,197 @@ | |||
1 | /*================================================================ | ||
2 | * function [i,j] = cimgnbmap_cross([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 | * Output: | ||
10 | * [i,j] = each is a column vector, give indices of neighbour pairs | ||
11 | * UINT32 type | ||
12 | * i is of total length of valid elements, 0 for first row | ||
13 | * j is of length nr * nc + 1 | ||
14 | * | ||
15 | * See also: imgnbmap.c, id2cind.m | ||
16 | * | ||
17 | * Examples: | ||
18 | * [i,j] = imgnbmap(10, 20); % [10,10] are assumed | ||
19 | * | ||
20 | * Stella X. Yu, Nov 12, 2001. | ||
21 | |||
22 | % test sequence: | ||
23 | nr = 15; | ||
24 | nc = 15; | ||
25 | nbr = 1; | ||
26 | [i,j] = cimgnbmap([nr,nc], nbr); | ||
27 | mask = csparse(i,j,ones(length(i),1),nr*nc); | ||
28 | show_dist_w(rand(nr,nc),mask) | ||
29 | |||
30 | *=================================================================*/ | ||
31 | |||
32 | # include "mex.h" | ||
33 | # include "math.h" | ||
34 | |||
35 | void mexFunction( | ||
36 | int nargout, | ||
37 | mxArray *out[], | ||
38 | int nargin, | ||
39 | const mxArray *in[] | ||
40 | ) | ||
41 | { | ||
42 | /* declare variables */ | ||
43 | int nr, nc, np, nb, total; | ||
44 | double *dim, sample_rate; | ||
45 | int r_i, r_j, a1, a2, b1, b2, self, neighbor; | ||
46 | int i, j, k, s, t, nsamp, th_rand, no_sample; | ||
47 | /* unsigned long *p, *qi, *qj; */ | ||
48 | unsigned int *p, *qi, *qj; | ||
49 | |||
50 | /* check argument */ | ||
51 | if (nargin < 2) { | ||
52 | mexErrMsgTxt("Two input arguments required"); | ||
53 | } | ||
54 | if (nargout> 2) { | ||
55 | mexErrMsgTxt("Too many output arguments."); | ||
56 | } | ||
57 | |||
58 | /* get image size */ | ||
59 | i = mxGetM(in[0]); | ||
60 | j = mxGetN(in[0]); | ||
61 | dim = mxGetData(in[0]); | ||
62 | nr = (int)dim[0]; | ||
63 | if (j>1 || i>1) { | ||
64 | nc = (int)dim[1]; | ||
65 | } else { | ||
66 | nc = nr; | ||
67 | } | ||
68 | np = nr * nc; | ||
69 | |||
70 | /* get neighbourhood size */ | ||
71 | i = mxGetM(in[1]); | ||
72 | j = mxGetN(in[1]); | ||
73 | dim = mxGetData(in[1]); | ||
74 | r_i = (int)dim[0]; | ||
75 | if (j>1 || i>1) { | ||
76 | r_j = (int)dim[1]; | ||
77 | } else { | ||
78 | r_j = r_i; | ||
79 | } | ||
80 | if (r_i<0) { r_i = 0; } | ||
81 | if (r_j<0) { r_j = 0; } | ||
82 | |||
83 | /* get sample rate */ | ||
84 | if (nargin==3) { | ||
85 | sample_rate = (mxGetM(in[2])==0) ? 1: mxGetScalar(in[2]); | ||
86 | } else { | ||
87 | sample_rate = 1; | ||
88 | } | ||
89 | /* prepare for random number generator */ | ||
90 | if (sample_rate<1) { | ||
91 | srand( (unsigned)time( NULL ) ); | ||
92 | th_rand = (int)ceil((double)RAND_MAX * sample_rate); | ||
93 | no_sample = 0; | ||
94 | } else { | ||
95 | sample_rate = 1; | ||
96 | th_rand = RAND_MAX; | ||
97 | no_sample = 1; | ||
98 | } | ||
99 | |||
100 | /* figure out neighbourhood size */ | ||
101 | |||
102 | nb = (r_i + r_i) * (r_j + r_j)+1; | ||
103 | if (nb>np) { | ||
104 | nb = np; | ||
105 | } | ||
106 | nb = (int)ceil((double)nb * sample_rate); | ||
107 | /*printf("nb=%d\n",nb);*/ | ||
108 | /* intermediate data structure */ | ||
109 | /* p = mxCalloc(np * (nb+1), sizeof(unsigned long)); */ | ||
110 | p = mxCalloc(np * (nb+1), sizeof(unsigned int)); | ||
111 | if (p==NULL) { | ||
112 | mexErrMsgTxt("Not enough space for my computation."); | ||
113 | } | ||
114 | |||
115 | /* computation */ | ||
116 | total = 0; | ||
117 | for (j=0; j<nc; j++) { | ||
118 | /*printf("j=%d\n",j);*/ | ||
119 | for (i=0; i<nr; i++) { | ||
120 | |||
121 | self = i + j * nr; | ||
122 | |||
123 | /* put self in, otherwise the index is not ordered */ | ||
124 | p[self] = p[self] + 1; | ||
125 | p[self+p[self]*np] = self; | ||
126 | |||
127 | /* j range */ | ||
128 | b1 = j; | ||
129 | b2 = j + r_j; | ||
130 | if (b2>=nc) { b2 = nc-1; } | ||
131 | |||
132 | /* i range */ | ||
133 | /*a1 = i - r_i; | ||
134 | if (a1<0) { a1 = 0; }*/ | ||
135 | a2 = i + r_i; | ||
136 | if (a2>=nr) { a2 = nr-1; } | ||
137 | |||
138 | /* number of more samples needed */ | ||
139 | nsamp = nb - p[self]; | ||
140 | /*if (nsamp<0) | ||
141 | {printf("nsamp=%d\n",nsamp);}*/ | ||
142 | k = 0; | ||
143 | t = b1; | ||
144 | s = i + 1; | ||
145 | if (s>a2) { | ||
146 | s = i; | ||
147 | t = t + 1; | ||
148 | } | ||
149 | |||
150 | |||
151 | while (k<nsamp && t<=b2) { | ||
152 | |||
153 | if (no_sample || (rand()<th_rand)) { | ||
154 | k = k + 1; | ||
155 | neighbor = s + t * nr; | ||
156 | p[self] = p[self] + 1; | ||
157 | p[self+p[self]*np] = neighbor; | ||
158 | p[neighbor] = p[neighbor] + 1; | ||
159 | p[neighbor+p[neighbor]*np] = self; | ||
160 | } | ||
161 | if (s!=i){ | ||
162 | s = s + 1; if (s>a2) { s = i; t = t + 1; | ||
163 | } | ||
164 | } | ||
165 | else {t=t+1;} | ||
166 | } /* k */ | ||
167 | total = total + p[self]; | ||
168 | } /* i */ | ||
169 | |||
170 | } /* j */ | ||
171 | |||
172 | /* i, j */ | ||
173 | |||
174 | out[0] = mxCreateNumericMatrix(total, 1, mxUINT32_CLASS, mxREAL); | ||
175 | out[1] = mxCreateNumericMatrix(np+1, 1, mxUINT32_CLASS, mxREAL); | ||
176 | qi = mxGetData(out[0]); | ||
177 | qj = mxGetData(out[1]); | ||
178 | |||
179 | if (out[0]==NULL || out[1]==NULL) { | ||
180 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
181 | } | ||
182 | |||
183 | total = 0; | ||
184 | for (j=0; j<np; j++) { | ||
185 | qj[j] = total; | ||
186 | s = j + np; | ||
187 | for (t=0; t<p[j]; t++) { | ||
188 | qi[total] = p[s]; | ||
189 | total = total + 1; | ||
190 | s = s + np; | ||
191 | } | ||
192 | } | ||
193 | qj[np] = total; | ||
194 | |||
195 | mxFree(p); | ||
196 | |||
197 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.dll b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.dll new file mode 100755 index 0000000..d66e578 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexa64 b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexa64 new file mode 100755 index 0000000..905aae5 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexglx b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexglx new file mode 100755 index 0000000..a7fc50d --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_cross.mexglx | |||
Binary files differ | |||
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 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.dll b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.dll new file mode 100755 index 0000000..a7cddfa --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexa64 b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexa64 new file mode 100755 index 0000000..d687d91 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexglx b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexglx new file mode 100755 index 0000000..25da17f --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/cimgnbmap_star.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/compileAll.m b/SD-VBS/common/toolbox/MultiNcut/compileAll.m new file mode 100755 index 0000000..16fe77b --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/compileAll.m | |||
@@ -0,0 +1,10 @@ | |||
1 | function compileAll(Cdir); | ||
2 | |||
3 | files = dir([Cdir,'/*.c']); | ||
4 | |||
5 | for j=1:length(files), | ||
6 | |||
7 | cm = sprintf('mex %s',files(j).name); | ||
8 | disp(cm); | ||
9 | eval(cm); | ||
10 | end | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m b/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m new file mode 100755 index 0000000..b2cb7ea --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m | |||
@@ -0,0 +1,245 @@ | |||
1 | %inputs : image, number of layers, distance defining the subgrid, the edge filter scales for each layer, radius for each layer, | ||
2 | %edge variance for filter, shape of the neighbourhood layout ('square', 'star', 'cross'), sigma for intensity affinity, | ||
3 | % sigma for distance influence in affinity, weight coefficients for Wpps in the multiscale matrix. | ||
4 | %output : multiscale affinity matrix , extern constraint matrix, affinity matrices of each layer seperatly. | ||
5 | |||
6 | |||
7 | function [multiWpp,constraintMat, Wind,data,emag,ephase]= computeMultiW (image,data); | ||
8 | |||
9 | %variables | ||
10 | |||
11 | if isempty(data.layers.number) | ||
12 | n=2; | ||
13 | else | ||
14 | n=data.layers.number; | ||
15 | end | ||
16 | |||
17 | if isempty(data.layers.dist) | ||
18 | dist_grid=3; | ||
19 | else | ||
20 | dist_grid=data.layers.dist; | ||
21 | end | ||
22 | |||
23 | if isempty(data.W.scales) | ||
24 | s=1:n; | ||
25 | elseif (length(data.W.scales)==n) | ||
26 | s=data.W.scales; | ||
27 | else | ||
28 | s=1:n; | ||
29 | end | ||
30 | |||
31 | if isempty(data.W.radius) | ||
32 | r(1)=2; | ||
33 | for i=2:n | ||
34 | r(i)=10; | ||
35 | end | ||
36 | else | ||
37 | r=data.W.radius; | ||
38 | end | ||
39 | |||
40 | |||
41 | if isempty(data.W.edgeVariance) | ||
42 | data.W.edgeVariance=0.1; | ||
43 | end | ||
44 | |||
45 | if isempty(data.W.gridtype) | ||
46 | data.W.gridtype='square'; | ||
47 | end | ||
48 | |||
49 | if isempty(data.W.sigmaI) | ||
50 | data.W.sigmaI=0.12; | ||
51 | end | ||
52 | |||
53 | if isempty(data.W.sigmaX) | ||
54 | data.W.sigmaX=10; | ||
55 | end | ||
56 | |||
57 | if isempty(data.layers.weight) | ||
58 | coef(1)=5; | ||
59 | coef(2:n)=200; | ||
60 | elseif (length(data.layers.weight)==n) | ||
61 | coef=data.layers.weight; | ||
62 | else | ||
63 | coef(1)=5; | ||
64 | coef(2:n)=100; %200 | ||
65 | end | ||
66 | |||
67 | if isempty(data.W.mode) | ||
68 | data.W.mode=mixed; | ||
69 | end | ||
70 | |||
71 | |||
72 | [p1,q1,ignore]=size(image); | ||
73 | image=image(:,:,1); | ||
74 | filter_par = [4,30,4]; %[9,30,4] | ||
75 | [x,y,gx,gy,par,threshold,emag,ephase,g,FIe,FIo,mago] = quadedgep2(image,filter_par,data,0.001); | ||
76 | minW=10^(-2); %-3 | ||
77 | |||
78 | |||
79 | % function [multiWpp,constraintMat,p,q,Wppp,subgrid] = computemultiWpp(n,imageX,r,dist_grid,s,dataWpp,emag,ephase,minW,mode,facteurMul,contrainte,tt,gridtype,colormode,imageOriginale,subgridImageReduite,pG,qG) | ||
80 | |||
81 | p= p1*ones(n,1); | ||
82 | q= q1*ones(n,1); | ||
83 | d= dist_grid*ones(n,1); | ||
84 | d(1)=1; | ||
85 | for (i=2:n) | ||
86 | d(i)=d(i)*3^(i-2); | ||
87 | end | ||
88 | p=ceil(p1./d); | ||
89 | q=ceil(q1./d); | ||
90 | |||
91 | %computation of the subgrids (the first pixel is coded by one). S{i,j}(k) is the index of | ||
92 | %the kth pixel of the jth grid in the ith grid. | ||
93 | |||
94 | for i=1:n-1 | ||
95 | for j=i+1:n | ||
96 | a=[0:p(j)*q(j)-1]; | ||
97 | subgrid{i,j}=p(i)*(floor(a/p(j)))*d(j)/d(i)+(1+mod(a,p(j))*d(j)/d(i)); | ||
98 | end | ||
99 | end | ||
100 | |||
101 | %computation of the independent W matrix for each layer Wind{i} 1=<i=<n. | ||
102 | |||
103 | [w1i,w1j]=cimgnbmap([p1,q1], r(1), 1); | ||
104 | |||
105 | if strcmp(data.W.mode,'mixed') | ||
106 | rMin = 0; | ||
107 | imageXX=double(image(:)); | ||
108 | sigmaI= (std(imageXX(:)) + 1e-10 )* data.W.sigmaI; | ||
109 | Wpp{1} = multiIntensityFirstLayer(double(image),w1i,w1j,rMin,data.W.sigmaX,sigmaI,minW); | ||
110 | Wpp2= affinityic(emag(:,:,s(1)),ephase(:,:,s(1)),w1i,w1j,max(max(emag(:,:,s(1)))) * data.W.edgeVariance); | ||
111 | Wpp{1} = sqrt(Wpp{1} .* Wpp2)+0.1*Wpp2; | ||
112 | |||
113 | elseif strcmp(data.W.mode,'notmixed') | ||
114 | Wpp{1}= affinityic(emag(:,:,s(1)),ephase(:,:,s(1)),w1i,w1j,max(max(emag(:,:,s(1)))) * data.W.edgeVariance); | ||
115 | |||
116 | elseif strcmp(data.W.mode,'intensity') | ||
117 | rMin = 0; | ||
118 | imageXX=double(image(:)); | ||
119 | sigmaI= (std(imageXX(:)) + 1e-10 )* data.W.sigmaI; | ||
120 | Wpp{1} = multiIntensityFirstLayer(double(image),w1i,w1j,rMin,data.W.sigmaX,sigmaI,minW); | ||
121 | |||
122 | end | ||
123 | Wpp{1}=coef(1)*(Wpp{1}+Wpp{1}')/2; | ||
124 | %Wpp{1}= coef(1)*Wpp{1}; | ||
125 | Wind{1}=Wpp{1}; | ||
126 | |||
127 | |||
128 | |||
129 | |||
130 | for i=2:n | ||
131 | if strcmp(data.W.gridtype,'square') | ||
132 | [wwi,wwj]=cimgnbmap([p(i),q(i)], r(i), 1); | ||
133 | elseif strcmp(data.W.gridtype,'star') | ||
134 | [wwi,wwj]=cimgnbmap_star([p(i),q(i)], r(i), 1); | ||
135 | elseif strcmp(data.W.gridtype,'cross') | ||
136 | [wwi,wwj]=cimgnbmap_cross([p(i),q(i)], r(i), 1); | ||
137 | end | ||
138 | wwi=double(wwi); | ||
139 | wiInOriginalImage=(p1*(floor(wwi/p(i)))*d(i))+(1+mod(wwi,p(i))*d(i)); | ||
140 | wiInOriginalImage=(p1*(floor(wwi/p(i)))*d(i))+(1+mod(wwi,p(i))*d(i)); | ||
141 | |||
142 | wiInOriginalImage= uint32(wiInOriginalImage); | ||
143 | |||
144 | if strcmp(data.W.mode,'mixed') | ||
145 | Wpp2= multiAffinityic(emag(:,:,i),ephase(:,:,i),wiInOriginalImage,wwj,subgrid{1,i},p(i),q(i),uint32(wwi),max(max(emag(:,:,i))) * data.W.edgeVariance); | ||
146 | a=floor(d(i)/d(i-1)); | ||
147 | if (mod(a,2)==0) | ||
148 | a=a+1; | ||
149 | end | ||
150 | % Wpp{i} = multiIntensityWppc(double(imageX),wiInOriginalImage,wwj,rMin,dataWpp.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),wi{i}); | ||
151 | |||
152 | Wpp{i} = multiIntensityWppc(double(image),wiInOriginalImage,wwj,rMin,data.W.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),uint32(wwi)); | ||
153 | Wpp{i} = sqrt(Wpp{i} .* Wpp2)+0.1*Wpp2; | ||
154 | elseif strcmp(data.W.mode,'notmixed') | ||
155 | Wpp{i}= multiAffinityic(emag(:,:,i),ephase(:,:,i),wiInOriginalImage,wwj,subgrid{1,i},p(i),q(i),uint32(wwi),max(max(emag(:,:,i))) * data.W.edgeVariance); | ||
156 | elseif strcmp(data.W.mode,'intensity') | ||
157 | Wpp{i} = multiIntensityWppc(double(image),wiInOriginalImage,wwj,rMin,data.W.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),uint32(wwi)); | ||
158 | end | ||
159 | Wpp{i}= coef(i)*(Wpp{i}+Wpp{i}')/2; | ||
160 | |||
161 | %Wpp{i}= coef(i)*Wpp{i}; | ||
162 | Wind{i}=Wpp{i}; | ||
163 | |||
164 | end | ||
165 | |||
166 | %computation of the intern contraint matrices C{i,j}. | ||
167 | |||
168 | for i=1:n-1 | ||
169 | r=floor(d(i+1)/(d(i)*2)); | ||
170 | [wwi,wwj]=cimgnbmap([p(i),q(i)], r, 1); | ||
171 | wi{i}=wwi; | ||
172 | wj{i}=wwj; | ||
173 | end | ||
174 | |||
175 | for i=1:n-1 | ||
176 | for j=i+1:n | ||
177 | C{i,j}=sparse(p(i)*q(i),p(j)*q(j)); | ||
178 | firstPointer=double(wj{i}(subgrid{i,j}))+1; | ||
179 | lastPointer=double(wj{i}(subgrid{i,j}+1)); | ||
180 | invNbNeighbours=1./(lastPointer-firstPointer+1); | ||
181 | for (k=1:p(j)*q(j)) | ||
182 | for (m=firstPointer(k):lastPointer(k)) | ||
183 | C{i,j}(double(wi{i}(m))+1,k)=invNbNeighbours(k); | ||
184 | end | ||
185 | end | ||
186 | end | ||
187 | end | ||
188 | |||
189 | %Assembling the built matrices to make up multiWpp. | ||
190 | for i=1:n | ||
191 | if (i>1) | ||
192 | for j=i-1:-1:1 | ||
193 | Wpp{i}=[C{j,i}',Wpp{i}]; | ||
194 | end | ||
195 | end | ||
196 | if (i<n) | ||
197 | for j=i+1:n | ||
198 | Wpp{i}=[Wpp{i},C{i,j}]; | ||
199 | end | ||
200 | end | ||
201 | end | ||
202 | |||
203 | % %Assembling the built matrices to make up Wpp without intern constrains. | ||
204 | % for i=1:n | ||
205 | % if (i>1) | ||
206 | % for j=i-1:-1:1 | ||
207 | % Wpp{i}=[sparse(p(i)*q(i),p(j)*q(j)),Wpp{i}]; | ||
208 | % end | ||
209 | % end | ||
210 | % if (i<n) | ||
211 | % for j=i+1:n | ||
212 | % Wpp{i}=[Wpp{i},sparse(p(i)*q(i),p(j)*q(j))]; | ||
213 | % end | ||
214 | % end | ||
215 | % end | ||
216 | |||
217 | clear Wind;Wind = 1; | ||
218 | |||
219 | multiWpp=Wpp{1}; clear Wpp{1} | ||
220 | for i=2:n | ||
221 | multiWpp=[multiWpp;Wpp{i}];clear Wpp{i} | ||
222 | end | ||
223 | |||
224 | |||
225 | % Computing the average extern constraint | ||
226 | |||
227 | pq=sum(p(2:n).*q(2:n)); | ||
228 | p2q2=p(2)*q(2); | ||
229 | constraintMat=[-C{1,2};speye(p2q2);sparse(pq-p2q2,p2q2)]; | ||
230 | if n>2 | ||
231 | for i=3:n | ||
232 | piqi=p(i)*q(i); | ||
233 | if i~=n | ||
234 | constraintMat=[constraintMat,[sparse(sum(p(1:i-2).*q(1:i-2)),piqi);-C{i-1,i};speye(piqi);sparse(pq-sum(p(2:i).*q(2:i)),piqi)]]; | ||
235 | else | ||
236 | constraintMat=[constraintMat,[sparse(sum(p(1:i-2).*q(1:i-2)),piqi);-C{i-1,i};speye(piqi)]]; | ||
237 | end | ||
238 | end | ||
239 | end | ||
240 | |||
241 | % saving useful information | ||
242 | %subgrids, p and q | ||
243 | data.subgrid=subgrid; | ||
244 | data.p=p; | ||
245 | data.q=q; | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/discretisation.m b/SD-VBS/common/toolbox/MultiNcut/discretisation.m new file mode 100755 index 0000000..70b5650 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/discretisation.m | |||
@@ -0,0 +1,49 @@ | |||
1 | function [SegLabel,EigenVectors]=discretisation(EigenVectors,nr,nc) | ||
2 | % | ||
3 | % EigenvectorsDiscrete=discretisation(EigenVectors) | ||
4 | % | ||
5 | % Input: EigenVectors = continuous Ncut vector, size = ndata x nbEigenvectors | ||
6 | % Output EigenvectorsDiscrete = discrete Ncut vector, size = ndata x nbEigenvectors | ||
7 | % | ||
8 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004 | ||
9 | |||
10 | [n,k]=size(EigenVectors); | ||
11 | |||
12 | vm = sqrt(sum(EigenVectors.*EigenVectors,2)); | ||
13 | EigenVectors = EigenVectors./repmat(vm,1,k); | ||
14 | |||
15 | R=zeros(k); | ||
16 | R(:,1)=EigenVectors(1+round(rand(1)*(n-1)),:)'; | ||
17 | c=zeros(n,1); | ||
18 | for j=2:k | ||
19 | c=c+abs(EigenVectors*R(:,j-1)); | ||
20 | [minimum,i]=min(c); | ||
21 | R(:,j)=EigenVectors(i,:)'; | ||
22 | end | ||
23 | |||
24 | lastObjectiveValue=0; | ||
25 | exitLoop=0; | ||
26 | nbIterationsDiscretisation = 0; | ||
27 | nbIterationsDiscretisationMax = 20;%voir | ||
28 | while exitLoop== 0 | ||
29 | nbIterationsDiscretisation = nbIterationsDiscretisation + 1 ; | ||
30 | EigenvectorsDiscrete = discretisationEigenVectorData(EigenVectors*R); | ||
31 | [U,S,V] = svd(EigenvectorsDiscrete'*EigenVectors,0); | ||
32 | NcutValue=2*(n-trace(S)); | ||
33 | |||
34 | if abs(NcutValue-lastObjectiveValue) < eps | nbIterationsDiscretisation > nbIterationsDiscretisationMax | ||
35 | exitLoop=1; | ||
36 | else | ||
37 | lastObjectiveValue = NcutValue; | ||
38 | R=V*U'; | ||
39 | end | ||
40 | end | ||
41 | |||
42 | %%%% | ||
43 | |||
44 | SegLabel = zeros(nr,nc); | ||
45 | for j=1:size(EigenvectorsDiscrete,2), | ||
46 | SegLabel = SegLabel + j*reshape(EigenvectorsDiscrete(:,j),nr,nc); | ||
47 | end | ||
48 | EigenVectors = reshape(EigenVectors,nr,nc,size(EigenVectors,2)); | ||
49 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/discretisationEigenVectorData.m b/SD-VBS/common/toolbox/MultiNcut/discretisationEigenVectorData.m new file mode 100755 index 0000000..4626e3d --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/discretisationEigenVectorData.m | |||
@@ -0,0 +1,12 @@ | |||
1 | function Y = discretisationEigenVectorData(EigenVector) | ||
2 | % Y = discretisationEigenVectorData(EigenVector) | ||
3 | % | ||
4 | % discretizes previously rotated eigenvectors in discretisation | ||
5 | % Timothee Cour, Stella Yu, Jianbo Shi, 2004 | ||
6 | |||
7 | [n,k]=size(EigenVector); | ||
8 | |||
9 | |||
10 | [Maximum,J]=max(EigenVector'); | ||
11 | |||
12 | Y=sparse(1:n,J',1,n,k); | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/doog1.m b/SD-VBS/common/toolbox/MultiNcut/doog1.m new file mode 100755 index 0000000..dd8e87b --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/doog1.m | |||
@@ -0,0 +1,32 @@ | |||
1 | function H=doog1(sig,r,th,N); | ||
2 | % H=doog1(sig,r,th,N); | ||
3 | |||
4 | |||
5 | % by Serge Belongie | ||
6 | |||
7 | no_pts=N; % no. of points in x,y grid | ||
8 | |||
9 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
10 | |||
11 | phi=pi*th/180; | ||
12 | sigy=sig; | ||
13 | sigx=r*sig; | ||
14 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
15 | C=R*diag([sigx,sigy])*R'; | ||
16 | |||
17 | X=[x(:) y(:)]; | ||
18 | |||
19 | Gb=gaussian(X,[0 0]',C); | ||
20 | Gb=reshape(Gb,N,N); | ||
21 | |||
22 | m=R*[0 sig]'; | ||
23 | |||
24 | a=1; | ||
25 | b=-1; | ||
26 | |||
27 | % make odd-symmetric filter | ||
28 | Ga=gaussian(X,m/2,C); | ||
29 | Ga=reshape(Ga,N,N); | ||
30 | Gb=rot90(Ga,2); | ||
31 | H=a*Ga+b*Gb; | ||
32 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/doog2.m b/SD-VBS/common/toolbox/MultiNcut/doog2.m new file mode 100755 index 0000000..a0511cb --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/doog2.m | |||
@@ -0,0 +1,38 @@ | |||
1 | function G=doog2(sig,r,th,N); | ||
2 | % G=doog2(sig,r,th,N); | ||
3 | % Make difference of offset gaussians kernel | ||
4 | % theta is in degrees | ||
5 | % (see Malik & Perona, J. Opt. Soc. Amer., 1990) | ||
6 | % | ||
7 | % Example: | ||
8 | % >> imagesc(doog2(1,12,0,64,1)) | ||
9 | % >> colormap(gray) | ||
10 | |||
11 | % by Serge Belongie | ||
12 | |||
13 | no_pts=N; % no. of points in x,y grid | ||
14 | |||
15 | [x,y]=meshgrid(-(N/2)+1/2:(N/2)-1/2,-(N/2)+1/2:(N/2)-1/2); | ||
16 | |||
17 | phi=pi*th/180; | ||
18 | sigy=sig; | ||
19 | sigx=r*sig; | ||
20 | R=[cos(phi) -sin(phi); sin(phi) cos(phi)]; | ||
21 | C=R*diag([sigx,sigy])*R'; | ||
22 | |||
23 | X=[x(:) y(:)]; | ||
24 | |||
25 | Gb=gaussian(X,[0 0]',C); | ||
26 | Gb=reshape(Gb,N,N); | ||
27 | |||
28 | m=R*[0 sig]'; | ||
29 | Ga=gaussian(X,m,C); | ||
30 | Ga=reshape(Ga,N,N); | ||
31 | Gc=rot90(Ga,2); | ||
32 | |||
33 | a=-1; | ||
34 | b=2; | ||
35 | c=-1; | ||
36 | |||
37 | G = a*Ga + b*Gb + c*Gc; | ||
38 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/eigSolve.m b/SD-VBS/common/toolbox/MultiNcut/eigSolve.m new file mode 100755 index 0000000..cfb64fc --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/eigSolve.m | |||
@@ -0,0 +1,5 @@ | |||
1 | function [eigenVectors,s]= eigSolve (multiWpp,ConstraintMat,data) | ||
2 | |||
3 | [v,s] = quickNcutHardBiais2(multiWpp,ConstraintMat,data.dataGraphCut.nbEigenValues,data.dataGraphCut); | ||
4 | v=v(1:data.p(1)*data.q(1),:); | ||
5 | eigenVectors=reshape(v,data.p(1),data.q(1),data.dataGraphCut.nbEigenValues); | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/fft_filt_2.m b/SD-VBS/common/toolbox/MultiNcut/fft_filt_2.m new file mode 100755 index 0000000..9c84e96 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/fft_filt_2.m | |||
@@ -0,0 +1,29 @@ | |||
1 | function FI=fft_filt_2(V,FB,sf); | ||
2 | % FI=fft_filt_2(V,FB,sf); | ||
3 | % fft-based filtering | ||
4 | % requires image to be called "V" | ||
5 | % and filters to be in FB | ||
6 | % sf is the subsampling factor | ||
7 | % | ||
8 | % FI is the result | ||
9 | |||
10 | [M1,M2,N3]=size(FB); | ||
11 | % prepare FFT of image for filtering | ||
12 | [N1,N2]=size(V); | ||
13 | I=zeros(size(V)+[M1-1 M2-1]); | ||
14 | I(1:N1,1:N2)=V; | ||
15 | N1s=length(1:sf:N1); | ||
16 | N2s=length(1:sf:N2); | ||
17 | IF=fft2(I); | ||
18 | FI=zeros(N1s,N2s,N3); | ||
19 | |||
20 | % apply filters | ||
21 | for n=1:N3; | ||
22 | f=rot90(FB(:,:,n),2); | ||
23 | fF=fft2(f,N1+M1-1,N2+M2-1); | ||
24 | IfF=IF.*fF; | ||
25 | If=real(ifft2(IfF)); | ||
26 | If=If(ceil((M1+1)/2):ceil((M1+1)/2)+N1-1,ceil((M2+1)/2):ceil((M2+1)/2)+N2-1); | ||
27 | FI(:,:,n)=If(1:sf:N1,1:sf:N2); | ||
28 | end | ||
29 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/gaussian.m b/SD-VBS/common/toolbox/MultiNcut/gaussian.m new file mode 100755 index 0000000..509b129 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/gaussian.m | |||
@@ -0,0 +1,31 @@ | |||
1 | function p=gaussian(x,m,C); | ||
2 | % p=gaussian(x,m,C); | ||
3 | % | ||
4 | % Evaluate the multi-variate density with mean vector m and covariance | ||
5 | % matrix C for the input vector x. | ||
6 | % | ||
7 | % p=gaussian(X,m,C); | ||
8 | % | ||
9 | % Vectorized version: Here X is a matrix of column vectors, and p is | ||
10 | % a vector of probabilities for each vector. | ||
11 | |||
12 | d=length(m); | ||
13 | |||
14 | if size(x,1)~=d | ||
15 | x=x'; | ||
16 | end | ||
17 | N=size(x,2); | ||
18 | |||
19 | detC = det(C); | ||
20 | if rcond(C)<eps | ||
21 | % fprintf(1,'Covariance matrix close to singular. (gaussian.m)\n'); | ||
22 | p = zeros(N,1); | ||
23 | else | ||
24 | m=m(:); | ||
25 | M=m*ones(1,N); | ||
26 | denom=(2*pi)^(d/2)*sqrt(abs(detC)); | ||
27 | mahal=sum(((x-M)'*inv(C)).*(x-M)',2); % Chris Bregler's trick | ||
28 | numer=exp(-0.5*mahal); | ||
29 | p=numer/denom; | ||
30 | end | ||
31 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/make_filterbank_even2.m b/SD-VBS/common/toolbox/MultiNcut/make_filterbank_even2.m new file mode 100755 index 0000000..f7f4527 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/make_filterbank_even2.m | |||
@@ -0,0 +1,45 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = 2*enlong; | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | % elongated filter set | ||
27 | counter = 1; | ||
28 | |||
29 | for m=1:num_scale | ||
30 | for n=1:num_ori | ||
31 | % r=12 here is equivalent to Malik's r=3; | ||
32 | f=doog2(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
33 | FB(:,:,n,m)=f; | ||
34 | end | ||
35 | end | ||
36 | |||
37 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
38 | total_num_filt=size(FB,3); | ||
39 | |||
40 | for j=1:total_num_filt, | ||
41 | F = FB(:,:,j); | ||
42 | a = sum(sum(abs(F))); | ||
43 | FB(:,:,j) = FB(:,:,j)/a; | ||
44 | end | ||
45 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/make_filterbank_odd2.m b/SD-VBS/common/toolbox/MultiNcut/make_filterbank_odd2.m new file mode 100755 index 0000000..0059dca --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/make_filterbank_odd2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | function FB = make_filterbank(num_ori,filter_scales,wsz,enlong) | ||
2 | % | ||
3 | % F = make_filterbank(num_ori,num_scale,wsz) | ||
4 | % | ||
5 | |||
6 | if nargin<4, | ||
7 | enlong = 3; | ||
8 | end | ||
9 | |||
10 | enlong = enlong*2; | ||
11 | |||
12 | % definine filterbank | ||
13 | %num_ori=6; | ||
14 | %num_scale=3; | ||
15 | |||
16 | num_scale = length(filter_scales); | ||
17 | |||
18 | M1=wsz; % size in pixels | ||
19 | M2=M1; | ||
20 | |||
21 | ori_incr=180/num_ori; | ||
22 | ori_offset=ori_incr/2; % helps with equalizing quantiz. error across filter set | ||
23 | |||
24 | FB=zeros(M1,M2,num_ori,num_scale); | ||
25 | |||
26 | |||
27 | % elongated filter set | ||
28 | counter = 1; | ||
29 | |||
30 | for m=1:num_scale | ||
31 | for n=1:num_ori | ||
32 | % r=12 here is equivalent to Malik's r=3; | ||
33 | f=doog1(filter_scales(m),enlong,ori_offset+(n-1)*ori_incr,M1); | ||
34 | FB(:,:,n,m)=f; | ||
35 | end | ||
36 | end | ||
37 | |||
38 | FB=reshape(FB,M1,M2,num_scale*num_ori); | ||
39 | total_num_filt=size(FB,3); | ||
40 | |||
41 | for j=1:total_num_filt, | ||
42 | F = FB(:,:,j); | ||
43 | a = sum(sum(abs(F))); | ||
44 | FB(:,:,j) = FB(:,:,j)/a; | ||
45 | end | ||
46 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.c b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.c new file mode 100755 index 0000000..5ac4820 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.c | |||
@@ -0,0 +1,82 @@ | |||
1 | /*================================================================ | ||
2 | * mex_projection_QR.c = used by quickNcutHardBiais.m in eigensolver. | ||
3 | * mex_projection_QR(X,P,Ubar,R) == (eye(length(P))-Ubar*(R'*R)^{-1}*Ubar') * P * (eye(length(P))-Ubar*(R'*R)^{-1}*Ubar') * X ; | ||
4 | * (R'*R)^{-1} is solved by solving a triangular system | ||
5 | |||
6 | * P, Ubar, R are sparse, but X is full | ||
7 | |||
8 | * R is upper triangular | ||
9 | |||
10 | % test sequence: | ||
11 | |||
12 | *=================================================================*/ | ||
13 | |||
14 | # include "math.h" | ||
15 | # include "mex.h" | ||
16 | # include "a_times_b_cmplx.c" | ||
17 | /*# include "a_times_b.c"*/ | ||
18 | |||
19 | void mexFunction( | ||
20 | int nargout, | ||
21 | mxArray *out[], | ||
22 | int nargin, | ||
23 | const mxArray *in[] | ||
24 | ) | ||
25 | { | ||
26 | int *ir[4], *jc[4], m[4], n[4]; | ||
27 | double *y, *y1,*y2,*y3,*y4,*y5,*y6, *pr[4]; | ||
28 | double *y2bis, *y5bis; | ||
29 | int k; | ||
30 | |||
31 | for (k=0; k<4; k++) { | ||
32 | m[k] = mxGetM(in[k]); | ||
33 | n[k] = mxGetN(in[k]); | ||
34 | pr[k] = mxGetPr(in[k]); | ||
35 | if (k>0) { | ||
36 | ir[k] = mxGetIr(in[k]); | ||
37 | jc[k] = mxGetJc(in[k]); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | out[0] = mxCreateDoubleMatrix(m[1],1,mxREAL); | ||
42 | y = mxGetPr(out[0]); | ||
43 | |||
44 | |||
45 | y1 = mxCalloc(n[2], sizeof(double)); | ||
46 | y2 = mxCalloc(m[3], sizeof(double)); | ||
47 | y2bis = mxCalloc(m[3], sizeof(double)); | ||
48 | y3 = mxCalloc(m[1], sizeof(double)); | ||
49 | y4 = mxCalloc(m[1], sizeof(double)); | ||
50 | y5 = mxCalloc(n[2], sizeof(double)); | ||
51 | y5bis = mxCalloc(n[2], sizeof(double)); | ||
52 | y6 = mxCalloc(n[2], sizeof(double)); | ||
53 | |||
54 | CSR_VecMult_CAB_double(m[2],n[2],pr[2],ir[2],jc[2],pr[0],y1); | ||
55 | CSR_VecTriangSlvLD_CAB_double(m[3],pr[3],ir[3],jc[3],y1,y2bis); | ||
56 | CSC_VecTriangSlvUD_CAB_double(m[3],pr[3],ir[3],jc[3],y2bis,y2); | ||
57 | for(k=0;k<m[1];k++) { | ||
58 | y3[k]=pr[0][k]; | ||
59 | } | ||
60 | CSC_VecMult_CaABC_double(m[2],n[2],-1,pr[2],ir[2],jc[2],y2,y3); | ||
61 | |||
62 | CSR_VecMult_CAB_double(m[1],n[1],pr[1],ir[1],jc[1],y3,y4); | ||
63 | |||
64 | CSR_VecMult_CAB_double(m[2],n[2],pr[2],ir[2],jc[2],y4,y5); | ||
65 | |||
66 | CSR_VecTriangSlvLD_CAB_double(m[3],pr[3],ir[3],jc[3],y5,y5bis); | ||
67 | CSC_VecTriangSlvUD_CAB_double(m[3],pr[3],ir[3],jc[3],y5bis,y6); | ||
68 | for(k=0;k<m[1];k++) { | ||
69 | y[k]=y4[k]; | ||
70 | } | ||
71 | CSC_VecMult_CaABC_double(m[2],n[2],-1,pr[2],ir[2],jc[2],y6,y); | ||
72 | |||
73 | |||
74 | mxFree(y1); | ||
75 | mxFree(y2); | ||
76 | mxFree(y2bis); | ||
77 | mxFree(y3); | ||
78 | mxFree(y4); | ||
79 | mxFree(y5); | ||
80 | mxFree(y5bis); | ||
81 | mxFree(y6); | ||
82 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.dll b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.dll new file mode 100755 index 0000000..d77f509 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexa64 b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexa64 new file mode 100755 index 0000000..bb7b7bd --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexglx b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexglx new file mode 100755 index 0000000..dd63169 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.c b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.c new file mode 100755 index 0000000..5915959 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /*================================================================ | ||
2 | * mex_projection_QR_symmetric.c = used by quickNcutHardBiais.m in eigensolver. | ||
3 | * mex_projection_QR_symmetric(X,P,Ubar,R) == (eye(length(P))-Ubar*(R'*R)^{-1}*Ubar') * P * (eye(length(P))-Ubar*(R'*R)^{-1}*Ubar') * X ; | ||
4 | * (R'*R)^{-1} is solved by solving a triangular system | ||
5 | |||
6 | * P, Ubar, R are sparse, but X is full | ||
7 | |||
8 | * R is upper triangular | ||
9 | * P is symmetric | ||
10 | |||
11 | % test sequence: | ||
12 | |||
13 | *=================================================================*/ | ||
14 | |||
15 | # include "math.h" | ||
16 | # include "mex.h" | ||
17 | # include "a_times_b_cmplx.c" | ||
18 | /*# include "a_times_b.c"*/ | ||
19 | |||
20 | void mexFunction( | ||
21 | int nargout, | ||
22 | mxArray *out[], | ||
23 | int nargin, | ||
24 | const mxArray *in[] | ||
25 | ) | ||
26 | { | ||
27 | int *ir[4], *jc[4], m[4], n[4]; | ||
28 | double *y, *y1,*y2,*y3,*y4,*y5,*y6, *pr[4]; | ||
29 | double *y2bis, *y5bis; | ||
30 | int k; | ||
31 | |||
32 | for (k=0; k<4; k++) { | ||
33 | m[k] = mxGetM(in[k]); | ||
34 | n[k] = mxGetN(in[k]); | ||
35 | pr[k] = mxGetPr(in[k]); | ||
36 | if (k>0) { | ||
37 | ir[k] = mxGetIr(in[k]); | ||
38 | jc[k] = mxGetJc(in[k]); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | out[0] = mxCreateDoubleMatrix(m[1],1,mxREAL); | ||
43 | y = mxGetPr(out[0]); | ||
44 | |||
45 | |||
46 | y1 = mxCalloc(n[2], sizeof(double)); | ||
47 | y2 = mxCalloc(m[3], sizeof(double)); | ||
48 | y2bis = mxCalloc(m[3], sizeof(double)); | ||
49 | y3 = mxCalloc(m[1], sizeof(double)); | ||
50 | y4 = mxCalloc(m[1], sizeof(double)); | ||
51 | y5 = mxCalloc(n[2], sizeof(double)); | ||
52 | y5bis = mxCalloc(n[2], sizeof(double)); | ||
53 | y6 = mxCalloc(n[2], sizeof(double)); | ||
54 | |||
55 | CSR_VecMult_CAB_double(m[2],n[2],pr[2],ir[2],jc[2],pr[0],y1); | ||
56 | CSR_VecTriangSlvLD_CAB_double(m[3],pr[3],ir[3],jc[3],y1,y2bis); | ||
57 | CSC_VecTriangSlvUD_CAB_double(m[3],pr[3],ir[3],jc[3],y2bis,y2); | ||
58 | for(k=0;k<m[1];k++) { | ||
59 | y3[k]=pr[0][k]; | ||
60 | } | ||
61 | CSC_VecMult_CaABC_double(m[2],n[2],-1,pr[2],ir[2],jc[2],y2,y3); | ||
62 | |||
63 | CSRsymm_VecMult_CAB_double(m[1],n[1],pr[1],ir[1],jc[1],y3,y4); | ||
64 | |||
65 | CSR_VecMult_CAB_double(m[2],n[2],pr[2],ir[2],jc[2],y4,y5); | ||
66 | |||
67 | CSR_VecTriangSlvLD_CAB_double(m[3],pr[3],ir[3],jc[3],y5,y5bis); | ||
68 | CSC_VecTriangSlvUD_CAB_double(m[3],pr[3],ir[3],jc[3],y5bis,y6); | ||
69 | for(k=0;k<m[1];k++) { | ||
70 | y[k]=y4[k]; | ||
71 | } | ||
72 | CSC_VecMult_CaABC_double(m[2],n[2],-1,pr[2],ir[2],jc[2],y6,y); | ||
73 | |||
74 | |||
75 | mxFree(y1); | ||
76 | mxFree(y2); | ||
77 | mxFree(y2bis); | ||
78 | mxFree(y3); | ||
79 | mxFree(y4); | ||
80 | mxFree(y5); | ||
81 | mxFree(y5bis); | ||
82 | mxFree(y6); | ||
83 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.dll b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.dll new file mode 100755 index 0000000..0270d17 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexa64 b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexa64 new file mode 100755 index 0000000..201f6e3 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexglx b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexglx new file mode 100755 index 0000000..5b80e13 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_projection_QR_symmetric.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexglx b/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexglx new file mode 100755 index 0000000..bfad956 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexmac b/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexmac new file mode 100755 index 0000000..54be540 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/mex_w_times_x_symmetric.mexmac | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.c b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.c new file mode 100755 index 0000000..6919db9 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.c | |||
@@ -0,0 +1,216 @@ | |||
1 | /*================================================================ | ||
2 | * function w = affinityic(emag,ephase,pi,pj,subgrid,nrSubgrid,ncSubgrid,subpi,sigma) | ||
3 | * Input: | ||
4 | * emag = edge strength at each pixel | ||
5 | * ephase = edge phase at each pixel | ||
6 | * [pi,pj] = index pair representation for MALTAB sparse matrices, pi index in original grid, size(pj)=nrW*ncW+1 | ||
7 | * subgrid= index of the subgrid in the original image (first pixel's index is one!) | ||
8 | * [nrSubgrid,ncSubgrid]= number of rows et colums of the subgrid | ||
9 | * subpi = pi but with index in subgrid | ||
10 | * sigma = sigma for IC energy | ||
11 | * Output: | ||
12 | * w = affinity with IC at [pi,pj] | ||
13 | * | ||
14 | |||
15 | % test sequence | ||
16 | f = synimg(10); | ||
17 | [i,j] = cimgnbmap(size(f),2); | ||
18 | [ex,ey,egx,egy] = quadedgep(f); | ||
19 | a = affinityic(ex,ey,egx,egy,i,j) | ||
20 | show_dist_w(f,a); | ||
21 | |||
22 | * Stella X. Yu, Nov 19, 2001. | ||
23 | *=================================================================*/ | ||
24 | |||
25 | # include "mex.h" | ||
26 | # include "math.h" | ||
27 | |||
28 | void mexFunction( | ||
29 | int nargout, | ||
30 | mxArray *out[], | ||
31 | int nargin, | ||
32 | const mxArray *in[] | ||
33 | ) | ||
34 | { | ||
35 | /* declare variables */ | ||
36 | int nr, nc, np, nW, total; | ||
37 | int i, j, k, t, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step,nrSubgrid, ncSubgrid; | ||
38 | double sigma, di, dj, a, z, maxori, phase1, phase2, slope; | ||
39 | int *ir, *jc; | ||
40 | /* unsigned long *pi, *pj, *subpi; */ | ||
41 | unsigned int *pi, *pj, *subpi; | ||
42 | double *emag, *ephase, *w,*tmp,*subgrid; | ||
43 | |||
44 | |||
45 | /* check argument */ | ||
46 | if (nargin<8) { | ||
47 | mexErrMsgTxt("Eight input arguments required"); | ||
48 | } | ||
49 | if (nargout>1) { | ||
50 | mexErrMsgTxt("Too many output arguments"); | ||
51 | } | ||
52 | |||
53 | /* get edgel information */ | ||
54 | nr = mxGetM(in[0]); | ||
55 | nc = mxGetN(in[0]); | ||
56 | if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) { | ||
57 | mexErrMsgTxt("Edge magnitude and phase shall be of the same image size"); | ||
58 | } | ||
59 | emag = mxGetPr(in[0]); | ||
60 | ephase = mxGetPr(in[1]); | ||
61 | np = nr * nc; | ||
62 | |||
63 | /*get subgrid information*/ | ||
64 | |||
65 | tmp = mxGetData(in[5]); | ||
66 | nrSubgrid = (int)tmp[0]; | ||
67 | tmp = mxGetData(in[6]); | ||
68 | ncSubgrid = (int)tmp[0]; | ||
69 | |||
70 | /* printf("nrSubgrid=%d\n",nrSubgrid); | ||
71 | printf("ncSubgrid=%d\n",ncSubgrid); */ | ||
72 | if (nrSubgrid* ncSubgrid != mxGetM(in[4])*mxGetN(in[4])) { | ||
73 | mexErrMsgTxt("Error in the size of the subgrid"); | ||
74 | } | ||
75 | subgrid = mxGetData(in[4]); | ||
76 | nW = nrSubgrid * ncSubgrid; | ||
77 | |||
78 | /* get new index pair */ | ||
79 | if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) { | ||
80 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
81 | } | ||
82 | if (mxGetM(in[3]) * mxGetN(in[3]) != nW + 1) { | ||
83 | mexErrMsgTxt("Wrong index representation"); | ||
84 | } | ||
85 | pi = mxGetData(in[2]); | ||
86 | pj = mxGetData(in[3]); | ||
87 | subpi = mxGetData(in[7]); | ||
88 | /*{printf("pi[50] = %d\n",pi[50]);} | ||
89 | {printf("subpi[5] = %d\n",subpi[5]);} | ||
90 | {printf("subpi[6] = %d\n",subpi[6]);} | ||
91 | {printf("subpi[4] = %d\n",subpi[4]);}*/ | ||
92 | |||
93 | /* create output */ /*!!!!!!!!!!!!!!!!!!!!!!!changer taille output!!!!!!!!!!*/ | ||
94 | out[0] = mxCreateSparse(nW,nW,pj[nW],mxREAL); | ||
95 | if (out[0]==NULL) { | ||
96 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
97 | } | ||
98 | w = mxGetPr(out[0]); | ||
99 | ir = mxGetIr(out[0]); | ||
100 | jc = mxGetJc(out[0]); | ||
101 | |||
102 | /* find my sigma */ | ||
103 | if (nargin<9) { | ||
104 | sigma = 0; | ||
105 | for (k=0; k<np; k++) { | ||
106 | if (emag[k]>sigma) { sigma = emag[k]; } | ||
107 | } | ||
108 | sigma = sigma / 6; | ||
109 | printf("sigma = %6.5f",sigma); | ||
110 | } else { | ||
111 | sigma = mxGetScalar(in[8]); | ||
112 | } | ||
113 | a = 0.5 / (sigma * sigma); | ||
114 | |||
115 | /* computation */ | ||
116 | total = 0; | ||
117 | |||
118 | for (j=0; j<nW; j++){ | ||
119 | t= (int)subgrid[j]-1; /*on parcourt tous les pixels de la sous-grille dans la grille d'origine*/ | ||
120 | |||
121 | jc[j] = total; /* total represente le nombre voisins du pixel j*/ | ||
122 | jx = t / nr; /* col */ | ||
123 | jy = t % nr; /* row */ | ||
124 | |||
125 | for (k=pj[j]; k<pj[j+1]; k++) { /*k represente les indices correspondant au pixel j dans pi*/ | ||
126 | |||
127 | i = pi[k]-1; /*i est un voisin de j a considerer*/ | ||
128 | |||
129 | if (i==j) { | ||
130 | maxori = 1; | ||
131 | |||
132 | } else { | ||
133 | |||
134 | ix = i / nr; | ||
135 | iy = i % nr; | ||
136 | |||
137 | /* scan */ | ||
138 | di = (double) (iy - jy); | ||
139 | dj = (double) (ix - jx); | ||
140 | |||
141 | maxori = 0.; | ||
142 | phase1 = ephase[j]; | ||
143 | |||
144 | |||
145 | /* sample in i direction */ | ||
146 | if (abs(di) >= abs(dj)) { | ||
147 | slope = dj / di; | ||
148 | step = (iy>=jy) ? 1 : -1; | ||
149 | |||
150 | iip1 = jy; | ||
151 | jjp1 = jx; | ||
152 | |||
153 | |||
154 | for (ii=0;ii<abs(di);ii++){ | ||
155 | iip2 = iip1 + step; | ||
156 | jjp2 = (int)(0.5 + slope*(iip2-jy) + jx); | ||
157 | |||
158 | phase2 = ephase[iip2+jjp2*nr]; | ||
159 | |||
160 | if (phase1 != phase2) { | ||
161 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
162 | if (z > maxori){ | ||
163 | maxori = z; | ||
164 | } | ||
165 | } | ||
166 | |||
167 | iip1 = iip2; | ||
168 | jjp1 = jjp2; | ||
169 | phase1 = phase2; | ||
170 | } | ||
171 | |||
172 | /* sample in j direction */ | ||
173 | } else { | ||
174 | slope = di / dj; | ||
175 | step = (ix>=jx) ? 1: -1; | ||
176 | |||
177 | jjp1 = jx; | ||
178 | iip1 = jy; | ||
179 | |||
180 | |||
181 | for (jj=0;jj<abs(dj);jj++){ | ||
182 | jjp2 = jjp1 + step; | ||
183 | iip2 = (int)(0.5+ slope*(jjp2-jx) + jy); | ||
184 | |||
185 | phase2 = ephase[iip2+jjp2*nr]; | ||
186 | |||
187 | if (phase1 != phase2){ | ||
188 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
189 | if (z > maxori){ | ||
190 | maxori = z; | ||
191 | } | ||
192 | |||
193 | } | ||
194 | |||
195 | iip1 = iip2; | ||
196 | jjp1 = jjp2; | ||
197 | phase1 = phase2; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | maxori = 0.5 * maxori; | ||
202 | maxori = exp(-maxori * maxori * a); | ||
203 | } | ||
204 | /*if (total<20) {printf("subpi[k] = %d\n",subpi[k]);}*/ | ||
205 | |||
206 | ir[total] = (int)subpi[k]; | ||
207 | /*if (total<20) {printf("ir[total] = %d\n",ir[total]);}*/ | ||
208 | w[total] = maxori; | ||
209 | total = total + 1; | ||
210 | |||
211 | } /* i */ | ||
212 | } /*j*/ | ||
213 | /*printf("total = %d\n",total);*/ | ||
214 | /*printf("ir[100] = %d\n",ir[100]);*/ | ||
215 | jc[nW] = total; | ||
216 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.dll b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.dll new file mode 100755 index 0000000..f9b6bc9 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexa64 b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexa64 new file mode 100755 index 0000000..9a2a3cc --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexglx b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexglx new file mode 100755 index 0000000..ec11a0e --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.c b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.c new file mode 100755 index 0000000..0cae523 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.c | |||
@@ -0,0 +1,126 @@ | |||
1 | /*================================================================ | ||
2 | * function w = intensityWppc(image,pi,pj,rMin,sigmaX,sigmaIntensite,valeurMinW ) | ||
3 | * Input: | ||
4 | * [pi,pj] = index pair representation for MALTAB sparse matrices | ||
5 | * Output: | ||
6 | * w = affinity with IC at [pi,pj] | ||
7 | * | ||
8 | |||
9 | pixels i and j (corresponding to the sampling in pi,pj) are fully connected when d(i,j) <= rmin; | ||
10 | |||
11 | % test sequence | ||
12 | f = synimg(10); | ||
13 | [i,j] = cimgnbmap(size(f),2); | ||
14 | [ex,ey,egx,egy] = quadedgep(f); | ||
15 | a = affinityic(ex,ey,egx,egy,i,j) | ||
16 | show_dist_w(f,a); | ||
17 | |||
18 | * Stella X. Yu, Nov 19, 2001. | ||
19 | *=================================================================*/ | ||
20 | |||
21 | # include "mex.h" | ||
22 | # include "math.h" | ||
23 | |||
24 | void mexFunction( | ||
25 | int nargout, | ||
26 | mxArray *out[], | ||
27 | int nargin, | ||
28 | const mxArray *in[] | ||
29 | ) | ||
30 | { | ||
31 | /* declare variables */ | ||
32 | int nr, nc, np, total; | ||
33 | int i, j, k, ix, iy, jx, jy; | ||
34 | int *ir, *jc; | ||
35 | int squareDistance; | ||
36 | /* unsigned long *pi, *pj; */ | ||
37 | unsigned int *pi, *pj; | ||
38 | double *w; | ||
39 | |||
40 | double temp,a1,a2,wij; | ||
41 | double rMin; | ||
42 | double sigmaX, sigmaIntensite,valeurMinW; | ||
43 | double *image; | ||
44 | |||
45 | /* check argument */ | ||
46 | if (nargin<7) { | ||
47 | mexErrMsgTxt("Four input arguments required"); | ||
48 | } | ||
49 | if (nargout>1) { | ||
50 | mexErrMsgTxt("Too many output arguments"); | ||
51 | } | ||
52 | |||
53 | /* get edgel information */ | ||
54 | nr = mxGetM(in[0]); | ||
55 | nc = mxGetN(in[0]); | ||
56 | np = nr * nc; | ||
57 | |||
58 | image = mxGetPr(in[0]); | ||
59 | |||
60 | |||
61 | |||
62 | /* get new index pair */ | ||
63 | if (!mxIsUint32(in[1]) | !mxIsUint32(in[2])) { | ||
64 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
65 | } | ||
66 | if (mxGetM(in[2]) * mxGetN(in[2]) != np + 1) { | ||
67 | mexErrMsgTxt("Wrong index representation"); | ||
68 | } | ||
69 | pi = mxGetData(in[1]); | ||
70 | pj = mxGetData(in[2]); | ||
71 | |||
72 | /* create output */ | ||
73 | out[0] = mxCreateSparse(np,np,pj[np],mxREAL); | ||
74 | if (out[0]==NULL) { | ||
75 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
76 | } | ||
77 | w = mxGetPr(out[0]); | ||
78 | ir = mxGetIr(out[0]); | ||
79 | jc = mxGetJc(out[0]); | ||
80 | |||
81 | rMin = mxGetScalar(in[3]); | ||
82 | sigmaX = mxGetScalar(in[4]); | ||
83 | sigmaIntensite= mxGetScalar(in[5]); | ||
84 | valeurMinW = mxGetScalar(in[6]); | ||
85 | |||
86 | a1 = 1.0/ (sigmaX*sigmaX); | ||
87 | a2 = 1.0 / (sigmaIntensite*sigmaIntensite ); | ||
88 | |||
89 | /* computation */ | ||
90 | total = 0; | ||
91 | for (j=0; j<np; j++) { | ||
92 | |||
93 | jc[j] = total; | ||
94 | jx = j / nr; /* col */ | ||
95 | jy = j % nr; /* row */ | ||
96 | |||
97 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
98 | |||
99 | i = pi[k]; | ||
100 | |||
101 | if (i==j) { | ||
102 | wij= 1; /*voir*/ | ||
103 | |||
104 | } else { | ||
105 | ix = i / nr; | ||
106 | iy = i % nr; | ||
107 | |||
108 | squareDistance = (ix-jx)*(ix-jx)+(iy-jy)*(iy-jy); | ||
109 | |||
110 | temp = image[i]-image[j]; | ||
111 | wij = exp(- squareDistance * a1 - temp*temp * a2 ); | ||
112 | /*if(wij < valeurMinW) | ||
113 | wij = -0.1;*/ | ||
114 | } | ||
115 | ir[total] = i; | ||
116 | |||
117 | w[total] = wij; | ||
118 | total = total + 1; | ||
119 | |||
120 | } /* i */ | ||
121 | |||
122 | } /* j */ | ||
123 | |||
124 | jc[np] = total; | ||
125 | } | ||
126 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.dll b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.dll new file mode 100755 index 0000000..a47e22d --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexa64 b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexa64 new file mode 100755 index 0000000..06b2f57 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexglx b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexglx new file mode 100755 index 0000000..c6eed07 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityFirstLayer.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.c b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.c new file mode 100755 index 0000000..e01e516 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /*================================================================ | ||
2 | * function w = multiIntensityWppc(image,pi,pj,rMin,sigmaX,sigmaIntensite,valeurMinW, | ||
3 | * subgrid,nrSubgrid,ncSubgrid,subpi) | ||
4 | * Input: | ||
5 | * [pi,pj] = index pair representation for MALTAB sparse matrices | ||
6 | * Output: | ||
7 | * w = affinity with IC at [pi,pj] | ||
8 | * | ||
9 | |||
10 | imageX,wiInOriginalImage,wwj,rMin,dataWpp.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),wi{i} | ||
11 | |||
12 | |||
13 | pixels i and j (corresponding to the sampling in pi,pj) are fully connected when d(i,j) <= rmin; | ||
14 | |||
15 | % test sequence | ||
16 | f = synimg(10); | ||
17 | [i,j] = cimgnbmap(size(f),2); | ||
18 | [ex,ey,egx,egy] = quadedgep(f); | ||
19 | a = affinityic(ex,ey,egx,egy,i,j) | ||
20 | show_dist_w(f,a); | ||
21 | |||
22 | * Stella X. Yu, Nov 19, 2001. | ||
23 | *=================================================================*/ | ||
24 | |||
25 | # include "mex.h" | ||
26 | # include "math.h" | ||
27 | |||
28 | void mexFunction( | ||
29 | int nargout, | ||
30 | mxArray *out[], | ||
31 | int nargin, | ||
32 | const mxArray *in[] | ||
33 | ) | ||
34 | { | ||
35 | /* declare variables */ | ||
36 | int nr, nc, np, nW, total; | ||
37 | int i, j, k, t, ix, iy, jx, jy, nrSubgrid, ncSubgrid; | ||
38 | int *ir, *jc; | ||
39 | int squareDistance; | ||
40 | /* unsigned long *pi, *pj, *subpi; */ | ||
41 | unsigned int *pi, *pj, *subpi; | ||
42 | double *w, *subgrid, *tmp; | ||
43 | |||
44 | double temp,a1,a2,wij; | ||
45 | double rMin; | ||
46 | double sigmaX, sigmaIntensite,valeurMinW; | ||
47 | double *image; | ||
48 | |||
49 | /* check argument */ | ||
50 | if (nargin<11) { | ||
51 | mexErrMsgTxt("Eleven input arguments required"); | ||
52 | } | ||
53 | if (nargout>1) { | ||
54 | mexErrMsgTxt("Too many output arguments"); | ||
55 | } | ||
56 | |||
57 | /* get edgel information */ | ||
58 | nr = mxGetM(in[0]); | ||
59 | nc = mxGetN(in[0]); | ||
60 | np = nr * nc; | ||
61 | /*printf("size: %d, %d, %d\n", nc, nr, np); */ | ||
62 | image = mxGetPr(in[0]); | ||
63 | |||
64 | |||
65 | /*get subgrid information*/ | ||
66 | |||
67 | tmp = mxGetData(in[8]); | ||
68 | nrSubgrid = (int)tmp[0]; | ||
69 | |||
70 | /* printf("image end = %f ", image[np-1]); */ | ||
71 | |||
72 | tmp = mxGetData(in[9]); | ||
73 | ncSubgrid = (int)tmp[0]; | ||
74 | |||
75 | if (nrSubgrid* ncSubgrid != mxGetM(in[7])*mxGetN(in[7])) { | ||
76 | mexErrMsgTxt("Error in the size of the subgrid"); | ||
77 | } | ||
78 | subgrid = mxGetData(in[7]); | ||
79 | nW = nrSubgrid * ncSubgrid; | ||
80 | |||
81 | |||
82 | |||
83 | |||
84 | /* get new index pair */ | ||
85 | if (!mxIsUint32(in[1]) | !mxIsUint32(in[2])) { | ||
86 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
87 | } | ||
88 | if (mxGetM(in[2]) * mxGetN(in[2]) != nW + 1) { | ||
89 | mexErrMsgTxt("Wrong index representation"); | ||
90 | } | ||
91 | pi = mxGetData(in[1]); | ||
92 | pj = mxGetData(in[2]); | ||
93 | subpi = mxGetData(in[10]); | ||
94 | |||
95 | /* create output */ | ||
96 | out[0] = mxCreateSparse(nW,nW,pj[nW],mxREAL); | ||
97 | if (out[0]==NULL) { | ||
98 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
99 | } | ||
100 | |||
101 | w = mxGetPr(out[0]); | ||
102 | ir = mxGetIr(out[0]); | ||
103 | jc = mxGetJc(out[0]); | ||
104 | |||
105 | |||
106 | rMin = mxGetScalar(in[3]); | ||
107 | sigmaX = mxGetScalar(in[4]); | ||
108 | sigmaIntensite= mxGetScalar(in[5]); | ||
109 | valeurMinW = mxGetScalar(in[6]); | ||
110 | |||
111 | a1 = 1.0/ (sigmaX*sigmaX); | ||
112 | a2 = 1.0 / (sigmaIntensite*sigmaIntensite ); | ||
113 | |||
114 | |||
115 | |||
116 | /* computation */ | ||
117 | total = 0; | ||
118 | for (j=0; j<nW; j++) { | ||
119 | |||
120 | t= (int)subgrid[j]-1; /*on parcourt tous les pixels de la sous-grille dans la grille d'origine*/ | ||
121 | if ( (t<0) || (t>np-1)) {printf("badddddd!");} | ||
122 | /* printf("t = %d\n",t); | ||
123 | printf("j = %d\n",j); */ | ||
124 | jc[j] = total; | ||
125 | jx = t / nr; /* col */ | ||
126 | jy = t % nr; /* row */ | ||
127 | /* printf("pj[j+1] = %d\n",pj[j+1]); */ | ||
128 | |||
129 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
130 | /* printf("k = %d\n",k); */ | ||
131 | i = pi[k]-1; | ||
132 | ix = i / nr; | ||
133 | iy = i % nr; | ||
134 | squareDistance = (ix-jx)*(ix-jx)+(iy-jy)*(iy-jy);/*abs(ix-jx)+abs(iy-jy);*/ | ||
135 | if (squareDistance <= rMin) { wij = 1;} | ||
136 | else { | ||
137 | temp = image[i]-image[t]; | ||
138 | wij = exp(- squareDistance * a1 - temp*temp * a2 ); | ||
139 | /*if(wij < valeurMinW) | ||
140 | wij = 0;*/ | ||
141 | /*wij = exp( - temp*temp * a2 );*/ | ||
142 | } | ||
143 | |||
144 | ir[total] = (int)subpi[k]; | ||
145 | |||
146 | /* if (ir[total] >5000*5000 ) {printf("trouble! [%d,%d]\n",k,(int)subpi[k]);} */ | ||
147 | |||
148 | w[total] = wij; | ||
149 | |||
150 | total = total + 1; | ||
151 | |||
152 | } /* i */ | ||
153 | |||
154 | |||
155 | } /* j */ | ||
156 | |||
157 | jc[nW] = total; | ||
158 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.dll b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.dll new file mode 100755 index 0000000..16146c1 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexa64 b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexa64 new file mode 100755 index 0000000..525ca62 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexglx b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexglx new file mode 100755 index 0000000..a17fa03 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/multiIntensityWppc.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/quadedgep2.m b/SD-VBS/common/toolbox/MultiNcut/quadedgep2.m new file mode 100755 index 0000000..5041377 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/quadedgep2.m | |||
@@ -0,0 +1,188 @@ | |||
1 | % function [xs,ys,gx,gy,par,threshold,mag,mage,g,FIe,FIo,mago] = quadedgep(I,par,threshold); | ||
2 | % Input: | ||
3 | % I = image | ||
4 | % par = vector for 4 parameters | ||
5 | % [number of filter orientations, number of scale, filter size, elongation] | ||
6 | % To use default values, put 0. | ||
7 | % threshold = threshold on edge strength | ||
8 | % Output: | ||
9 | % [x,y,gx,gy] = locations and gradients of an ordered list of edgels | ||
10 | % x,y could be horizontal or vertical or 45 between pixel sites | ||
11 | % but it is guaranteed that there [floor(y) + (floor(x)-1)*nr] | ||
12 | % is ordered and unique. In other words, each edgel has a unique pixel id. | ||
13 | % par = actual par used | ||
14 | % threshold = actual threshold used | ||
15 | % mag = edge magnitude | ||
16 | % mage = phase map | ||
17 | % g = gradient map at each pixel | ||
18 | % [FIe,FIo] = odd and even filter outputs | ||
19 | % mago = odd filter output of optimum orientation | ||
20 | |||
21 | % Stella X. Yu, 2001 | ||
22 | |||
23 | % This is the multi scale version of the filtering | ||
24 | % For the moment the parameters are defined by default. See line 34 | ||
25 | |||
26 | |||
27 | function [x,y,gx,gy,par,threshold,mag_s,mage,g,FIe,FIo,mago] = quadedgep2(I,par,data,threshold); | ||
28 | |||
29 | |||
30 | if nargin<4 | isempty(threshold), | ||
31 | threshold = 0.1; | ||
32 | end | ||
33 | |||
34 | [r,c] = size(I); | ||
35 | def_par = [4,30,3]; | ||
36 | |||
37 | display_on = 1; | ||
38 | |||
39 | % take care of parameters, any missing value is substituted by a default value | ||
40 | if nargin<2 | isempty(par), | ||
41 | par = def_par; | ||
42 | end | ||
43 | % par(end+1:4)=0; | ||
44 | % par = par(:); | ||
45 | % j = (par>0); | ||
46 | % have_value = [ j, 1-j ]; | ||
47 | % j = 1; n_filter = have_value(j,:) * [par(j); def_par(j)]; | ||
48 | % j = 2; n_scale = have_value(j,:) * [par(j); def_par(j)]; | ||
49 | % j = 3; winsz = have_value(j,:) * [par(j); def_par(j)]; | ||
50 | % j = 4; enlong = have_value(j,:) * [par(j); def_par(j)]; | ||
51 | |||
52 | n_ori = par(1); %if it doesn't work, par<-def_par | ||
53 | |||
54 | winsz = par(2); | ||
55 | enlong = par(3); | ||
56 | |||
57 | % always make filter size an odd number so that the results will not be skewed | ||
58 | j = winsz/2; | ||
59 | if not(j > fix(j) + 0.1), | ||
60 | winsz = winsz + 1; | ||
61 | end | ||
62 | |||
63 | % filter the image with quadrature filters | ||
64 | if (isempty(data.W.scales)) | ||
65 | error ('no scales entered'); | ||
66 | end | ||
67 | |||
68 | n_scale=length(data.W.scales); | ||
69 | filter_scales=data.W.scales; | ||
70 | % | ||
71 | % if strcmp(data.dataWpp.mode,'multiscale') | ||
72 | % n_scale=size((data.dataWpp.scales),2); | ||
73 | % filter_scales=data.dataWpp.scales; | ||
74 | % else | ||
75 | % filter_scales=data.dataWpp.scales(1); | ||
76 | % n_scale=1; | ||
77 | % end | ||
78 | % if n_scale>0&&strcmp(data.dataWpp.mode,'multiscale') | ||
79 | % if (~isempty(data.dataWpp.scales)) | ||
80 | % filter_scales=data.dataWpp.scales; | ||
81 | % else | ||
82 | % filter_scales=zeros(1,n_scale); | ||
83 | % | ||
84 | % for i=1:n_scale, | ||
85 | % filter_scales(i)=(sqrt(2))^(i-1); | ||
86 | % end | ||
87 | % data.dataWpp.scales=filter_scales; | ||
88 | % end | ||
89 | % else filter_scale=1; | ||
90 | % data.dataWpp.scales=filter_scales; | ||
91 | % end | ||
92 | % | ||
93 | % %%%%%%% juste pour que ca tourne | ||
94 | % if ~strcmp(data.dataWpp.mode,'multiscale') | ||
95 | % filter_scales=data.dataWpp.scales(1); | ||
96 | % n_scale=4; | ||
97 | % end | ||
98 | % %%%%%%%%%%%% | ||
99 | |||
100 | FBo = make_filterbank_odd2(n_ori,filter_scales,winsz,enlong); | ||
101 | FBe = make_filterbank_even2(n_ori,filter_scales,winsz,enlong); | ||
102 | n = ceil(winsz/2); | ||
103 | f = [fliplr(I(:,2:n+1)), I, fliplr(I(:,c-n:c-1))]; | ||
104 | f = [flipud(f(2:n+1,:)); f; flipud(f(r-n:r-1,:))]; | ||
105 | FIo = fft_filt_2(f,FBo,1); | ||
106 | FIo = FIo(n+[1:r],n+[1:c],:); | ||
107 | FIe = fft_filt_2(f,FBe,1); | ||
108 | FIe = FIe(n+[1:r],n+[1:c],:); | ||
109 | |||
110 | % compute the orientation energy and recover a smooth edge map | ||
111 | % pick up the maximum energy across scale and orientation | ||
112 | % even filter's output: as it is the second derivative, zero cross localize the edge | ||
113 | % odd filter's output: orientation | ||
114 | |||
115 | [nr,nc,nb] = size(FIe); | ||
116 | |||
117 | FIe = reshape(FIe, nr,nc,n_ori,length(filter_scales)); | ||
118 | FIo = reshape(FIo, nr,nc,n_ori,length(filter_scales)); | ||
119 | |||
120 | mag_s = zeros(nr,nc,n_scale); | ||
121 | mag_a = zeros(nr,nc,n_ori); | ||
122 | |||
123 | mage = zeros(nr,nc,n_scale); | ||
124 | mago = zeros(nr,nc,n_scale); | ||
125 | mage = zeros(nr,nc,n_scale); | ||
126 | mago = zeros(nr,nc,n_scale); | ||
127 | |||
128 | |||
129 | |||
130 | for i = 1:n_scale, | ||
131 | mag_s(:,:,i) = sqrt(sum(FIo(:,:,:,i).^2,3)+sum(FIe(:,:,:,i).^2,3)); | ||
132 | mag_a = sqrt(FIo(:,:,:,i).^2+FIe(:,:,:,i).^2); | ||
133 | [tmp,max_id] = max(mag_a,[],3); | ||
134 | |||
135 | base_size = nr * nc; | ||
136 | id = [1:base_size]'; | ||
137 | mage(:,:,i) = reshape(FIe(id+(max_id(:)-1)*base_size+(i-1)*base_size*n_ori),[nr,nc]); | ||
138 | mago(:,:,i) = reshape(FIo(id+(max_id(:)-1)*base_size+(i-1)*base_size*n_ori),[nr,nc]); | ||
139 | |||
140 | mage(:,:,i) = (mage(:,:,i)>0) - (mage(:,:,i)<0); | ||
141 | |||
142 | if display_on, | ||
143 | ori_incr=pi/n_ori; % to convert jshi's coords to conventional image xy | ||
144 | ori_offset=ori_incr/2; | ||
145 | theta = ori_offset+([1:n_ori]-1)*ori_incr; % orientation detectors | ||
146 | % [gx,gy] are image gradient in image xy coords, winner take all | ||
147 | |||
148 | ori = theta(max_id); | ||
149 | ori = ori .* (mago(:,:,i)>0) + (ori + pi).*(mago(:,:,i)<0); | ||
150 | gy{i} = mag_s(:,:,i) .* cos(ori); | ||
151 | gx{i} = -mag_s(:,:,i) .* sin(ori); | ||
152 | g = cat(3,gx{i},gy{i}); | ||
153 | |||
154 | % phase map: edges are where the phase changes | ||
155 | mag_th = max(max(mag_s(:,:,i))) * threshold; | ||
156 | eg = (mag_s(:,:,i)>mag_th); | ||
157 | h = eg & [(mage(2:r,:,i) ~= mage(1:r-1,:,i)); zeros(1,nc)]; | ||
158 | v = eg & [(mage(:,2:c,i) ~= mage(:,1:c-1,i)), zeros(nr,1)]; | ||
159 | [y{i},x{i}] = find(h | v); | ||
160 | k = y{i} + (x{i}-1) * nr; | ||
161 | h = h(k); | ||
162 | v = v(k); | ||
163 | y{i} = y{i} + h * 0.5; % i | ||
164 | x{i} = x{i} + v * 0.5; % j | ||
165 | t = h + v * nr; | ||
166 | gx{i} = g(k) + g(k+t); | ||
167 | k = k + (nr * nc); | ||
168 | gy{i} = g(k) + g(k+t); | ||
169 | |||
170 | % figure(1); | ||
171 | % clf; | ||
172 | % imagesc(I);colormap(gray); | ||
173 | % hold on; | ||
174 | % quiver(x,y,gx,gy); hold off; | ||
175 | % title(sprintf('scale = %d, press return',i)); | ||
176 | |||
177 | % pause; | ||
178 | 0; | ||
179 | else | ||
180 | x = []; | ||
181 | y = []; | ||
182 | gx = []; | ||
183 | gy =[]; | ||
184 | g= []; | ||
185 | end | ||
186 | end | ||
187 | |||
188 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/quickNcutHardBiais2.m b/SD-VBS/common/toolbox/MultiNcut/quickNcutHardBiais2.m new file mode 100755 index 0000000..3ca1046 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/quickNcutHardBiais2.m | |||
@@ -0,0 +1,187 @@ | |||
1 | % function [v,s] = quickNcutHardBiais(W,U,nbEigenValues,dataGraphCut) | ||
2 | %ligne 35 : changement tim | ||
3 | %[v,s] = ncut(W,nbEigenValues,[],offset); | ||
4 | %devient : | ||
5 | %[v,s] = tim_ncut_rapide(W,nbEigenValues,[],offset); | ||
6 | %et eigs devient tim_eigs | ||
7 | |||
8 | % Input: | ||
9 | % W = affinity matrix | ||
10 | % U = hard constraint matrix, could be a cell of partial grouping | ||
11 | % nbEigenValues = number of eigenvectors | ||
12 | % offset = regularization factor, default = 0 | ||
13 | % Output: | ||
14 | % v = eigenvector | ||
15 | % s = eigenvalue of (W,d), s.t. U' * x = 0. | ||
16 | |||
17 | % call eigs using my own * operation | ||
18 | |||
19 | % Stella X. Yu, Jan 2002. | ||
20 | |||
21 | function [v,s] = quickNcutHardBiais2(W,U,nbEigenValues,dataGraphCut)%voir : rajouter sigma | ||
22 | n = size(W,1); | ||
23 | nbEigenValues = min(nbEigenValues,n); | ||
24 | |||
25 | offset = dataGraphCut.offset; | ||
26 | %offset = 2; | ||
27 | |||
28 | % degrees and regularization | ||
29 | d = sum(abs(W),2); | ||
30 | dr = 0.5 * (d - sum(W,2)); | ||
31 | d = d + offset * 2; | ||
32 | dr = dr + offset; | ||
33 | W = W + spdiags(dr,0,n,n); | ||
34 | clear dr | ||
35 | |||
36 | % normalize | ||
37 | Dinvsqrt = 1./sqrt(d+eps); | ||
38 | P = spmtimesd(W,Dinvsqrt,Dinvsqrt); | ||
39 | clear W; | ||
40 | |||
41 | % if max(max(P-P')) < 1e-10 %ou eps | ||
42 | % %S = sparse(1:n,1:n,0.5); | ||
43 | % P =max(P,P'); | ||
44 | % % P=S*(P+P'); | ||
45 | % %P=0.5*(P+P'); | ||
46 | % options.issym = 1; | ||
47 | % end | ||
48 | P = sparsifyc(P,dataGraphCut.valeurMin); | ||
49 | options.issym = 1; | ||
50 | |||
51 | Ubar = spmtimesd(U,Dinvsqrt,[]); | ||
52 | %Ubar = sparsifyc(Ubar,dataGraphCut.valeurMin); %voir | ||
53 | |||
54 | options.disp = dataGraphCut.verbose; | ||
55 | options.maxit = dataGraphCut.maxiterations; | ||
56 | options.tol = dataGraphCut.eigsErrorTolerance; | ||
57 | |||
58 | options.v0 = ones(size(P,1),1);%voir | ||
59 | |||
60 | options.p = max(35,2*nbEigenValues); %voir | ||
61 | options.p = min(options.p , n); | ||
62 | |||
63 | % nouvelle idee : factorisation de Cholesky | ||
64 | C=Ubar'*Ubar; | ||
65 | %permutation = symamd(C); | ||
66 | %R = cholinc(C(permutation,permutation)); | ||
67 | t_chol_Ubar = cputime; | ||
68 | [R,ooo] = cholinc(C,'0'); | ||
69 | t_chol_Ubar = cputime - t_chol_Ubar; | ||
70 | %if error occurs, check if Ubar = sparsifyc(Ubar,dataGraphCut.valeurMin); | ||
71 | %sparsifies too much | ||
72 | |||
73 | |||
74 | % compute H = (Ubar'*Ubar)^(-1) | ||
75 | % t_inv_H = cputime; | ||
76 | % H = inv(sparsifyc(Ubar' * Ubar,dataGraphCut.valeurMin)); %changer | ||
77 | % t_inv_H = cputime - t_inv_H; | ||
78 | % H = sparsifyc(H,dataGraphCut.valeurMin); | ||
79 | % tEigs = cputime; | ||
80 | % if options.issym & max(max(H-H')) < 1e-10 | ||
81 | % [vbar,s,convergence] = tim_eigs(@mex_projection_inv_symmetric,n,nbEigenValues,'lm',options,triu(P),Ubar,triu(H)); | ||
82 | % else | ||
83 | % [vbar,s,convergence] = tim_eigs(@mex_projection_inv,n,nbEigenValues,'lm',options,P,Ubar,H); | ||
84 | % end | ||
85 | % tEigs = cputime - tEigs; | ||
86 | % | ||
87 | |||
88 | |||
89 | |||
90 | R = sparsifyc(R,dataGraphCut.valeurMin); | ||
91 | tEigs = cputime; | ||
92 | if options.issym | ||
93 | [vbar,s,convergence] = tim_eigs(@mex_projection_QR_symmetric,n,nbEigenValues,'lm',options,tril(P),Ubar,R); | ||
94 | else | ||
95 | [vbar,s,convergence] = tim_eigs(@mex_projection_QR,n,nbEigenValues,'lm',options,P,Ubar,R); | ||
96 | end | ||
97 | tEigs = cputime - tEigs; | ||
98 | |||
99 | |||
100 | %afficheTexte(sprintf('\n\nTemps ecoule pendant eigs : %g',tEigs),dataGraphCut.verbose,2); | ||
101 | %afficheTexte(sprintf('\nTemps ecoule pendant chol(Ubar''*Ubar) : %g',t_chol_Ubar),dataGraphCut.verbose); | ||
102 | if convergence~=0 | ||
103 | afficheTexte(sprintf(' (Non-convergence)'),dataGraphCut.verbose); | ||
104 | end | ||
105 | |||
106 | |||
107 | %disp(sprintf('nnz(P) : %f\n',nnz(P))); | ||
108 | %disp(sprintf('nnz(Ubar) : %f\n',nnz(Ubar))); | ||
109 | %disp(sprintf('nnz(R) : %f\n',nnz(R))); | ||
110 | %disp(sprintf('nnz(global) : %f\n',nnz(P) + 4 * nnz(Ubar) + 4*nnz(R))); | ||
111 | |||
112 | |||
113 | |||
114 | s = real(diag(s)); | ||
115 | [x,y] = sort(-s); | ||
116 | s = -x; | ||
117 | vbar = vbar(:,y); | ||
118 | |||
119 | |||
120 | v = spdiags(Dinvsqrt,0,n,n) * vbar; | ||
121 | |||
122 | for i=1:size(v,2) | ||
123 | %v(:,i) = v(:,i) / max(abs(v(:,i))); | ||
124 | v(:,i) = (v(:,i) / norm(v(:,i)) )*norm(ones(n,1)); | ||
125 | if v(1,i)~=0 | ||
126 | v(:,i) = - v(:,i) * sign(v(1,i)); | ||
127 | end | ||
128 | end | ||
129 | |||
130 | % % nouvelle idee : factorisation de Cholesky | ||
131 | % t_chol_Ubar = cputime; | ||
132 | % R = chol(Ubar' * Ubar); | ||
133 | % t_chol_Ubar = cputime - t_chol_Ubar; | ||
134 | % R = sparsifyc(R,dataGraphCut.valeurMin); | ||
135 | % tEigs = cputime; | ||
136 | % if options.issym | ||
137 | % [vbar,s,convergence] = tim_eigs(@mex_projection_QR_symmetric,n,nbEigenValues,'lm',options,triu(P),Ubar,R); | ||
138 | % else | ||
139 | % [vbar,s,convergence] = tim_eigs(@mex_projection_QR,n,nbEigenValues,'lm',options,P,Ubar,R); | ||
140 | % end | ||
141 | % tEigs = cputime - tEigs; | ||
142 | |||
143 | |||
144 | % % compute H = (Ubar'*Ubar)^(-1) | ||
145 | % t_inv_H = cputime; | ||
146 | % H = inv(sparsifyc(Ubar' * Ubar,dataGraphCut.valeurMin)); %changer | ||
147 | % t_inv_H = cputime - t_inv_H; | ||
148 | % H = sparsifyc(H,dataGraphCut.valeurMin); | ||
149 | % tEigs = cputime; | ||
150 | % if options.issym & max(max(H-H')) < 1e-10 | ||
151 | % [vbar,s,convergence] = tim_eigs(@mex_projection_inv_symmetric,n,nbEigenValues,'lm',options,triu(P),Ubar,triu(H)); | ||
152 | % else | ||
153 | % [vbar,s,convergence] = tim_eigs(@mex_projection_inv,n,nbEigenValues,'lm',options,P,Ubar,H); | ||
154 | % end | ||
155 | % tEigs = cputime - tEigs; | ||
156 | |||
157 | |||
158 | |||
159 | % % idee de mon rapport... semble pas marcher car R = qr(Ubar,0) est plus | ||
160 | % % lent que H = inv(sparsifyc(Ubar' * Ubar,dataGraphCut.valeurMin)); | ||
161 | % t_qr_Ubar = cputime; | ||
162 | % R = qr(Ubar,0); | ||
163 | % t_qr_Ubar = cputime - t_qr_Ubar; | ||
164 | % R = sparsifyc(R,dataGraphCut.valeurMin); | ||
165 | % tEigs2 = cputime; | ||
166 | % if options.issym | ||
167 | % [vbar2,s2,convergence] = tim_eigs(@mex_projection_QR_symmetric,n,nbEigenValues,'lm',options,triu(P),Ubar,R); | ||
168 | % else | ||
169 | % [vbar2,s2,convergence] = tim_eigs(@mex_projection_QR,n,nbEigenValues,'lm',options,P,Ubar,R); | ||
170 | % end | ||
171 | % tEigs2 = cputime - tEigs2; | ||
172 | |||
173 | |||
174 | |||
175 | % idee de Jianbo... semble pas marcher car on a besoin de prendre k maximal | ||
176 | % dans [A,S,B] = svds(Ubar,k); | ||
177 | % | ||
178 | % [A,S,B] = svds(Ubar,300); | ||
179 | % A = sparsifyc(A,dataGraphCut.valeurMin); | ||
180 | % tEigs = cputime; | ||
181 | % [vbar,s,convergence] = tim_eigs(@mex_projection_svd,n,nbEigenValues,'lm',options,P,A); | ||
182 | |||
183 | % afficheTexte(sprintf('\ninv(H) : %g',t_inv_H),dataGraphCut.verbose); | ||
184 | % afficheTexte(sprintf('\n\nTemps ecoule pendant eigs : %g',tEigs2),dataGraphCut.verbose,2); | ||
185 | % afficheTexte(sprintf('\nqr(Ubar) : %g',t_qr_Ubar),dataGraphCut.verbose); | ||
186 | % disp(sprintf('nnz(H) : %f\n',nnz(H))); | ||
187 | % disp(sprintf('nnz(global) : %f\n',nnz(P) + 4 * nnz(Ubar) + 2*nnz(H))); | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/read_data.m b/SD-VBS/common/toolbox/MultiNcut/read_data.m new file mode 100755 index 0000000..c0929c0 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/read_data.m | |||
@@ -0,0 +1,13 @@ | |||
1 | |||
2 | %fnames = dir('/home/jshi/Results_DLIB/SegLabl*.mat'); | ||
3 | |||
4 | fnames = dir('/data/jshi/DLIB/Results/Results_DLIB/SegLabl*.mat'); | ||
5 | |||
6 | for j=1:length(fnames), | ||
7 | cm = sprintf('load /data/jshi/DLIB/Results/Results_DLIB/%s',fnames(j).name); | ||
8 | disp(cm);eval(cm); | ||
9 | figure(1);imagesc(I); colormap(gray); axis image; | ||
10 | figure(2); imagesc(SegLabel); axis image; | ||
11 | |||
12 | pause; | ||
13 | end | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/readimage.m b/SD-VBS/common/toolbox/MultiNcut/readimage.m new file mode 100755 index 0000000..e45fa19 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/readimage.m | |||
@@ -0,0 +1,15 @@ | |||
1 | function I = readimage(fn,maxSize); | ||
2 | |||
3 | Io = imread(fn); | ||
4 | [nr,nc,nb] = size(Io); | ||
5 | |||
6 | if nb>1, | ||
7 | I = rgb2gray(Io); | ||
8 | else | ||
9 | I= Io; | ||
10 | end | ||
11 | |||
12 | %maxSize = 400; | ||
13 | if max(nr,nc) > maxSize, | ||
14 | I = imresize(I,maxSize/max(nr,nc)); | ||
15 | end | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/run_script.m b/SD-VBS/common/toolbox/MultiNcut/run_script.m new file mode 100755 index 0000000..3d2e8c6 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/run_script.m | |||
@@ -0,0 +1,60 @@ | |||
1 | %% set path for the MNcut code | ||
2 | |||
3 | if 1, | ||
4 | % MNcutDir = '/home/jshi/Matlab/Toolbox/MultiNcut'; | ||
5 | MNcutDir = 'C:\qihuizhu\Checkout\Human\Source\MultiNcut_new\MultiNcut'; | ||
6 | path(path,MNcutDir); | ||
7 | end | ||
8 | |||
9 | %% set the image input and output dir. | ||
10 | % imagedir = '/data/jshi/DLIB/image.cd'; | ||
11 | % imagedir = 'C:\qihuizhu\Checkout\Human\Source\Data\test'; | ||
12 | imagedir = 'C:\qihuizhu\Checkout\Human\Data\Current\baby_case5'; | ||
13 | % imageformat = 'ppm'; | ||
14 | imageformat = 'tif'; | ||
15 | |||
16 | % OutputDir = '/home/jshi/Results_DLIB'; | ||
17 | % OutputDir = 'C:\qihuizhu\Checkout\Human\Source\Data\test'; | ||
18 | OutputDir = 'C:\qihuizhu\Checkout\Human\Result\Segmentation\MultiNcut_new_03.07'; | ||
19 | |||
20 | a = dir(OutputDir); | ||
21 | if (length(a) == 0), | ||
22 | cm = sprintf('mkdir %s',OutputDir); | ||
23 | disp(cm); eval(cm); | ||
24 | end | ||
25 | |||
26 | files = dir(sprintf('%s/*.%s',imagedir,imageformat)); | ||
27 | |||
28 | %% image size definition | ||
29 | imageSize = 400; | ||
30 | |||
31 | % for id =11:200, | ||
32 | for id = 1:length(files) | ||
33 | %for id = 19:19, | ||
34 | I=readimage(sprintf('%s/%s',imagedir,files(id).name),imageSize); | ||
35 | |||
36 | num_segs = [10, 20]; | ||
37 | |||
38 | tic | ||
39 | [SegLabel,eigenVectors,eigenValues]= MNcut(I,num_segs); | ||
40 | toc | ||
41 | |||
42 | for j=1:size(SegLabel,3), | ||
43 | [gx,gy] = gradient(SegLabel(:,:,j)); | ||
44 | bw = (abs(gx)>0.1) + (abs(gy) > 0.1); | ||
45 | |||
46 | figure(1);clf; J1=showmask(double(I),bw); imagesc(J1);axis image; | ||
47 | cm = sprintf('print -djpeg %s/file%.4d-%.2d.jpg',OutputDir,id,num_segs(j)); disp(cm);eval(cm); | ||
48 | |||
49 | |||
50 | figure(10);imagesc(SegLabel(:,:,j));axis image; | ||
51 | cm = sprintf('print -djpeg %s/Seg%.4d-%.2d.jpg',OutputDir,id,num_segs(j));disp(cm);eval(cm); | ||
52 | |||
53 | % pause; | ||
54 | end | ||
55 | |||
56 | fname = files(id).name; | ||
57 | %cm = sprintf('save %s/SegLabl%.4d.mat I SegLabel fname',OutputDir,id); disp(cm); eval(cm); | ||
58 | %cm = sprintf('save %s/SegEig%.4d.mat eigenVectors eigenValues',OutputDir,id);disp(cm); eval(cm); | ||
59 | |||
60 | end | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/showmask.m b/SD-VBS/common/toolbox/MultiNcut/showmask.m new file mode 100755 index 0000000..6fd1142 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/showmask.m | |||
@@ -0,0 +1,65 @@ | |||
1 | % function RGB=showmask(V,M,hue); | ||
2 | % Input: | ||
3 | % V = image | ||
4 | % M = nonnegative mask | ||
5 | % hue = a number in [0,1], red,yellow,green,cyan,...,red | ||
6 | % a char, 'r','g','b','y','c','m' | ||
7 | % or a matrix of the same size of image | ||
8 | % eg. hue = mask1 * 0.7 + mask2 * 1; | ||
9 | % | ||
10 | % Output: | ||
11 | % RGB = an RGB image with V as shades and M as saturated hues | ||
12 | % If no output is required, this image is displayed. | ||
13 | |||
14 | % Stella X. YU, 2000. Based on Jianbo Shi's version. | ||
15 | |||
16 | function RGB=showmask(V,M,hue); | ||
17 | |||
18 | if nargin<3 | isempty(hue), | ||
19 | hue = 0; | ||
20 | end | ||
21 | if ischar(hue), | ||
22 | switch hue, | ||
23 | case 'r', hue = 1.0; | ||
24 | case 'g', hue = 0.3; | ||
25 | case 'b', hue = 0.7; | ||
26 | case 'y', hue = 0.15; | ||
27 | case 'c', hue = 0.55; | ||
28 | case 'm', hue = 0.85; | ||
29 | end | ||
30 | end | ||
31 | |||
32 | |||
33 | V=V-min(V(:)); | ||
34 | V=V/max(V(:)); | ||
35 | V=.25+0.75*V; %brighten things up a bit | ||
36 | |||
37 | M = double(M); | ||
38 | M = M-min(M(:)); | ||
39 | M = M/max(M(:)); | ||
40 | |||
41 | H = hue+zeros(size(V)); | ||
42 | S = M; | ||
43 | RGB = hsv2rgb(H,S,V); | ||
44 | |||
45 | if nargout>0, | ||
46 | return; | ||
47 | end | ||
48 | |||
49 | hold off; image(RGB); axis('image'); | ||
50 | s = cell(1,2); | ||
51 | if isempty(inputname(1)), | ||
52 | s{1} = 'image'; | ||
53 | else | ||
54 | s{1} = inputname(1); | ||
55 | end | ||
56 | if isempty(inputname(2)), | ||
57 | s{2} = 'mask'; | ||
58 | else | ||
59 | s{2} = inputname(2); | ||
60 | end | ||
61 | title(sprintf('%s and colored %s',s{1},s{2})); | ||
62 | |||
63 | if nargout==0, | ||
64 | clear RGB; | ||
65 | end | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/sparsifyc.c b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.c new file mode 100755 index 0000000..82fca98 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.c | |||
@@ -0,0 +1,232 @@ | |||
1 | /*================================================================= | ||
2 | * syntax: SPMX = SPARSIFY(MX, THRES) | ||
3 | * | ||
4 | * SPARSIFY - sparsify the input matrix, i.e. ignore the values | ||
5 | * of the matrix which are below a threshold | ||
6 | * | ||
7 | * Input: - MX: m-by-n matrix (sparse or full) | ||
8 | * - THRES: threshold value (double) | ||
9 | * | ||
10 | * Output: - SPMX: m-by-n sparse matrix only with values | ||
11 | * whose absolut value is above the given threshold | ||
12 | * | ||
13 | * Written by Mirko Visontai (10/24/2003) | ||
14 | *=================================================================*/ | ||
15 | |||
16 | |||
17 | #include <math.h> | ||
18 | #include "mex.h" | ||
19 | |||
20 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) | ||
21 | { | ||
22 | /* Declare variable */ | ||
23 | int i,m,n,nzmax,newnnz,col,processed,passed; | ||
24 | int starting_row_index, current_row_index, stopping_row_index; | ||
25 | double *in_pr,*in_pi,*out_pr,*out_pi; | ||
26 | int *in_ir,*in_jc,*out_ir,*out_jc; | ||
27 | double thres; | ||
28 | |||
29 | /* Check for proper number of input and output arguments */ | ||
30 | if ((nlhs != 1) || (nrhs != 2)){ | ||
31 | mexErrMsgTxt("usage: SPMX = SPARSIFY(MX, THRES)."); | ||
32 | } | ||
33 | /* if matrix is complex threshold the norm of the numbers */ | ||
34 | if (mxIsComplex(prhs[0])){ | ||
35 | /* Check data type of input argument */ | ||
36 | if (mxIsSparse(prhs[0])){ | ||
37 | |||
38 | /* read input */ | ||
39 | in_pr = mxGetPr(prhs[0]); | ||
40 | in_pi = mxGetPi(prhs[0]); | ||
41 | in_ir = mxGetIr(prhs[0]); | ||
42 | in_jc = mxGetJc(prhs[0]); | ||
43 | nzmax = mxGetNzmax(prhs[0]); | ||
44 | m = mxGetM(prhs[0]); | ||
45 | n = mxGetN(prhs[0]); | ||
46 | thres = mxGetScalar(prhs[1]); | ||
47 | |||
48 | /* Count new nonzeros */ | ||
49 | newnnz=0; | ||
50 | for(i=0; i<nzmax; i++){ | ||
51 | if (sqrt(in_pr[i]*in_pr[i] + in_pi[i]*in_pi[i])>thres) {newnnz++;} | ||
52 | } | ||
53 | |||
54 | if (newnnz>0){ | ||
55 | /* create output */ | ||
56 | plhs[0] = mxCreateSparse(m,n,newnnz,mxCOMPLEX); | ||
57 | if (plhs[0]==NULL) | ||
58 | mexErrMsgTxt("Could not allocate enough memory!\n"); | ||
59 | out_pr = mxGetPr(plhs[0]); | ||
60 | out_pi = mxGetPr(plhs[0]); | ||
61 | out_ir = mxGetIr(plhs[0]); | ||
62 | out_jc = mxGetJc(plhs[0]); | ||
63 | passed = 0; | ||
64 | out_jc[0] = 0; | ||
65 | for (col=0; col<n; col++){ | ||
66 | starting_row_index = in_jc[col]; | ||
67 | stopping_row_index = in_jc[col+1]; | ||
68 | out_jc[col+1] = out_jc[col]; | ||
69 | if (starting_row_index == stopping_row_index) | ||
70 | continue; | ||
71 | else { | ||
72 | for (current_row_index = starting_row_index; | ||
73 | current_row_index < stopping_row_index; | ||
74 | current_row_index++) { | ||
75 | if (sqrt(in_pr[current_row_index]*in_pr[current_row_index] + | ||
76 | in_pi[current_row_index]*in_pi[current_row_index] ) > thres){ | ||
77 | |||
78 | out_pr[passed]=in_pr[current_row_index]; | ||
79 | out_pi[passed]=in_pi[current_row_index]; | ||
80 | out_ir[passed]=in_ir[current_row_index]; | ||
81 | out_jc[col+1] = out_jc[col+1]+1; | ||
82 | passed++; | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | else{ | ||
89 | plhs[0] = mxCreateSparse(m,n,0,mxCOMPLEX); | ||
90 | } | ||
91 | } | ||
92 | else{ /* for full matrices */ | ||
93 | /* read input */ | ||
94 | in_pr = mxGetPr(prhs[0]); | ||
95 | in_pi = mxGetPr(prhs[0]); | ||
96 | m = mxGetM(prhs[0]); | ||
97 | n = mxGetN(prhs[0]); | ||
98 | thres = mxGetScalar(prhs[1]); | ||
99 | |||
100 | /* Count new nonzeros */ | ||
101 | newnnz=0; | ||
102 | for(i=0; i<m*n; i++){ | ||
103 | if (sqrt(in_pr[i]*in_pr[i] + in_pi[i]*in_pi[i])>thres) {newnnz++;} | ||
104 | } | ||
105 | |||
106 | if (newnnz>0){ | ||
107 | /* create output */ | ||
108 | plhs[0] = mxCreateSparse(m,n,newnnz,mxCOMPLEX); | ||
109 | if (plhs[0]==NULL) | ||
110 | mexErrMsgTxt("Could not allocate enough memory!\n"); | ||
111 | out_pr = mxGetPr(plhs[0]); | ||
112 | out_pi = mxGetPi(plhs[0]); | ||
113 | out_ir = mxGetIr(plhs[0]); | ||
114 | out_jc = mxGetJc(plhs[0]); | ||
115 | passed = 0; | ||
116 | out_jc[0] = 0; | ||
117 | |||
118 | for (col=0; col<n; col++){ | ||
119 | out_jc[col+1] = out_jc[col]; | ||
120 | for (current_row_index=0; current_row_index<m; current_row_index++){ | ||
121 | if (sqrt(in_pr[current_row_index+m*col]*in_pr[current_row_index+m*col] + | ||
122 | in_pi[current_row_index+m*col]*in_pi[current_row_index+m*col]) > thres){ | ||
123 | |||
124 | out_pr[passed]=in_pr[current_row_index+m*col]; | ||
125 | out_ir[passed]=current_row_index; | ||
126 | out_jc[col+1] = out_jc[col+1]+1; | ||
127 | passed++; | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | else{ | ||
133 | plhs[0] = mxCreateSparse(m,n,0,mxCOMPLEX); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | else { | ||
138 | /* Check data type of input argument */ | ||
139 | if (mxIsSparse(prhs[0])){ | ||
140 | |||
141 | /* read input */ | ||
142 | in_pr = mxGetPr(prhs[0]); | ||
143 | in_ir = mxGetIr(prhs[0]); | ||
144 | in_jc = mxGetJc(prhs[0]); | ||
145 | nzmax = mxGetNzmax(prhs[0]); | ||
146 | n = mxGetN(prhs[0]); | ||
147 | m = mxGetM(prhs[0]); | ||
148 | thres = mxGetScalar(prhs[1]); | ||
149 | |||
150 | /* Count new nonzeros */ | ||
151 | newnnz=0; | ||
152 | for(i=0; i<nzmax; i++){ | ||
153 | if ((fabs(in_pr[i]))>thres) {newnnz++;} | ||
154 | } | ||
155 | |||
156 | if (newnnz>0){ | ||
157 | /* create output */ | ||
158 | plhs[0] = mxCreateSparse(m,n,newnnz,mxREAL); | ||
159 | if (plhs[0]==NULL) | ||
160 | mexErrMsgTxt("Could not allocate enough memory!\n"); | ||
161 | out_pr = mxGetPr(plhs[0]); | ||
162 | out_ir = mxGetIr(plhs[0]); | ||
163 | out_jc = mxGetJc(plhs[0]); | ||
164 | passed = 0; | ||
165 | out_jc[0] = 0; | ||
166 | for (col=0; col<n; col++){ | ||
167 | starting_row_index = in_jc[col]; | ||
168 | stopping_row_index = in_jc[col+1]; | ||
169 | out_jc[col+1] = out_jc[col]; | ||
170 | if (starting_row_index == stopping_row_index) | ||
171 | continue; | ||
172 | else { | ||
173 | for (current_row_index = starting_row_index; | ||
174 | current_row_index < stopping_row_index; | ||
175 | current_row_index++) { | ||
176 | if (fabs(in_pr[current_row_index])>thres){ | ||
177 | out_pr[passed]=in_pr[current_row_index]; | ||
178 | out_ir[passed]=in_ir[current_row_index]; | ||
179 | out_jc[col+1] = out_jc[col+1]+1; | ||
180 | passed++; | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | else{ | ||
187 | plhs[0] = mxCreateSparse(m,n,0,mxREAL); | ||
188 | } | ||
189 | } | ||
190 | else{ /* for full matrices */ | ||
191 | /* read input */ | ||
192 | in_pr = mxGetPr(prhs[0]); | ||
193 | n = mxGetN(prhs[0]); | ||
194 | m = mxGetM(prhs[0]); | ||
195 | thres = mxGetScalar(prhs[1]); | ||
196 | |||
197 | /* Count new nonzeros */ | ||
198 | newnnz=0; | ||
199 | for(i=0; i<m*n; i++){ | ||
200 | if ((fabs(in_pr[i]))>thres) {newnnz++;} | ||
201 | } | ||
202 | |||
203 | if (newnnz>0){ | ||
204 | /* create output */ | ||
205 | plhs[0] = mxCreateSparse(m,n,newnnz,mxREAL); | ||
206 | if (plhs[0]==NULL) | ||
207 | mexErrMsgTxt("Could not allocate enough memory!\n"); | ||
208 | out_pr = mxGetPr(plhs[0]); | ||
209 | out_ir = mxGetIr(plhs[0]); | ||
210 | out_jc = mxGetJc(plhs[0]); | ||
211 | passed = 0; | ||
212 | out_jc[0] = 0; | ||
213 | |||
214 | for (col=0; col<n; col++){ | ||
215 | out_jc[col+1] = out_jc[col]; | ||
216 | for (current_row_index=0; current_row_index<m; current_row_index++){ | ||
217 | if (fabs(in_pr[current_row_index+m*col])>thres){ | ||
218 | out_pr[passed]=in_pr[current_row_index+m*col]; | ||
219 | out_ir[passed]=current_row_index; | ||
220 | out_jc[col+1] = out_jc[col+1]+1; | ||
221 | passed++; | ||
222 | } | ||
223 | } | ||
224 | } | ||
225 | } | ||
226 | else{ | ||
227 | plhs[0] = mxCreateSparse(m,n,0,mxREAL); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/sparsifyc.dll b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.dll new file mode 100755 index 0000000..cf832a6 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexa64 b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexa64 new file mode 100755 index 0000000..2f5ed26 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexglx b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexglx new file mode 100755 index 0000000..0ba41b6 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexmac b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexmac new file mode 100755 index 0000000..caa2306 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/sparsifyc.mexmac | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/spmtimesd.c b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.c new file mode 100755 index 0000000..a98dc0a --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /*================================================================ | ||
2 | * spmtimesd.c | ||
3 | * This routine computes a sparse matrix times a diagonal matrix | ||
4 | * whose diagonal entries are stored in a full vector. | ||
5 | * | ||
6 | * Examples: | ||
7 | * spmtimesd(m,d,[]) = diag(d) * m, | ||
8 | * spmtimesd(m,[],d) = m * diag(d) | ||
9 | * spmtimesd(m,d1,d2) = diag(d1) * m * diag(d2) | ||
10 | * m could be complex, but d is assumed real | ||
11 | * | ||
12 | * Stella X. Yu's first MEX function, Nov 9, 2001. | ||
13 | |||
14 | % test sequence: | ||
15 | |||
16 | m = 1000; | ||
17 | n = 2000; | ||
18 | a=sparse(rand(m,n)); | ||
19 | d1 = rand(m,1); | ||
20 | d2 = rand(n,1); | ||
21 | tic; b=spmtimesd(a,d1,d2); toc | ||
22 | tic; bb = spdiags(d1,0,m,m) * a * spdiags(d2,0,n,n); toc | ||
23 | e = (bb-b); | ||
24 | max(abs(e(:))) | ||
25 | |||
26 | *=================================================================*/ | ||
27 | |||
28 | # include "mex.h" | ||
29 | |||
30 | void mexFunction( | ||
31 | int nargout, | ||
32 | mxArray *out[], | ||
33 | int nargin, | ||
34 | const mxArray *in[] | ||
35 | ) | ||
36 | { | ||
37 | /* declare variables */ | ||
38 | int i, j, k, m, n, nzmax, cmplx, xm, yn; | ||
39 | int *pir, *pjc, *qir, *qjc; | ||
40 | double *x, *y, *pr, *pi, *qr, *qi; | ||
41 | |||
42 | /* check argument */ | ||
43 | if (nargin != 3) { | ||
44 | mexErrMsgTxt("Three input arguments required"); | ||
45 | } | ||
46 | if (nargout>1) { | ||
47 | mexErrMsgTxt("Too many output arguments."); | ||
48 | } | ||
49 | if (!(mxIsSparse(in[0]))) { | ||
50 | mexErrMsgTxt("Input argument #1 must be of type sparse"); | ||
51 | } | ||
52 | if ( mxIsSparse(in[1]) || mxIsSparse(in[2]) ) { | ||
53 | mexErrMsgTxt("Input argument #2 & #3 must be of type full"); | ||
54 | } | ||
55 | |||
56 | /* computation starts */ | ||
57 | m = mxGetM(in[0]); | ||
58 | n = mxGetN(in[0]); | ||
59 | pr = mxGetPr(in[0]); | ||
60 | pi = mxGetPi(in[0]); | ||
61 | pir = mxGetIr(in[0]); | ||
62 | pjc = mxGetJc(in[0]); | ||
63 | |||
64 | i = mxGetM(in[1]); | ||
65 | j = mxGetN(in[1]); | ||
66 | xm = ((i>j)? i: j); | ||
67 | |||
68 | i = mxGetM(in[2]); | ||
69 | j = mxGetN(in[2]); | ||
70 | yn = ((i>j)? i: j); | ||
71 | |||
72 | if ( xm>0 && xm != m) { | ||
73 | mexErrMsgTxt("Row multiplication dimension mismatch."); | ||
74 | } | ||
75 | if ( yn>0 && yn != n) { | ||
76 | mexErrMsgTxt("Column multiplication dimension mismatch."); | ||
77 | } | ||
78 | |||
79 | |||
80 | nzmax = mxGetNzmax(in[0]); | ||
81 | cmplx = (pi==NULL ? 0 : 1); | ||
82 | out[0] = mxCreateSparse(m,n,nzmax,cmplx); | ||
83 | if (out[0]==NULL) { | ||
84 | mexErrMsgTxt("Not enough space for the output matrix."); | ||
85 | } | ||
86 | |||
87 | qr = mxGetPr(out[0]); | ||
88 | qi = mxGetPi(out[0]); | ||
89 | qir = mxGetIr(out[0]); | ||
90 | qjc = mxGetJc(out[0]); | ||
91 | |||
92 | /* left multiplication */ | ||
93 | x = mxGetPr(in[1]); | ||
94 | if (yn==0) { | ||
95 | for (j=0; j<n; j++) { | ||
96 | qjc[j] = pjc[j]; | ||
97 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
98 | i = pir[k]; | ||
99 | qir[k] = i; | ||
100 | qr[k] = x[i] * pr[k]; | ||
101 | if (cmplx) { | ||
102 | qi[k] = x[i] * pi[k]; | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | qjc[n] = k; | ||
107 | return; | ||
108 | } | ||
109 | |||
110 | /* right multiplication */ | ||
111 | y = mxGetPr(in[2]); | ||
112 | if (xm==0) { | ||
113 | for (j=0; j<n; j++) { | ||
114 | qjc[j] = pjc[j]; | ||
115 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
116 | qir[k] = pir[k]; | ||
117 | qr[k] = pr[k] * y[j]; | ||
118 | if (cmplx) { | ||
119 | qi[k] = qi[k] * y[j]; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | qjc[n] = k; | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | /* both sides */ | ||
128 | for (j=0; j<n; j++) { | ||
129 | qjc[j] = pjc[j]; | ||
130 | for (k=pjc[j]; k<pjc[j+1]; k++) { | ||
131 | i = pir[k]; | ||
132 | qir[k]= i; | ||
133 | qr[k] = x[i] * pr[k] * y[j]; | ||
134 | if (cmplx) { | ||
135 | qi[k] = x[i] * qi[k] * y[j]; | ||
136 | } | ||
137 | } | ||
138 | qjc[n] = k; | ||
139 | } | ||
140 | |||
141 | } | ||
diff --git a/SD-VBS/common/toolbox/MultiNcut/spmtimesd.dll b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.dll new file mode 100755 index 0000000..f78a650 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.dll | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexa64 b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexa64 new file mode 100755 index 0000000..43c59e3 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexa64 | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexglx b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexglx new file mode 100755 index 0000000..5bc9ec4 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexglx | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexmac b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexmac new file mode 100755 index 0000000..014113f --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/spmtimesd.mexmac | |||
Binary files differ | |||
diff --git a/SD-VBS/common/toolbox/MultiNcut/tim_eigs.m b/SD-VBS/common/toolbox/MultiNcut/tim_eigs.m new file mode 100755 index 0000000..4b80262 --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/tim_eigs.m | |||
@@ -0,0 +1,1084 @@ | |||
1 | function varargout = tim_eigs(varargin) | ||
2 | |||
3 | nombre_A_times_X = 0; %tim | ||
4 | nombreIterations = 0; %tim | ||
5 | |||
6 | %seule difference avec eigs : | ||
7 | % arguments_Afun = varargin{7-Amatrix-Bnotthere:end}; | ||
8 | %(pour l'instant : n'accepte que 2 arguments dans le cas de Afun : Afun(W,X)) | ||
9 | %permet d'aller plus vite en minimisant les acces a varargin | ||
10 | %(Timothee) | ||
11 | |||
12 | %EIGS Find a few eigenvalues and eigenvectors of a matrix using ARPACK. | ||
13 | % D = EIGS(A) returns a vector of A's 6 largest magnitude eigenvalues. | ||
14 | % A must be square and should be large and sparse. | ||
15 | % | ||
16 | % [V,D] = EIGS(A) returns a diagonal matrix D of A's 6 largest magnitude | ||
17 | % eigenvalues and a matrix V whose columns are the corresponding eigenvectors. | ||
18 | % | ||
19 | % [V,D,FLAG] = EIGS(A) also returns a convergence flag. If FLAG is 0 | ||
20 | % then all the eigenvalues converged; otherwise not all converged. | ||
21 | % | ||
22 | % EIGS(A,B) solves the generalized eigenvalue problem A*V == B*V*D. B must | ||
23 | % be symmetric (or Hermitian) positive definite and the same size as A. | ||
24 | % EIGS(A,[],...) indicates the standard eigenvalue problem A*V == V*D. | ||
25 | % | ||
26 | % EIGS(A,K) and EIGS(A,B,K) return the K largest magnitude eigenvalues. | ||
27 | % | ||
28 | % EIGS(A,K,SIGMA) and EIGS(A,B,K,SIGMA) return K eigenvalues based on SIGMA: | ||
29 | % 'LM' or 'SM' - Largest or Smallest Magnitude | ||
30 | % For real symmetric problems, SIGMA may also be: | ||
31 | % 'LA' or 'SA' - Largest or Smallest Algebraic | ||
32 | % 'BE' - Both Ends, one more from high end if K is odd | ||
33 | % For nonsymmetric and complex problems, SIGMA may also be: | ||
34 | % 'LR' or 'SR' - Largest or Smallest Real part | ||
35 | % 'LI' or 'SI' - Largest or Smallest Imaginary part | ||
36 | % If SIGMA is a real or complex scalar including 0, EIGS finds the eigenvalues | ||
37 | % closest to SIGMA. For scalar SIGMA, and also when SIGMA = 'SM' which uses | ||
38 | % the same algorithm as SIGMA = 0, B need only be symmetric (or Hermitian) | ||
39 | % positive semi-definite since it is not Cholesky factored as in the other cases. | ||
40 | % | ||
41 | % EIGS(A,K,SIGMA,OPTS) and EIGS(A,B,K,SIGMA,OPTS) specify options: | ||
42 | % OPTS.issym: symmetry of A or A-SIGMA*B represented by AFUN [{0} | 1] | ||
43 | % OPTS.isreal: complexity of A or A-SIGMA*B represented by AFUN [0 | {1}] | ||
44 | % OPTS.tol: convergence: Ritz estimate residual <= tol*NORM(A) [scalar | {eps}] | ||
45 | % OPTS.maxit: maximum number of iterations [integer | {300}] | ||
46 | % OPTS.p: number of Lanczos vectors: K+1<p<=N [integer | {2K}] | ||
47 | % OPTS.v0: starting vector [N-by-1 vector | {randomly generated by ARPACK}] | ||
48 | % OPTS.disp: diagnostic information display level [0 | {1} | 2] | ||
49 | % OPTS.cholB: B is actually its Cholesky factor CHOL(B) [{0} | 1] | ||
50 | % OPTS.permB: sparse B is actually CHOL(B(permB,permB)) [permB | {1:N}] | ||
51 | % | ||
52 | % EIGS(AFUN,N) accepts the function AFUN instead of the matrix A. | ||
53 | % Y = AFUN(X) should return | ||
54 | % A*X if SIGMA is not specified, or is a string other than 'SM' | ||
55 | % A\X if SIGMA is 0 or 'SM' | ||
56 | % (A-SIGMA*I)\X if SIGMA is a nonzero scalar (standard eigenvalue problem) | ||
57 | % (A-SIGMA*B)\X if SIGMA is a nonzero scalar (generalized eigenvalue problem) | ||
58 | % N is the size of A. The matrix A, A-SIGMA*I or A-SIGMA*B represented by AFUN is | ||
59 | % assumed to be real and nonsymmetric unless specified otherwise by OPTS.isreal | ||
60 | % and OPTS.issym. In all these EIGS syntaxes, EIGS(A,...) may be replaced by | ||
61 | % EIGS(AFUN,N,...). | ||
62 | % | ||
63 | % EIGS(AFUN,N,K,SIGMA,OPTS,P1,P2,...) and EIGS(AFUN,N,B,K,SIGMA,OPTS,P1,P2,...) | ||
64 | % provide for additional arguments which are passed to AFUN(X,P1,P2,...). | ||
65 | % | ||
66 | % Examples: | ||
67 | % A = delsq(numgrid('C',15)); d1 = eigs(A,5,'SM'); | ||
68 | % Equivalently, if dnRk is the following one-line function: | ||
69 | % function y = dnRk(x,R,k) | ||
70 | % y = (delsq(numgrid(R,k))) \ x; | ||
71 | % then pass dnRk's additional arguments, 'C' and 15, to EIGS: | ||
72 | % n = size(A,1); opts.issym = 1; d2 = eigs(@dnRk,n,5,'SM',opts,'C',15); | ||
73 | % | ||
74 | % See also EIG, SVDS, ARPACKC. | ||
75 | |||
76 | % Copyright 1984-2002 The MathWorks, Inc. | ||
77 | % $Revision: 1.45 $ $Date: 2002/05/14 18:50:58 $ | ||
78 | |||
79 | cputms = zeros(5,1); | ||
80 | t0 = cputime; % start timing pre-processing | ||
81 | |||
82 | if (nargout > 3) | ||
83 | error('Too many output arguments.') | ||
84 | end | ||
85 | |||
86 | % Process inputs and do error-checking | ||
87 | if isa(varargin{1},'double') | ||
88 | A = varargin{1}; | ||
89 | Amatrix = 1; | ||
90 | else | ||
91 | A = fcnchk(varargin{1}); | ||
92 | Amatrix = 0; | ||
93 | end | ||
94 | |||
95 | isrealprob = 1; % isrealprob = isreal(A) & isreal(B) & isreal(sigma) | ||
96 | if Amatrix | ||
97 | isrealprob = isreal(A); | ||
98 | end | ||
99 | |||
100 | issymA = 0; | ||
101 | if Amatrix | ||
102 | issymA = isequal(A,A'); | ||
103 | end | ||
104 | |||
105 | if Amatrix | ||
106 | [m,n] = size(A); | ||
107 | if (m ~= n) | ||
108 | error('A must be a square matrix or a function which computes A*x.') | ||
109 | end | ||
110 | else | ||
111 | n = varargin{2}; | ||
112 | nstr = 'Size of problem, ''n'', must be a positive integer.'; | ||
113 | if ~isequal(size(n),[1,1]) | ~isreal(n) | ||
114 | error(nstr) | ||
115 | end | ||
116 | if (round(n) ~= n) | ||
117 | warning('MATLAB:eigs:NonIntegerSize',['%s\n ' ... | ||
118 | 'Rounding input size.'],nstr) | ||
119 | n = round(n); | ||
120 | end | ||
121 | if issparse(n) | ||
122 | n = full(n); | ||
123 | end | ||
124 | end | ||
125 | |||
126 | Bnotthere = 0; | ||
127 | Bstr = sprintf(['Generalized matrix B must be the same size as A and' ... | ||
128 | ' either a symmetric positive (semi-)definite matrix or' ... | ||
129 | ' its Cholesky factor.']); | ||
130 | if (nargin < (3-Amatrix-Bnotthere)) | ||
131 | B = []; | ||
132 | Bnotthere = 1; | ||
133 | else | ||
134 | Bk = varargin{3-Amatrix-Bnotthere}; | ||
135 | if isempty(Bk) % allow eigs(A,[],k,sigma,opts); | ||
136 | B = Bk; | ||
137 | else | ||
138 | if isequal(size(Bk),[1,1]) & (n ~= 1) | ||
139 | B = []; | ||
140 | k = Bk; | ||
141 | Bnotthere = 1; | ||
142 | else % eigs(9,8,...) assumes A=9, B=8, ... NOT A=9, k=8, ... | ||
143 | B = Bk; | ||
144 | if ~isa(B,'double') | ~isequal(size(B),[n,n]) | ||
145 | error(Bstr) | ||
146 | end | ||
147 | isrealprob = isrealprob & isreal(B); | ||
148 | end | ||
149 | end | ||
150 | end | ||
151 | |||
152 | if Amatrix & ((nargin - ~Bnotthere)>4) | ||
153 | error('Too many inputs.') | ||
154 | end | ||
155 | |||
156 | if (nargin < (4-Amatrix-Bnotthere)) | ||
157 | k = min(n,6); | ||
158 | else | ||
159 | k = varargin{4-Amatrix-Bnotthere}; | ||
160 | end | ||
161 | |||
162 | kstr = ['Number of eigenvalues requested, k, must be a' ... | ||
163 | ' positive integer <= n.']; | ||
164 | if ~isa(k,'double') | ~isequal(size(k),[1,1]) | ~isreal(k) | (k>n) | ||
165 | error(kstr) | ||
166 | end | ||
167 | if issparse(k) | ||
168 | k = full(k); | ||
169 | end | ||
170 | if (round(k) ~= k) | ||
171 | warning('MATLAB:eigs:NonIntegerEigQty',['%s\n ' ... | ||
172 | 'Rounding number of eigenvalues.'],kstr) | ||
173 | k = round(k); | ||
174 | end | ||
175 | |||
176 | whchstr = sprintf(['Eigenvalue range sigma must be a valid 2-element string.']); | ||
177 | if (nargin < (5-Amatrix-Bnotthere)) | ||
178 | % default: eigs('LM') => ARPACK which='LM', sigma=0 | ||
179 | eigs_sigma = 'LM'; | ||
180 | whch = 'LM'; | ||
181 | sigma = 0; | ||
182 | else | ||
183 | eigs_sigma = varargin{5-Amatrix-Bnotthere}; | ||
184 | if isstr(eigs_sigma) | ||
185 | % eigs(string) => ARPACK which=string, sigma=0 | ||
186 | if ~isequal(size(eigs_sigma),[1,2]) | ||
187 | whchstr = [whchstr sprintf(['\nFor real symmetric A, the choices are ''%s'', ''%s'', ''%s'', ''%s'' or ''%s''.'], ... | ||
188 | 'LM','SM','LA','SA','BE')]; | ||
189 | whchstr = [whchstr sprintf(['\nFor non-symmetric or complex A, the choices are ''%s'', ''%s'', ''%s'', ''%s'', ''%s'' or ''%s''.\n'], ... | ||
190 | 'LM','SM','LR','SR','LI','SI')]; | ||
191 | error(whchstr) | ||
192 | end | ||
193 | eigs_sigma = upper(eigs_sigma); | ||
194 | if isequal(eigs_sigma,'SM') | ||
195 | % eigs('SM') => ARPACK which='LM', sigma=0 | ||
196 | whch = 'LM'; | ||
197 | else | ||
198 | % eigs(string), where string~='SM' => ARPACK which=string, sigma=0 | ||
199 | whch = eigs_sigma; | ||
200 | end | ||
201 | sigma = 0; | ||
202 | else | ||
203 | % eigs(scalar) => ARPACK which='LM', sigma=scalar | ||
204 | if ~isa(eigs_sigma,'double') | ~isequal(size(eigs_sigma),[1,1]) | ||
205 | error('Eigenvalue shift sigma must be a scalar.') | ||
206 | end | ||
207 | sigma = eigs_sigma; | ||
208 | if issparse(sigma) | ||
209 | sigma = full(sigma); | ||
210 | end | ||
211 | isrealprob = isrealprob & isreal(sigma); | ||
212 | whch = 'LM'; | ||
213 | end | ||
214 | end | ||
215 | |||
216 | tol = eps; % ARPACK's minimum tolerance is eps/2 (DLAMCH's EPS) | ||
217 | maxit = []; | ||
218 | p = []; | ||
219 | info = int32(0); % use a random starting vector | ||
220 | display = 1; | ||
221 | cholB = 0; | ||
222 | |||
223 | if (nargin >= (6-Amatrix-Bnotthere)) | ||
224 | opts = varargin{6-Amatrix-Bnotthere}; | ||
225 | if ~isa(opts,'struct') | ||
226 | error('Options argument must be a structure.') | ||
227 | end | ||
228 | |||
229 | if isfield(opts,'issym') & ~Amatrix | ||
230 | issymA = opts.issym; | ||
231 | if (issymA ~= 0) & (issymA ~= 1) | ||
232 | error('opts.issym must be 0 or 1.') | ||
233 | end | ||
234 | end | ||
235 | |||
236 | if isfield(opts,'isreal') & ~Amatrix | ||
237 | if (opts.isreal ~= 0) & (opts.isreal ~= 1) | ||
238 | error('opts.isreal must be 0 or 1.') | ||
239 | end | ||
240 | isrealprob = isrealprob & opts.isreal; | ||
241 | end | ||
242 | |||
243 | if ~isempty(B) & (isfield(opts,'cholB') | isfield(opts,'permB')) | ||
244 | if isfield(opts,'cholB') | ||
245 | cholB = opts.cholB; | ||
246 | if (cholB ~= 0) & (cholB ~= 1) | ||
247 | error('opts.cholB must be 0 or 1.') | ||
248 | end | ||
249 | if isfield(opts,'permB') | ||
250 | if issparse(B) & cholB | ||
251 | permB = opts.permB; | ||
252 | if ~isequal(sort(permB),(1:n)) & ... | ||
253 | ~isequal(sort(permB),(1:n)') | ||
254 | error('opts.permB must be a permutation of 1:n.') | ||
255 | end | ||
256 | else | ||
257 | warning('MATLAB:eigs:IgnoredOptionPermB', ... | ||
258 | ['Ignoring opts.permB since B is not its sparse' ... | ||
259 | ' Cholesky factor.']) | ||
260 | end | ||
261 | else | ||
262 | permB = 1:n; | ||
263 | end | ||
264 | end | ||
265 | end | ||
266 | |||
267 | if isfield(opts,'tol') | ||
268 | if ~isequal(size(opts.tol),[1,1]) | ~isreal(opts.tol) | (opts.tol<=0) | ||
269 | error(['Convergence tolerance opts.tol must be a strictly' ... | ||
270 | ' positive real scalar.']) | ||
271 | else | ||
272 | tol = full(opts.tol); | ||
273 | end | ||
274 | end | ||
275 | |||
276 | if isfield(opts,'p') | ||
277 | p = opts.p; | ||
278 | pstr = ['Number of basis vectors opts.p must be a positive' ... | ||
279 | ' integer <= n.']; | ||
280 | if ~isequal(size(p),[1,1]) | ~isreal(p) | (p<=0) | (p>n) | ||
281 | error(pstr) | ||
282 | end | ||
283 | if issparse(p) | ||
284 | p = full(p); | ||
285 | end | ||
286 | if (round(p) ~= p) | ||
287 | warning('MATLAB:eigs:NonIntegerVecQty',['%s\n ' ... | ||
288 | 'Rounding number of basis vectors.'],pstr) | ||
289 | p = round(p); | ||
290 | end | ||
291 | end | ||
292 | |||
293 | if isfield(opts,'maxit') | ||
294 | maxit = opts.maxit; | ||
295 | str = ['Maximum number of iterations opts.maxit must be' ... | ||
296 | ' a positive integer.']; | ||
297 | if ~isequal(size(maxit),[1,1]) | ~isreal(maxit) | (maxit<=0) | ||
298 | error(str) | ||
299 | end | ||
300 | if issparse(maxit) | ||
301 | maxit = full(maxit); | ||
302 | end | ||
303 | if (round(maxit) ~= maxit) | ||
304 | warning('MATLAB:eigs:NonIntegerIterationQty',['%s\n ' ... | ||
305 | 'Rounding number of iterations.'],str) | ||
306 | maxit = round(maxit); | ||
307 | end | ||
308 | end | ||
309 | |||
310 | if isfield(opts,'v0') | ||
311 | if ~isequal(size(opts.v0),[n,1]) | ||
312 | error('Start vector opts.v0 must be n-by-1.') | ||
313 | end | ||
314 | if isrealprob | ||
315 | if ~isreal(opts.v0) | ||
316 | error(['Start vector opts.v0 must be real for real problems.']) | ||
317 | end | ||
318 | resid = full(opts.v0); | ||
319 | else | ||
320 | resid(1:2:(2*n-1),1) = full(real(opts.v0)); | ||
321 | resid(2:2:2*n,1) = full(imag(opts.v0)); | ||
322 | end | ||
323 | info = int32(1); % use resid as starting vector | ||
324 | end | ||
325 | |||
326 | if isfield(opts,'disp') | ||
327 | display = opts.disp; | ||
328 | dispstr = 'Diagnostic level opts.disp must be an integer.'; | ||
329 | if (~isequal(size(display),[1,1])) | (~isreal(display)) | (display<0) | ||
330 | error(dispstr) | ||
331 | end | ||
332 | if (round(display) ~= display) | ||
333 | warning('MATLAB:eigs:NonIntegerDiagnosticLevel', ... | ||
334 | '%s\n Rounding diagnostic level.',dispstr) | ||
335 | display = round(display); | ||
336 | end | ||
337 | end | ||
338 | |||
339 | if isfield(opts,'cheb') | ||
340 | warning('MATLAB:eigs:ObsoleteOptionCheb', ... | ||
341 | ['Ignoring polynomial acceleration opts.cheb' ... | ||
342 | ' (no longer an option).']); | ||
343 | end | ||
344 | if isfield(opts,'stagtol') | ||
345 | warning('MATLAB:eigs:ObsoleteOptionStagtol', ... | ||
346 | ['Ignoring stagnation tolerance opts.stagtol' ... | ||
347 | ' (no longer an option).']); | ||
348 | end | ||
349 | |||
350 | end | ||
351 | |||
352 | % Now we know issymA, isrealprob, cholB, and permB | ||
353 | |||
354 | if isempty(p) | ||
355 | if isrealprob & ~issymA | ||
356 | p = min(max(2*k+1,20),n); | ||
357 | else | ||
358 | p = min(max(2*k,20),n); | ||
359 | end | ||
360 | end | ||
361 | if isempty(maxit) | ||
362 | maxit = max(300,ceil(2*n/max(p,1))); | ||
363 | end | ||
364 | if (info == int32(0)) | ||
365 | if isrealprob | ||
366 | resid = zeros(n,1); | ||
367 | else | ||
368 | resid = zeros(2*n,1); | ||
369 | end | ||
370 | end | ||
371 | |||
372 | if ~isempty(B) % B must be symmetric (Hermitian) positive (semi-)definite | ||
373 | if cholB | ||
374 | if ~isequal(triu(B),B) | ||
375 | error(Bstr) | ||
376 | end | ||
377 | else | ||
378 | if ~isequal(B,B') | ||
379 | error(Bstr) | ||
380 | end | ||
381 | end | ||
382 | end | ||
383 | |||
384 | useeig = 0; | ||
385 | if isrealprob & issymA | ||
386 | knstr = sprintf(['For real symmetric problems, must have number' ... | ||
387 | ' of eigenvalues k < n.\n']); | ||
388 | else | ||
389 | knstr = sprintf(['For nonsymmetric and complex problems, must have' ... | ||
390 | ' number of eigenvalues k < n-1.\n']); | ||
391 | end | ||
392 | if isempty(B) | ||
393 | knstr = [knstr sprintf(['Using eig(full(A)) instead.'])]; | ||
394 | else | ||
395 | knstr = [knstr sprintf(['Using eig(full(A),full(B)) instead.'])]; | ||
396 | end | ||
397 | if (k == 0) | ||
398 | useeig = 1; | ||
399 | end | ||
400 | if isrealprob & issymA | ||
401 | if (k > n-1) | ||
402 | if (n >= 6) | ||
403 | warning('MATLAB:eigs:TooManyRequestedEigsForRealSym', ... | ||
404 | '%s',knstr) | ||
405 | end | ||
406 | useeig = 1; | ||
407 | end | ||
408 | else | ||
409 | if (k > n-2) | ||
410 | if (n >= 7) | ||
411 | warning('MATLAB:eigs:TooManyRequestedEigsForComplexNonsym', ... | ||
412 | '%s',knstr) | ||
413 | end | ||
414 | useeig = 1; | ||
415 | end | ||
416 | end | ||
417 | |||
418 | if isrealprob & issymA | ||
419 | if ~isreal(sigma) | ||
420 | error(['For real symmetric problems, eigenvalue shift sigma must' ... | ||
421 | ' be real.']) | ||
422 | end | ||
423 | else | ||
424 | if ~isrealprob & issymA & ~isreal(sigma) | ||
425 | warning('MATLAB:eigs:ComplexShiftForRealProblem', ... | ||
426 | ['Complex eigenvalue shift sigma on a Hermitian problem' ... | ||
427 | ' (all real eigenvalues).']) | ||
428 | end | ||
429 | end | ||
430 | |||
431 | if isrealprob & issymA | ||
432 | if strcmp(whch,'LR') | ||
433 | whch = 'LA'; | ||
434 | warning('MATLAB:eigs:SigmaChangedToLA', ... | ||
435 | ['For real symmetric problems, sigma value ''LR''' ... | ||
436 | ' (Largest Real) is now ''LA'' (Largest Algebraic).']) | ||
437 | end | ||
438 | if strcmp(whch,'SR') | ||
439 | whch = 'SA'; | ||
440 | warning('MATLAB:eigs:SigmaChangedToSA', ... | ||
441 | ['For real symmetric problems, sigma value ''SR''' ... | ||
442 | ' (Smallest Real) is now ''SA'' (Smallest Algebraic).']) | ||
443 | end | ||
444 | if ~ismember(whch,{'LM', 'SM', 'LA', 'SA', 'BE'}) | ||
445 | whchstr = [whchstr sprintf(['\nFor real symmetric A, the choices are ''%s'', ''%s'', ''%s'', ''%s'' or ''%s''.'], ... | ||
446 | 'LM','SM','LA','SA','BE')]; | ||
447 | error(whchstr) | ||
448 | end | ||
449 | else | ||
450 | if strcmp(whch,'BE') | ||
451 | warning('MATLAB:eigs:SigmaChangedToLM', ... | ||
452 | ['Sigma value ''BE'' is now only available for real' ... | ||
453 | ' symmetric problems. Computing ''LM'' eigenvalues instead.']) | ||
454 | whch = 'LM'; | ||
455 | end | ||
456 | if ~ismember(whch,{'LM', 'SM', 'LR', 'SR', 'LI', 'SI'}) | ||
457 | whchstr = [whchstr sprintf(['\nFor non-symmetric or complex A, the choices are ''%s'', ''%s'', ''%s'', ''%s'', ''%s'' or ''%s''.\n'], ... | ||
458 | 'LM','SM','LR','SR','LI','SI')]; | ||
459 | error(whchstr) | ||
460 | end | ||
461 | end | ||
462 | |||
463 | % Now have enough information to do early return on cases eigs does not handle | ||
464 | if useeig | ||
465 | if (nargout <= 1) | ||
466 | varargout{1} = eigs2(A,n,B,k,whch,sigma,cholB, ... | ||
467 | varargin{7-Amatrix-Bnotthere:end}); | ||
468 | else | ||
469 | [varargout{1},varargout{2}] = eigs2(A,n,B,k,whch,sigma,cholB, ... | ||
470 | varargin{7-Amatrix-Bnotthere:end}); | ||
471 | end | ||
472 | if (nargout == 3) | ||
473 | varargout{3} = 0; % flag indicates "convergence" | ||
474 | end | ||
475 | return | ||
476 | end | ||
477 | |||
478 | if isrealprob & ~issymA | ||
479 | sigmar = real(sigma); | ||
480 | sigmai = imag(sigma); | ||
481 | end | ||
482 | |||
483 | if isrealprob & issymA | ||
484 | if (p <= k) | ||
485 | error(['For real symmetric problems, must have number of' ... | ||
486 | ' basis vectors opts.p > k.']) | ||
487 | end | ||
488 | else | ||
489 | if (p <= k+1) | ||
490 | error(['For nonsymmetric and complex problems, must have number of' ... | ||
491 | ' basis vectors opts.p > k+1.']) | ||
492 | end | ||
493 | end | ||
494 | |||
495 | if isequal(whch,'LM') & ~isequal(eigs_sigma,'LM') | ||
496 | % A*x = lambda*M*x, M symmetric (positive) semi-definite | ||
497 | % => OP = inv(A - sigma*M)*M and B = M | ||
498 | % => shift-and-invert mode | ||
499 | mode = 3; | ||
500 | elseif isempty(B) | ||
501 | % A*x = lambda*x | ||
502 | % => OP = A and B = I | ||
503 | mode = 1; | ||
504 | else % B is not empty | ||
505 | % Do not use mode=2. | ||
506 | % Use mode = 1 with OP = R'\(A*(R\x)) and B = I | ||
507 | % where R is B's upper triangular Cholesky factor: B = R'*R. | ||
508 | % Finally, V = R\V returns the actual generalized eigenvectors of A and B. | ||
509 | mode = 1; | ||
510 | end | ||
511 | |||
512 | if cholB | ||
513 | pB = 0; | ||
514 | RB = B; | ||
515 | RBT = B'; | ||
516 | end | ||
517 | |||
518 | if (mode == 3) & Amatrix % need lu(A-sigma*B) | ||
519 | if issparse(A) & (isempty(B) | issparse(B)) | ||
520 | if isempty(B) | ||
521 | AsB = A - sigma * speye(n); | ||
522 | else | ||
523 | if cholB | ||
524 | AsB = A - sigma * RBT * RB; | ||
525 | else | ||
526 | AsB = A - sigma * B; | ||
527 | end | ||
528 | end | ||
529 | [L,U,P,Q] = lu(AsB); | ||
530 | [perm,dummy] = find(Q); | ||
531 | else | ||
532 | if isempty(B) | ||
533 | AsB = A - sigma * eye(n); | ||
534 | else | ||
535 | if cholB | ||
536 | AsB = A - sigma * RBT * RB; | ||
537 | else | ||
538 | AsB = A - sigma * B; | ||
539 | end | ||
540 | end | ||
541 | [L,U,P] = lu(AsB); | ||
542 | end | ||
543 | dU = diag(U); | ||
544 | rcondestU = full(min(abs(dU)) / max(abs(dU))); | ||
545 | if (rcondestU < eps) | ||
546 | if isempty(B) | ||
547 | ds = sprintf(['(A-sigma*I) has small reciprocal condition' ... | ||
548 | ' estimate: %f\n'],rcondestU); | ||
549 | else | ||
550 | ds = sprintf(['(A-sigma*B) has small reciprocal condition' ... | ||
551 | ' estimate: %f\n'],rcondestU); | ||
552 | end | ||
553 | ds = [ds sprintf(['indicating that sigma is near an exact' ... | ||
554 | ' eigenvalue. The\nalgorithm may not converge unless' ... | ||
555 | ' you try a new value for sigma.\n'])]; | ||
556 | warning('MATLAB:eigs:SigmaNearExactEig',ds) | ||
557 | end | ||
558 | end | ||
559 | |||
560 | if (mode == 1) & ~isempty(B) & ~cholB % need chol(B) | ||
561 | if issparse(B) | ||
562 | permB = symamd(B); | ||
563 | [RB,pB] = chol(B(permB,permB)); | ||
564 | else | ||
565 | [RB,pB] = chol(B); | ||
566 | end | ||
567 | if (pB == 0) | ||
568 | RBT = RB'; | ||
569 | else | ||
570 | error(Bstr) | ||
571 | end | ||
572 | end | ||
573 | |||
574 | % Allocate outputs and ARPACK work variables | ||
575 | if isrealprob | ||
576 | if issymA % real and symmetric | ||
577 | prefix = 'ds'; | ||
578 | v = zeros(n,p); | ||
579 | ldv = int32(size(v,1)); | ||
580 | ipntr = int32(zeros(15,1)); | ||
581 | workd = zeros(n,3); | ||
582 | lworkl = p*(p+8); | ||
583 | workl = zeros(lworkl,1); | ||
584 | lworkl = int32(lworkl); | ||
585 | d = zeros(k,1); | ||
586 | else % real but not symmetric | ||
587 | prefix = 'dn'; | ||
588 | v = zeros(n,p); | ||
589 | ldv = int32(size(v,1)); | ||
590 | ipntr = int32(zeros(15,1)); | ||
591 | workd = zeros(n,3); | ||
592 | lworkl = 3*p*(p+2); | ||
593 | workl = zeros(lworkl,1); | ||
594 | lworkl = int32(lworkl); | ||
595 | workev = zeros(3*p,1); | ||
596 | d = zeros(k+1,1); | ||
597 | di = zeros(k+1,1); | ||
598 | end | ||
599 | else % complex | ||
600 | prefix = 'zn'; | ||
601 | zv = zeros(2*n*p,1); | ||
602 | ldv = int32(n); | ||
603 | ipntr = int32(zeros(15,1)); | ||
604 | workd = complex(zeros(n,3)); | ||
605 | zworkd = zeros(2*prod(size(workd)),1); | ||
606 | lworkl = 3*p^2+5*p; | ||
607 | workl = zeros(2*lworkl,1); | ||
608 | lworkl = int32(lworkl); | ||
609 | workev = zeros(2*2*p,1); | ||
610 | zd = zeros(2*(k+1),1); | ||
611 | rwork = zeros(p,1); | ||
612 | end | ||
613 | |||
614 | ido = int32(0); % reverse communication parameter | ||
615 | if isempty(B) | (~isempty(B) & (mode == 1)) | ||
616 | bmat = 'I'; % standard eigenvalue problem | ||
617 | else | ||
618 | bmat = 'G'; % generalized eigenvalue problem | ||
619 | end | ||
620 | nev = int32(k); % number of eigenvalues requested | ||
621 | ncv = int32(p); % number of Lanczos vectors | ||
622 | iparam = int32(zeros(11,1)); | ||
623 | iparam([1 3 7]) = int32([1 maxit mode]); | ||
624 | select = int32(zeros(p,1)); | ||
625 | |||
626 | cputms(1) = cputime - t0; % end timing pre-processing | ||
627 | |||
628 | iter = 0; | ||
629 | ariter = 0; | ||
630 | |||
631 | |||
632 | %tim | ||
633 | |||
634 | |||
635 | indexArgumentsAfun = 7-Amatrix-Bnotthere:length(varargin); | ||
636 | nbArgumentsAfun = length(indexArgumentsAfun); | ||
637 | if nbArgumentsAfun >=1 | ||
638 | arguments_Afun = varargin{7-Amatrix-Bnotthere}; | ||
639 | end | ||
640 | if nbArgumentsAfun >=2 | ||
641 | arguments_Afun2 = varargin{7-Amatrix-Bnotthere+1}; | ||
642 | end | ||
643 | if nbArgumentsAfun >=3 | ||
644 | arguments_Afun3 = varargin{7-Amatrix-Bnotthere+2}; | ||
645 | end | ||
646 | %fin tim | ||
647 | |||
648 | |||
649 | |||
650 | while (ido ~= 99) | ||
651 | |||
652 | t0 = cputime; % start timing ARPACK calls **aupd | ||
653 | |||
654 | if isrealprob | ||
655 | arpackc( [prefix 'aupd'], ido, ... | ||
656 | bmat, int32(n), whch, nev, tol, resid, ncv, ... | ||
657 | v, ldv, iparam, ipntr, workd, workl, lworkl, info); | ||
658 | else | ||
659 | zworkd(1:2:end-1) = real(workd); | ||
660 | zworkd(2:2:end) = imag(workd); | ||
661 | arpackc( 'znaupd', ido, ... | ||
662 | bmat, int32(n), whch, nev, tol, resid, ncv, zv, ... | ||
663 | ldv, iparam, ipntr, zworkd, workl, lworkl, ... | ||
664 | rwork, info ); | ||
665 | workd = reshape(complex(zworkd(1:2:end-1),zworkd(2:2:end)),[n,3]); | ||
666 | end | ||
667 | |||
668 | if (info < 0) | ||
669 | es = sprintf('Error with ARPACK routine %saupd: info = %d',... | ||
670 | prefix,full(info)); | ||
671 | error(es) | ||
672 | end | ||
673 | |||
674 | cputms(2) = cputms(2) + (cputime-t0); % end timing ARPACK calls **aupd | ||
675 | t0 = cputime; % start timing MATLAB OP(X) | ||
676 | |||
677 | % Compute which columns of workd ipntr references | ||
678 | |||
679 | |||
680 | |||
681 | |||
682 | |||
683 | %[row,col1] = ind2sub([n,3],double(ipntr(1))); | ||
684 | %tim | ||
685 | row = mod(double(ipntr(1))-1,n) + 1 ; | ||
686 | col1 = floor((double(ipntr(1))-1)/n) + 1; | ||
687 | |||
688 | |||
689 | if (row ~= 1) | ||
690 | str = sprintf(['ipntr(1)=%d does not refer to the start of a' ... | ||
691 | ' column of the %d-by-3 array workd.'],full(ipntr(1)),n); | ||
692 | error(str) | ||
693 | end | ||
694 | |||
695 | |||
696 | |||
697 | %[row,col2] = ind2sub([n,3],double(ipntr(2))); | ||
698 | %tim | ||
699 | row = mod(double(ipntr(2))-1,n) + 1 ; | ||
700 | col2 = floor((double(ipntr(2))-1)/n) + 1; | ||
701 | |||
702 | |||
703 | |||
704 | if (row ~= 1) | ||
705 | str = sprintf(['ipntr(2)=%d does not refer to the start of a' ... | ||
706 | ' column of the %d-by-3 array workd.'],full(ipntr(2)),n); | ||
707 | error(str) | ||
708 | end | ||
709 | if ~isempty(B) & (mode == 3) & (ido == 1) | ||
710 | [row,col3] = ind2sub([n,3],double(ipntr(3))); | ||
711 | if (row ~= 1) | ||
712 | str = sprintf(['ipntr(3)=%d does not refer to the start of a' ... | ||
713 | ' column of the %d-by-3 array workd.'],full(ipntr(3)),n); | ||
714 | error(str) | ||
715 | end | ||
716 | end | ||
717 | |||
718 | switch (ido) | ||
719 | case {-1,1} | ||
720 | if Amatrix | ||
721 | if (mode == 1) | ||
722 | if isempty(B) | ||
723 | % OP = A*x | ||
724 | workd(:,col2) = A * workd(:,col1); | ||
725 | else | ||
726 | % OP = R'\(A*(R\x)) | ||
727 | if issparse(B) | ||
728 | workd(permB,col2) = RB \ workd(:,col1); | ||
729 | workd(:,col2) = A * workd(:,col2); | ||
730 | workd(:,col2) = RBT \ workd(permB,col2); | ||
731 | else | ||
732 | workd(:,col2) = RBT \ (A * (RB \ workd(:,col1))); | ||
733 | end | ||
734 | end | ||
735 | elseif (mode == 3) | ||
736 | if isempty(B) | ||
737 | if issparse(A) | ||
738 | workd(perm,col2) = U \ (L \ (P * workd(:,col1))); | ||
739 | else | ||
740 | workd(:,col2) = U \ (L \ (P * workd(:,col1))); | ||
741 | end | ||
742 | else % B is not empty | ||
743 | if (ido == -1) | ||
744 | if cholB | ||
745 | workd(:,col2) = RBT * (RB * workd(:,col1)); | ||
746 | else | ||
747 | workd(:,col2) = B * workd(:,col1); | ||
748 | end | ||
749 | if issparse(A) & issparse(B) | ||
750 | workd(perm,col2) = U \ (L \ (P * workd(:,col1))); | ||
751 | else | ||
752 | workd(:,col2) = U \ (L \ (P * workd(:,col1))); | ||
753 | end | ||
754 | elseif (ido == 1) | ||
755 | if issparse(A) & issparse(B) | ||
756 | workd(perm,col2) = U \ (L \ (P * workd(:,col3))); | ||
757 | else | ||
758 | workd(:,col2) = U \ (L \ (P * workd(:,col3))); | ||
759 | end | ||
760 | end | ||
761 | end | ||
762 | else % mode is not 1 or 3 | ||
763 | error(['Unknown mode returned from ' prefix 'aupd.']) | ||
764 | end | ||
765 | else % A is not a matrix | ||
766 | if (mode == 1) | ||
767 | if isempty(B) | ||
768 | % OP = A*x | ||
769 | %workd(:,col2) = feval(A,workd(:,col1),varargin{7-Amatrix-Bnotthere:end}); | ||
770 | |||
771 | |||
772 | |||
773 | |||
774 | |||
775 | nombre_A_times_X = nombre_A_times_X + 1; | ||
776 | |||
777 | |||
778 | |||
779 | pause(0); %voir | ||
780 | |||
781 | if nbArgumentsAfun == 1 | ||
782 | workd(:,col2) = feval(A,workd(:,col1),arguments_Afun); | ||
783 | %workd(:,col2) = max(workd(:,col2),0); | ||
784 | elseif nbArgumentsAfun == 2 | ||
785 | workd(:,col2) = feval(A,workd(:,col1),arguments_Afun,arguments_Afun2); | ||
786 | elseif nbArgumentsAfun == 3 | ||
787 | workd(:,col2) = feval(A,workd(:,col1),arguments_Afun,arguments_Afun2,arguments_Afun3); | ||
788 | else | ||
789 | workd(:,col2) = feval(A,workd(:,col1),varargin{indexArgumentsAfun}); | ||
790 | end | ||
791 | %workd(:,col2) = tim_w_times_x_c(workd(:,col1),arguments_Afun); %slower | ||
792 | |||
793 | else | ||
794 | % OP = R'\(A*(R\x)) | ||
795 | if issparse(B) | ||
796 | workd(permB,col2) = RB \ workd(:,col1); | ||
797 | workd(:,col2) = feval(A,workd(:,col2),arguments_Afun); | ||
798 | workd(:,col2) = RBT \ workd(permB,col2); | ||
799 | |||
800 | else | ||
801 | workd(:,col2) = RBT \ feval(A,(RB\workd(:,col1)),arguments_Afun); | ||
802 | end | ||
803 | end | ||
804 | elseif (mode == 3) | ||
805 | if isempty(B) | ||
806 | workd(:,col2) = feval(A,workd(:,col1), arguments_Afun); | ||
807 | else | ||
808 | if (ido == -1) | ||
809 | if cholB | ||
810 | workd(:,col2) = RBT * (RB * workd(:,col1)); | ||
811 | else | ||
812 | workd(:,col2) = B * workd(:,col1); | ||
813 | end | ||
814 | workd(:,col2) = feval(A,workd(:,col2), arguments_Afun); | ||
815 | elseif (ido == 1) | ||
816 | workd(:,col2) = feval(A,workd(:,col3), arguments_Afun); | ||
817 | end | ||
818 | end | ||
819 | else % mode is not 1 or 3 | ||
820 | error(['Unknown mode returned from ' prefix 'aupd.']) | ||
821 | end | ||
822 | end % if Amatrix | ||
823 | case 2 | ||
824 | if (mode == 3) | ||
825 | if cholB | ||
826 | workd(:,col2) = RBT * (RB * workd(:,col1)); | ||
827 | else | ||
828 | workd(:,col2) = B * workd(:,col1); | ||
829 | end | ||
830 | else | ||
831 | error(['Unknown mode returned from ' prefix 'aupd.']) | ||
832 | end | ||
833 | case 3 | ||
834 | % setting iparam(1) = ishift = 1 ensures this never happens | ||
835 | warning('MATLAB:eigs:WorklShiftsUnsupported', ... | ||
836 | ['EIGS does not yet support computing the shifts in workl.' ... | ||
837 | ' Setting reverse communication parameter to 99 and returning.']) | ||
838 | ido = int32(99); | ||
839 | case 99 | ||
840 | otherwise | ||
841 | error(['Unknown value of reverse communication parameter' ... | ||
842 | ' returned from ' prefix 'aupd.']) | ||
843 | end | ||
844 | |||
845 | cputms(3) = cputms(3) + (cputime-t0); % end timing MATLAB OP(X) | ||
846 | |||
847 | %tim | ||
848 | if nombreIterations ~= double(ipntr(15)) | ||
849 | nombreIterations = double(ipntr(15)); | ||
850 | end | ||
851 | |||
852 | if display >= 1 && display <=2 | ||
853 | iter = double(ipntr(15)); | ||
854 | if (iter > ariter) & (ido ~= 99) | ||
855 | ariter = iter; | ||
856 | ds = sprintf(['Iteration %d: a few Ritz values of the' ... | ||
857 | ' %d-by-%d matrix:'],iter,p,p); | ||
858 | disp(ds) | ||
859 | if isrealprob | ||
860 | if issymA | ||
861 | dispvec = [workl(double(ipntr(6))+(0:p-1))]; | ||
862 | if isequal(whch,'BE') | ||
863 | % roughly k Large eigenvalues and k Small eigenvalues | ||
864 | disp(dispvec(max(end-2*k+1,1):end)) | ||
865 | else | ||
866 | % k eigenvalues | ||
867 | disp(dispvec(max(end-k+1,1):end)) | ||
868 | end | ||
869 | else | ||
870 | dispvec = [complex(workl(double(ipntr(6))+(0:p-1)), ... | ||
871 | workl(double(ipntr(7))+(0:p-1)))]; | ||
872 | % k+1 eigenvalues (keep complex conjugate pairs together) | ||
873 | disp(dispvec(max(end-k,1):end)) | ||
874 | end | ||
875 | else | ||
876 | dispvec = [complex(workl(2*double(ipntr(6))-1+(0:2:2*(p-1))), ... | ||
877 | workl(2*double(ipntr(6))+(0:2:2*(p-1))))]; | ||
878 | disp(dispvec(max(end-k+1,1):end)) | ||
879 | end | ||
880 | end | ||
881 | end | ||
882 | |||
883 | end % while (ido ~= 99) | ||
884 | |||
885 | t0 = cputime; % start timing post-processing | ||
886 | |||
887 | flag = 0; | ||
888 | if (info < 0) | ||
889 | es = sprintf('Error with ARPACK routine %saupd: info = %d',prefix,full(info)); | ||
890 | error(es) | ||
891 | else | ||
892 | if (nargout >= 2) | ||
893 | rvec = int32(1); % compute eigenvectors | ||
894 | else | ||
895 | rvec = int32(0); % do not compute eigenvectors | ||
896 | end | ||
897 | |||
898 | if isrealprob | ||
899 | if issymA | ||
900 | arpackc( 'dseupd', rvec, 'A', select, ... | ||
901 | d, v, ldv, sigma, ... | ||
902 | bmat, int32(n), whch, nev, tol, resid, ncv, ... | ||
903 | v, ldv, iparam, ipntr, workd, workl, lworkl, info ); | ||
904 | if isequal(whch,'LM') | isequal(whch,'LA') | ||
905 | d = flipud(d); | ||
906 | if (rvec == 1) | ||
907 | v(:,1:k) = v(:,k:-1:1); | ||
908 | end | ||
909 | end | ||
910 | if ((isequal(whch,'SM') | isequal(whch,'SA')) & (rvec == 0)) | ||
911 | d = flipud(d); | ||
912 | end | ||
913 | else | ||
914 | arpackc( 'dneupd', rvec, 'A', select, ... | ||
915 | d, di, v, ldv, sigmar, sigmai, workev, ... | ||
916 | bmat, int32(n), whch, nev, tol, resid, ncv, ... | ||
917 | v, ldv, iparam, ipntr, workd, workl, lworkl, info ); | ||
918 | d = complex(d,di); | ||
919 | if rvec | ||
920 | d(k+1) = []; | ||
921 | else | ||
922 | zind = find(d == 0); | ||
923 | if isempty(zind) | ||
924 | d = d(k+1:-1:2); | ||
925 | else | ||
926 | d(max(zind)) = []; | ||
927 | d = flipud(d); | ||
928 | end | ||
929 | end | ||
930 | end | ||
931 | else | ||
932 | zsigma = [real(sigma); imag(sigma)]; | ||
933 | arpackc( 'zneupd', rvec, 'A', select, ... | ||
934 | zd, zv, ldv, zsigma, workev, ... | ||
935 | bmat, int32(n), whch, nev, tol, resid, ncv, zv, ... | ||
936 | ldv, iparam, ipntr, zworkd, workl, lworkl, ... | ||
937 | rwork, info ); | ||
938 | if issymA | ||
939 | d = zd(1:2:end-1); | ||
940 | else | ||
941 | d = complex(zd(1:2:end-1),zd(2:2:end)); | ||
942 | end | ||
943 | v = reshape(complex(zv(1:2:end-1),zv(2:2:end)),[n p]); | ||
944 | end | ||
945 | |||
946 | if (info ~= 0) | ||
947 | es = ['Error with ARPACK routine ' prefix 'eupd: ']; | ||
948 | switch double(info) | ||
949 | case 2 | ||
950 | ss = sum(select); | ||
951 | if (ss < k) | ||
952 | es = [es ... | ||
953 | ' The logical variable select was only set with ' int2str(ss) ... | ||
954 | ' 1''s instead of nconv=' int2str(double(iparam(5))) ... | ||
955 | ' (k=' int2str(k) ').' ... | ||
956 | ' Please contact the ARPACK authors at arpack@caam.rice.edu.']; | ||
957 | else | ||
958 | es = [es ... | ||
959 | 'The LAPACK reordering routine ' prefix(1) ... | ||
960 | 'trsen did not return all ' int2str(k) ' eigenvalues.'] | ||
961 | end | ||
962 | case 1 | ||
963 | es = [es ... | ||
964 | 'The Schur form could not be reordered by the LAPACK routine ' ... | ||
965 | prefix(1) 'trsen.' ... | ||
966 | ' Please contact the ARPACK authors at arpack@caam.rice.edu.']; | ||
967 | case -14 | ||
968 | es = [es prefix ... | ||
969 | 'aupd did not find any eigenvalues to sufficient accuracy.']; | ||
970 | otherwise | ||
971 | es = [es sprintf('info = %d',full(info))]; | ||
972 | end | ||
973 | error(es) | ||
974 | else | ||
975 | nconv = double(iparam(5)); | ||
976 | if (nconv == 0) | ||
977 | if (nargout < 3) | ||
978 | warning('MATLAB:eigs:NoEigsConverged', ... | ||
979 | 'None of the %d requested eigenvalues converged.',k) | ||
980 | else | ||
981 | flag = 1; | ||
982 | end | ||
983 | elseif (nconv < k) | ||
984 | if (nargout < 3) | ||
985 | warning('MATLAB:eigs:NotAllEigsConverged', ... | ||
986 | 'Only %d of the %d requested eigenvalues converged.',nconv,k) | ||
987 | else | ||
988 | flag = 1; | ||
989 | end | ||
990 | end | ||
991 | end % if (info ~= 0) | ||
992 | end % if (info < 0) | ||
993 | |||
994 | if (issymA) | (~isrealprob) | ||
995 | if (nargout <= 1) | ||
996 | if isrealprob | ||
997 | varargout{1} = d; | ||
998 | else | ||
999 | varargout{1} = d(k:-1:1,1); | ||
1000 | end | ||
1001 | else | ||
1002 | varargout{1} = v(:,1:k); | ||
1003 | varargout{2} = diag(d(1:k,1)); | ||
1004 | if (nargout >= 3) | ||
1005 | varargout{3} = flag; | ||
1006 | end | ||
1007 | end | ||
1008 | else | ||
1009 | if (nargout <= 1) | ||
1010 | varargout{1} = d; | ||
1011 | else | ||
1012 | cplxd = find(di ~= 0); | ||
1013 | % complex conjugate pairs of eigenvalues occur together | ||
1014 | cplxd = cplxd(1:2:end); | ||
1015 | v(:,[cplxd cplxd+1]) = [complex(v(:,cplxd),v(:,cplxd+1)) ... | ||
1016 | complex(v(:,cplxd),-v(:,cplxd+1))]; | ||
1017 | varargout{1} = v(:,1:k); | ||
1018 | varargout{2} = diag(d); | ||
1019 | if (nargout >= 3) | ||
1020 | varargout{3} = flag; | ||
1021 | end | ||
1022 | end | ||
1023 | end | ||
1024 | |||
1025 | if (nargout >= 2) & (mode == 1) & ~isempty(B) | ||
1026 | if issparse(B) | ||
1027 | varargout{1}(permB,:) = RB \ varargout{1}; | ||
1028 | else | ||
1029 | varargout{1} = RB \ varargout{1}; | ||
1030 | end | ||
1031 | end | ||
1032 | |||
1033 | cputms(4) = cputime-t0; % end timing post-processing | ||
1034 | |||
1035 | cputms(5) = sum(cputms(1:4)); % total time | ||
1036 | |||
1037 | if (display >= 2) %tim | ||
1038 | if (mode == 1) | ||
1039 | innerstr = sprintf(['Compute A*X:' ... | ||
1040 | ' %f\n'],cputms(3)); | ||
1041 | elseif (mode == 2) | ||
1042 | innerstr = sprintf(['Compute A*X and solve B*X=Y for X:' ... | ||
1043 | ' %f\n'],cputms(3)); | ||
1044 | elseif (mode == 3) | ||
1045 | if isempty(B) | ||
1046 | innerstr = sprintf(['Solve (A-SIGMA*I)*X=Y for X:' ... | ||
1047 | ' %f\n'],cputms(3)); | ||
1048 | else | ||
1049 | innerstr = sprintf(['Solve (A-SIGMA*B)*X=B*Y for X:' ... | ||
1050 | ' %f\n'],cputms(3)); | ||
1051 | end | ||
1052 | end | ||
1053 | if ((mode == 3) & (Amatrix)) | ||
1054 | if isempty(B) | ||
1055 | prepstr = sprintf(['Pre-processing, including lu(A-sigma*I):' ... | ||
1056 | ' %f\n'],cputms(1)); | ||
1057 | else | ||
1058 | prepstr = sprintf(['Pre-processing, including lu(A-sigma*B):' ... | ||
1059 | ' %f\n'],cputms(1)); | ||
1060 | end | ||
1061 | elseif ((mode == 2) & (~cholB)) | ||
1062 | prepstr = sprintf(['Pre-processing, including chol(B):' ... | ||
1063 | ' %f\n'],cputms(1)); | ||
1064 | else | ||
1065 | prepstr = sprintf(['Pre-processing:' ... | ||
1066 | ' %f\n'],cputms(1)); | ||
1067 | end | ||
1068 | sstr = sprintf(['***********CPU Timing Results in seconds***********']); | ||
1069 | ds = sprintf(['\n' sstr '\n' ... | ||
1070 | prepstr ... | ||
1071 | 'ARPACK''s %saupd: %f\n' ... | ||
1072 | innerstr ... | ||
1073 | 'Post-processing with ARPACK''s %seupd: %f\n' ... | ||
1074 | '***************************************************\n' ... | ||
1075 | 'Total: %f\n' ... | ||
1076 | sstr '\n'], ... | ||
1077 | prefix,cputms(2),prefix,cputms(4),cputms(5)); | ||
1078 | disp(ds) | ||
1079 | if nombre_A_times_X > 0 %tim | ||
1080 | disp(sprintf('Number of iterations : %f\n',nombreIterations)); | ||
1081 | disp(sprintf('Number of times A*X was computed : %f\n',nombre_A_times_X)); | ||
1082 | disp(sprintf('Average time for A*X : %f\n',cputms(3)/nombre_A_times_X)); | ||
1083 | end | ||
1084 | end | ||