1 /*================================================================
\r
2 * function w = affinityic(emag,ephase,pi,pj,sigma)
\r
4 * emag = edge strength at each pixel
\r
5 * ephase = edge phase at each pixel
\r
6 * [pi,pj] = index pair representation for MALTAB sparse matrices
\r
7 * sigma = sigma for IC energy
\r
9 * w = affinity with IC at [pi,pj]
\r
14 [i,j] = cimgnbmap(size(f),2);
\r
15 [ex,ey,egx,egy] = quadedgep(f);
\r
16 a = affinityic(ex,ey,egx,egy,i,j)
\r
19 * Stella X. Yu, Nov 19, 2001.
\r
20 *=================================================================*/
\r
32 /* declare variables */
\r
33 int nr, nc, np, total;
\r
34 int i, j, k, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step;
\r
35 double sigma, di, dj, a, z, maxori, phase1, phase2, slope;
\r
38 unsigned int *pi, *pj;
\r
39 double *emag, *ephase, *w;
\r
41 /* check argument */
\r
43 mexErrMsgTxt("Four input arguments required");
\r
46 mexErrMsgTxt("Too many output arguments");
\r
49 /* get edgel information */
\r
52 if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) {
\r
53 mexErrMsgTxt("Edge magnitude and phase shall be of the same image size");
\r
55 emag = mxGetPr(in[0]);
\r
56 ephase = mxGetPr(in[1]);
\r
59 /* get new index pair */
\r
60 if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) {
\r
61 mexErrMsgTxt("Index pair shall be of type UINT32");
\r
63 if (mxGetM(in[3]) * mxGetN(in[3]) != np + 1) {
\r
64 mexErrMsgTxt("Wrong index representation");
\r
66 pi = (unsigned int*)mxGetData(in[2]);
\r
67 pj = (unsigned int*)mxGetData(in[3]);
\r
70 out[0] = mxCreateSparse(np,np,pj[np],mxREAL);
\r
72 mexErrMsgTxt("Not enough memory for the output matrix");
\r
74 w = mxGetPr(out[0]);
\r
75 ir = mxGetIr(out[0]);
\r
76 jc = mxGetJc(out[0]);
\r
81 for (k=0; k<np; k++) {
\r
82 if (emag[k]>sigma) { sigma = emag[k]; }
\r
85 printf("sigma = %6.5f",sigma);
\r
87 sigma = mxGetScalar(in[4]);
\r
89 a = 0.5 / (sigma * sigma);
\r
93 for (j=0; j<np; j++) {
\r
96 jx = j / nr; /* col */
\r
97 jy = j % nr; /* row */
\r
99 for (k=pj[j]; k<pj[j+1]; k++) {
\r
112 di = (double) (iy - jy);
\r
113 dj = (double) (ix - jx);
\r
116 phase1 = ephase[j];
\r
119 /* sample in i direction */
\r
120 if (abs(di) >= abs(dj)) {
\r
122 step = (iy>=jy) ? 1 : -1;
\r
128 for (ii=0;ii<abs(di);ii++){
\r
129 iip2 = iip1 + step;
\r
130 jjp2 = (int)(0.5 + slope*(iip2-jy) + jx);
\r
132 phase2 = ephase[iip2+jjp2*nr];
\r
134 if (phase1 != phase2) {
\r
135 z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
\r
146 /* sample in j direction */
\r
149 step = (ix>=jx) ? 1: -1;
\r
155 for (jj=0;jj<abs(dj);jj++){
\r
156 jjp2 = jjp1 + step;
\r
157 iip2 = (int)(0.5+ slope*(jjp2-jx) + jy);
\r
159 phase2 = ephase[iip2+jjp2*nr];
\r
161 if (phase1 != phase2){
\r
162 z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
\r
175 maxori = 0.5 * maxori;
\r
176 maxori = exp(-maxori * maxori * a);
\r