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