summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/MultiNcut/multiAffinityic.c
blob: 6919db9a67fc7b8f637d6c2e123304bf7f47c029 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*================================================================
* function w = affinityic(emag,ephase,pi,pj,subgrid,nrSubgrid,ncSubgrid,subpi,sigma)
* Input:
*   emag = edge strength at each pixel
*   ephase = edge phase at each pixel
*   [pi,pj] = index pair representation for MALTAB sparse matrices, pi index in original grid, size(pj)=nrW*ncW+1
*   subgrid= index of the subgrid in the original image (first pixel's index is one!)
*   [nrSubgrid,ncSubgrid]= number of rows et colums of the subgrid
*   subpi = pi but with index in subgrid
*   sigma = sigma for IC energy
* Output:
*   w = affinity with IC at [pi,pj]
*

% test sequence
f = synimg(10);
[i,j] = cimgnbmap(size(f),2);
[ex,ey,egx,egy] = quadedgep(f);
a = affinityic(ex,ey,egx,egy,i,j)
show_dist_w(f,a);

* Stella X. Yu, Nov 19, 2001.
*=================================================================*/

# include "mex.h"
# include "math.h"

void mexFunction(
    int nargout,
    mxArray *out[],
    int nargin,
    const mxArray *in[]
)
{
    /* declare variables */
    int nr, nc, np,  nW, total;
    int i, j, k, t, ix, iy, jx, jy, ii, jj, iip1, jjp1, iip2, jjp2, step,nrSubgrid, ncSubgrid;
    double sigma, di, dj, a, z, maxori, phase1, phase2, slope;
	int *ir, *jc;
    /*	unsigned long *pi, *pj, *subpi;

 */
    unsigned int *pi, *pj, *subpi;
	double *emag, *ephase, *w,*tmp,*subgrid;
	
    
    /* check argument */
    if (nargin<8) {
        mexErrMsgTxt("Eight input arguments required");
    }
    if (nargout>1) {
        mexErrMsgTxt("Too many output arguments");
    }

    /* get edgel information */
	nr = mxGetM(in[0]);
	nc = mxGetN(in[0]);
	if ( nr*nc ==0 || nr != mxGetM(in[1]) || nc != mxGetN(in[1]) ) {
	    mexErrMsgTxt("Edge magnitude and phase shall be of the same image size");
	}
    emag = mxGetPr(in[0]);
    ephase = mxGetPr(in[1]);
    np = nr * nc;
    
    /*get subgrid information*/
    
    tmp = mxGetData(in[5]);
    nrSubgrid = (int)tmp[0];
    tmp = mxGetData(in[6]);
    ncSubgrid = (int)tmp[0];
    
    /* printf("nrSubgrid=%d\n",nrSubgrid);
 
       printf("ncSubgrid=%d\n",ncSubgrid);
 */
    if (nrSubgrid* ncSubgrid != mxGetM(in[4])*mxGetN(in[4])) {
          mexErrMsgTxt("Error in the size of the subgrid");
          }
    subgrid = mxGetData(in[4]);
    nW = nrSubgrid * ncSubgrid;
    
    /* get new index pair */
    if (!mxIsUint32(in[2]) | !mxIsUint32(in[3])) {
        mexErrMsgTxt("Index pair shall be of type UINT32");
    }
    if (mxGetM(in[3]) * mxGetN(in[3]) != nW + 1) {
        mexErrMsgTxt("Wrong index representation");
    }
    pi = mxGetData(in[2]);
    pj = mxGetData(in[3]);
    subpi = mxGetData(in[7]);
    /*{printf("pi[50] = %d\n",pi[50]);}
    {printf("subpi[5] = %d\n",subpi[5]);}
    {printf("subpi[6] = %d\n",subpi[6]);}
    {printf("subpi[4] = %d\n",subpi[4]);}*/

    /* create output */          /*!!!!!!!!!!!!!!!!!!!!!!!changer taille output!!!!!!!!!!*/
    out[0] = mxCreateSparse(nW,nW,pj[nW],mxREAL);
    if (out[0]==NULL) {
	    mexErrMsgTxt("Not enough memory for the output matrix");
	}
	w = mxGetPr(out[0]);
	ir = mxGetIr(out[0]);
	jc = mxGetJc(out[0]);
	
    /* find my sigma */
	if (nargin<9) {
	    sigma = 0;
    	for (k=0; k<np; k++) { 
    	    if (emag[k]>sigma) { sigma = emag[k]; }
    	}
    	sigma = sigma / 6;
    	printf("sigma = %6.5f",sigma);
	} else {
	    sigma = mxGetScalar(in[8]);
	}
	a = 0.5 / (sigma * sigma);
	
    /* computation */ 
    total = 0;
    
    for (j=0; j<nW; j++){
        t= (int)subgrid[j]-1;  /*on parcourt tous les pixels de la sous-grille dans la grille d'origine*/        

        jc[j] = total;    /* total represente le nombre voisins  du pixel j*/
        jx = t / nr; /* col */  
        jy = t % nr; /* row */
        
        for (k=pj[j]; k<pj[j+1]; k++) {   /*k represente les indices correspondant au pixel j dans pi*/
        
            i = pi[k]-1;                     /*i est un voisin de j a considerer*/
          
            if (i==j) {
                maxori = 1;
            
            } else {
        
                ix = i / nr; 
                iy = i % nr;

                /* scan */            
                di = (double) (iy - jy);
                dj = (double) (ix - jx);
            
                maxori = 0.;
	            phase1 = ephase[j];

	               
                /* sample in i direction */
                if (abs(di) >= abs(dj)) {  
            	    slope = dj / di;
            	    step = (iy>=jy) ? 1 : -1;
            	
              	    iip1 = jy;
            	    jjp1 = jx;
	
	               
	                for (ii=0;ii<abs(di);ii++){
	                    iip2 = iip1 + step;
	                    jjp2 = (int)(0.5 + slope*(iip2-jy) + jx);
	  	  
	                    phase2 = ephase[iip2+jjp2*nr];
               
	                    if (phase1 != phase2) {
	                        z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
	                        if (z > maxori){
	                            maxori = z;
	                        }
	                    } 
	             
	                    iip1 = iip2;
	                    jjp1 = jjp2;
	                    phase1 = phase2;
	                }
	            
	            /* sample in j direction */    
                } else { 
	                slope = di / dj;
	                step =  (ix>=jx) ? 1: -1;

    	            jjp1 = jx;
	                iip1 = jy;	           
	    
	 
	                for (jj=0;jj<abs(dj);jj++){
	                    jjp2 = jjp1 + step;
	                    iip2 = (int)(0.5+ slope*(jjp2-jx) + jy);
	  	  
	                    phase2 = ephase[iip2+jjp2*nr];
	     
	                    if (phase1 != phase2){
	                        z = (emag[iip1+jjp1*nr] + emag[iip2+jjp2*nr]);
	                        if (z > maxori){ 
	                            maxori = z; 
	                        }
	                        
	                    }
	  
	                    iip1 = iip2;
	                    jjp1 = jjp2;
	                    phase1 = phase2;
	                }
                }            
            
                maxori = 0.5 * maxori;
                maxori = exp(-maxori * maxori * a);
            }  
            /*if (total<20) {printf("subpi[k] = %d\n",subpi[k]);}*/

		    ir[total] = (int)subpi[k];
/*if (total<20) {printf("ir[total] = %d\n",ir[total]);}*/
		    w[total] = maxori;
		    total = total + 1;
			
		} /* i */
       }  /*j*/
     /*printf("total = %d\n",total);*/   
      /*printf("ir[100] = %d\n",ir[100]);*/ 
    jc[nW] = total;
}