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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
| /*****************************************************************/
/* This program is for 2D segmentation using the LEGION network */
/* The equations were modeled as a PDP algorithm for efficiency */
/* by DeLiang Wang in 7/94. For a detailed description of the */
/* algorithm see D.L. Wang and D. Terman: "Image segmentation */
/* based on oscillatory correlation", Neural Computation, Vol. 9 */
/* pp. 805-836, 1997 (For errata see Neural Computation 9, */
/* 1623-1626, 1997), on page 822. */
/*****************************************************************/
#include "mex.h"
#include "segment.h"
#define MAXVAL 32768.0
/* 2147483648.0 or 32768.0,machine (HP or SUN) dependable*/
#define MAX 32767 /* 2147483647 or 32767, machine dependable */
#define GRAY 255 /* Maximum value of a pixel */
int bi();
int jump[2], z, Num = 1; /* so we can change step size at will */
/* Num: number of the segments */
FILE *outfile, *fopen();
float x[N1][N2][2], lateral[N1][N2];
/* lateral: the sum of input from lateral connections */
float W[N1][N2][8]; /* strength of the interactions for 4 directions */
int stim[N1][N2], matrix[N1][N2];
int y[N1][N2], cycle[N1][N2], trigger[N1][N2];
/* y: integer of x; cycle: test for 1st cycle; trigger: jumping signal */
void inputs(void)
{
int i, j;
int width, height, c;
char magic[25];
float stimulus = 0.2;
FILE *IN;
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++)
matrix[i][j] = 0;
IN = fopen("lake1.pgm", "r");
fscanf(IN,"%s\n%d %d\n%d\n",magic,&width,&height,&c);
fprintf(stderr,"[Type %s %dx%d colours %d]\n",magic,width,height,c);
for(i=0;i<160;i++)
for(j=0;j<160;j++)
stim[i][j] = getc(IN);
fclose(IN);
}
void connections(void)
{
int i, j, k;
float baseline = 0.0, Wtotal = 6.0, sum;
for (i = 0; i < N1; i++) /* for eight nearest neighbors */
for (j = 0; j < N2; j++) {
W[i][j][0] = W[i][j][1] = W[i][j][2] = W[i][j][3] = 0.0;
W[i][j][4] = W[i][j][5] = W[i][j][6] = W[i][j][7] = 0.0;
}
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) {
sum = 0.0;
if (j-1 >= 0)
W[i][j][0] = (float) GRAY/(1+abs(stim[i][j]-stim[i][j-1]));
if (i-1 >= 0 && j-1 >= 0)
W[i][j][1] = (float) GRAY/(1+abs(stim[i][j]-stim[i-1][j-1]));
if (i-1 >= 0)
W[i][j][2] = (float) GRAY/(1+abs(stim[i][j]-stim[i-1][j]));
if (i-1 >= 0 && j+1 < N2)
W[i][j][3] = (float) GRAY/(1+abs(stim[i][j]-stim[i-1][j+1]));
if (j+1 < N2)
W[i][j][4] = (float) GRAY/(1+abs(stim[i][j]-stim[i][j+1]));
if (i+1 < N1 && j+1 < N2)
W[i][j][5] = (float) GRAY/(1+abs(stim[i][j]-stim[i+1][j+1]));
if (i+1 < N1)
W[i][j][6] = (float) GRAY/(1+abs(stim[i][j]-stim[i+1][j]));
if (i+1 < N1 && j-1 >= 0)
W[i][j][7] = (float) GRAY/(1+abs(stim[i][j]-stim[i+1][j-1]));
for (k = 0; k < 8; k++) lateral[i][j] += W[i][j][k];
}
}
void initiate(void)
{
int i,j,t, temp; /* labels for setting up display intervals */
for (i = 0; i < N1; i++) /* randomize the initial phases */
for (j = 0; j < N2; j++) {
y[i][j] = rand();
x[i][j][0] = y[i][j]/(MAXVAL + 1.0);
y[i][j] -= 2;
if (y[i][j] < 0)
y[i][j] = 0;
trigger[i][j] = 0;
cycle[i][j] = 0;
}
z = 0;
/* initial display values for 2D */
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) {
temp = x[i][j][0]*RP;
fprintf(outfile, "%d ", temp);
}
}
void select(void)
{
int i, j, max, imax=0, jmax=0;
float baseline = 1200.0, temp;
/* baseline 1200 -> 17 regions; 1000 -> 23 regions. Both reasonable */
max = -1;
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) {
trigger[i][j] = 0;
if (lateral[i][j] > baseline && y[i][j] != MAX && y[i][j] > max) {
max = y[i][j];
imax = i;
jmax = j;
}
}
if (cycle[imax][jmax]) { /* find out jumpers */
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++)
if (y[i][j] == max && cycle[i][j])
trigger[i][j] = 1;
} else
trigger[imax][jmax] = 1;
for (i = 0; i < N1; i++) /* advance oscillators on the left branch */
for (j = 0; j < N2; j++)
if (y[i][j] != MAX && (cycle[i][j] || lateral[i][j] > baseline)) {
y[i][j] = y[i][j] + (MAX - max);
x[i][j][0] = y[i][j]/(MAXVAL + 1.0);
}
}
void evaluate(void)
{
float temp, thetae = 2.0, temp2, temp3;
int i,j,t, cat, count; /* labels for setting up display intervals */
jump[0] = 0;
select();
for ( t = 0; t < WIDTH-1; t++) {
count = 0;
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) { /* compute net input */
if (x[i][j][0] > thetae && jump[0] == 0) { /* jumping down */
x[i][j][1] = LP;
y[i][j] = -1;
cycle[i][j] = 1;
count++;
} else if (x[i][j][0] < thetae) { /* summing up overall input */
temp = -20.0 * bi((float) z,0.5); /* used to be 50 */
/* the following is based on traversing through strongest link */
temp2 = 0.0;
if (j-1 >= 0) {
temp3 = W[i][j][0] * bi(x[i][j-1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i-1 >= 0 && j-1 >= 0) {
temp3 = W[i][j][1] * bi(x[i-1][j-1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i-1 >= 0) {
temp3 = W[i][j][2] * bi(x[i-1][j][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i-1 >= 0 && j+1 < N2) {
temp3 = W[i][j][3] * bi(x[i-1][j+1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (j+1 < N2) {
temp3 = W[i][j][4] * bi(x[i][j+1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i+1 < N1 && j+1 < N2) {
temp3 = W[i][j][5] * bi(x[i+1][j+1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i+1 < N1) {
temp3 = W[i][j][6] * bi(x[i+1][j][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
if (i+1 < N1 && j-1 >= 0) {
temp3 = W[i][j][7] * bi(x[i+1][j-1][0], thetae);
if (temp2 < temp3) temp2 = temp3; }
temp += temp2;
if (trigger[i][j] || temp > 0.25) { /* jumping up */
x[i][j][1] = RP;
y[i][j] = MAX;
jump[1] = 1;
z++;
} else
x[i][j][1] = x[i][j][0];
} else
x[i][j][1] = x[i][j][0];
}
z -= count;
if (jump[1] == 0 && z == 0)
select();
/* store for display in 2d case*/
if (jump[1] == 0 && jump[0] == 1) {
mexPrintf("we are here \n");
Num++;
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) {
if (z > 0) {
if (x[i][j][1] < thetae)
cat = 0;
else cat = x[i][j][1];
} else
cat = x[i][j][1];
fprintf(outfile, "%d ", cat);
if (Num < 25 && cat > 0)
matrix[N1-1-i][j] = 26-Num; /* 17 regions, or 23 */
}
}
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++) /* replace OLD by NEW */
x[i][j][0] = x[i][j][1];
jump[0] = jump[1];
jump[1] = 0;
}
}
void store(void)
{
int i, j;
FILE *out, *out1, *fopen();
mexPrintf("The number of the segments/cycles is %d\n", Num);
out = fopen("NUMBER", "w");
out1 = fopen("Result", "w");
fprintf(out, "%d ", Num);
for (i = 0; i < N1; i++)
for (j = 0; j < N2; j++)
fprintf(out1, "%d ", matrix[i][j]);
fclose(out);
fclose(out1);
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int *pr_seed;
pr_seed = (int *)mxGetData(prhs[0]);
srand(*pr_seed);
outfile = fopen("Out", "w");
inputs();
connections();
initiate();
evaluate();
store();
fclose(outfile);
}
int bi(x, threshold)
float x, threshold;
{
if (x > threshold) return (1);
else return (0);
} |
Partager