diff options
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/affinityic.c')
-rwxr-xr-x | SD-VBS/common/toolbox/toolbox_basic/affinityic.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/affinityic.c b/SD-VBS/common/toolbox/toolbox_basic/affinityic.c new file mode 100755 index 0000000..e48762a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/affinityic.c | |||
@@ -0,0 +1,186 @@ | |||
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 | * Jianbo Shi, 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 long *pi, *pj; | ||
38 | double *emag, *ephase, *w; | ||
39 | |||
40 | /* check argument */ | ||
41 | if (nargin<4) { | ||
42 | mexErrMsgTxt("Four input arguments required"); | ||
43 | } | ||
44 | if (nargout>1) { | ||
45 | mexErrMsgTxt("Too many output arguments"); | ||
46 | } | ||
47 | |||
48 | /* get edgel information */ | ||
49 | nr = mxGetM(in[0]); | ||
50 | nc = mxGetN(in[0]); | ||
51 | if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) { | ||
52 | mexErrMsgTxt("Edge magnitude and phase shall be of the same image size"); | ||
53 | } | ||
54 | emag = mxGetPr(in[0]); | ||
55 | ephase = mxGetPr(in[1]); | ||
56 | np = nr * nc; | ||
57 | |||
58 | /* get new index pair */ | ||
59 | if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) { | ||
60 | mexErrMsgTxt("Index pair shall be of type UINT32"); | ||
61 | } | ||
62 | if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) { | ||
63 | mexErrMsgTxt("Wrong index representation"); | ||
64 | } | ||
65 | pi = mxGetData(in[2]); | ||
66 | pj = mxGetData(in[3]); | ||
67 | |||
68 | /* create output */ | ||
69 | out[0] = mxCreateSparse(np,np,pj[np],mxREAL); | ||
70 | if (out[0]==NULL) { | ||
71 | mexErrMsgTxt("Not enough memory for the output matrix"); | ||
72 | } | ||
73 | w = mxGetPr(out[0]); | ||
74 | ir = mxGetIr(out[0]); | ||
75 | jc = mxGetJc(out[0]); | ||
76 | |||
77 | /* find my sigma */ | ||
78 | if (nargin<5) { | ||
79 | sigma = 0; | ||
80 | for (k=0; k<np; k++) { | ||
81 | if (emag[k]>sigma) { sigma = emag[k]; } | ||
82 | } | ||
83 | sigma = sigma / 10; | ||
84 | printf("sigma = %6.5f",sigma); | ||
85 | } else { | ||
86 | sigma = mxGetScalar(in[4]); | ||
87 | } | ||
88 | a = 1.0/ (sigma); | ||
89 | |||
90 | /* computation */ | ||
91 | total = 0; | ||
92 | for (j=0; j<np; j++) { | ||
93 | |||
94 | jc[j] = total; | ||
95 | jx = j / nr; /* col */ | ||
96 | jy = j % nr; /* row */ | ||
97 | |||
98 | for (k=pj[j]; k<pj[j+1]; k++) { | ||
99 | |||
100 | i = pi[k]; | ||
101 | |||
102 | if (i==j) { | ||
103 | maxori = 1; | ||
104 | |||
105 | } else { | ||
106 | |||
107 | ix = i / nr; | ||
108 | iy = i % nr; | ||
109 | |||
110 | /* scan */ | ||
111 | di = (double) (iy - jy); | ||
112 | dj = (double) (ix - jx); | ||
113 | |||
114 | maxori = 0.; | ||
115 | phase1 = ephase[j]; | ||
116 | |||
117 | |||
118 | /* sample in i direction */ | ||
119 | if (abs(di) >= abs(dj)) { | ||
120 | slope = dj / di; | ||
121 | step = (iy>=jy) ? 1 : -1; | ||
122 | |||
123 | iip1 = jy; | ||
124 | jjp1 = jx; | ||
125 | |||
126 | |||
127 | for (ii=0;ii<abs(di);ii++){ | ||
128 | iip2 = iip1 + step; | ||
129 | jjp2 = (int)(0.5 + slope*(iip2-jy) + jx); | ||
130 | |||
131 | phase2 = ephase[iip2+jjp2*nr]; | ||
132 | |||
133 | if (phase1 != phase2) { | ||
134 | z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]); | ||
135 | if (z > maxori){ | ||
136 | maxori = z; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | iip1 = iip2; | ||
141 | jjp1 = jjp2; | ||
142 | phase1 = phase2; | ||
143 | } | ||
144 | |||
145 | /* sample in j direction */ | ||
146 | } else { | ||
147 | slope = di / dj; | ||
148 | step = (ix>=jx) ? 1: -1; | ||
149 | |||
150 | jjp1 = jx; | ||
151 | iip1 = jy; | ||
152 | |||
153 | |||
154 | for (jj=0;jj<abs(dj);jj++){ | ||
155 | jjp2 = jjp1 + step; | ||
156 | iip2 = (int)(0.5+ slope*(jjp2-jx) + jy); | ||
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 | |||
168 | iip1 = iip2; | ||
169 | jjp1 = jjp2; | ||
170 | phase1 = phase2; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | maxori = 0.5 * maxori*a; | ||
175 | maxori = exp(-maxori*maxori); | ||
176 | } | ||
177 | ir[total] = i; | ||
178 | |||
179 | w[total] = maxori + 0.005; | ||
180 | total = total + 1; | ||
181 | |||
182 | } /* i */ | ||
183 | } /* j */ | ||
184 | |||
185 | jc[np] = total; | ||
186 | } | ||