diff options
Diffstat (limited to 'SD-VBS/common/toolbox/MultiNcut/affinityic.c')
| -rwxr-xr-x | SD-VBS/common/toolbox/MultiNcut/affinityic.c | 187 |
1 files changed, 187 insertions, 0 deletions
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 | } | ||
