Newer
Older
/* {{{ Copyright etc. */
/* siena_diff - compute brain change using edge motion or segmentation
Stephen Smith, FMRIB Image Analysis Group
Copyright (C) 1999-2002 University of Oxford */
/* CCOPYRIGHT */
/* }}} */
/* {{{ defines, includes and typedefs */
#include "libss/libss.h"
#include "libss/libavw.h"
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* }}} */
/* {{{ usage */
void usage()
{
printf("\nUsage: siena_diff <input1_basename> <input2_basename> [options] [-s segmentation options]\n\n");
printf("[-d] debug - generate edge images and don't remove temporary images\n");
printf("[-2] don't segment grey+white separately (because there is poor grey-white contrast)\n\n");
printf("[-c <corr>] apply self-calibrating correction factor\n");
printf("[-e] erode joint mask a lot instead of dilating it slightly (ie find ventricle surface)\n");
printf("[-i] ignore flow in z (may be good if top of brain is missing)\n");
printf("[-m] apply <input1_basename>_talmask to brain edge points\n");
/* printf("[-t <n>] ignore top n slices (may be good if top of brain is missing)\n");*/
/* printf("[-b <n>] ignore bottom n slices (may be good if top of brain is missing)\n");*/
printf("[-s <options>] <options> to be passed to segmentation (type \"fast\" to get these)\n\n");
exit(1);
}
/* }}} */
/* {{{ main(argc, argv) */
#define CORRWIDTH 3
#define SEARCH 4
int main(argc, argv)
int argc;
char *argv [];
{
/* {{{ vars */
unsigned char *mask, *mask_dil;
char filename[10000], thestring[10000], segoptions[10000], fsldir[10000];
int x_size, y_size, z_size, size, x, y, z, i, count,
seg2=0, ignore_z=0, erode_mask=0, ignore_top_slices=0,
ignore_bottom_slices=0, debug=0, flow_output=1, edge_masking=0;
float *flow, *in1, *in2, tmpf, calib=1.0, *m1, *m2, *seg1, *arrA, *arrB, *arr1, *arr2;
double total, voxel_volume, voxel_area, ex, ey, ez, lowest;
image_struct im;
/* }}} */
/* {{{ process arguments */
if (argc<3)
usage();
sprintf(fsldir,"%s",getenv("FSLDIR"));
for (i = 3; i < argc; i++) {
if (!strcmp(argv[i], "-i"))
ignore_z=1;
else if (!strcmp(argv[i], "-e"))
erode_mask=1;
else if (!strcmp(argv[i], "-d"))
debug=1;
else if (!strcmp(argv[i], "-2"))
seg2=1;
else if (!strcmp(argv[i], "-c"))
/* {{{ apply self-calibrating factor */
{
i++;
if (argc<i+1)
{
printf("Error: no factor given following -c\n");
usage();
}
calib=atof(argv[i]);
}
/* }}} */
else if (!strcmp(argv[i], "-m"))
edge_masking=1;
else if (!strcmp(argv[i], "-t"))
/* {{{ ignore n slices at top */
{
i++;
if (argc<i+1)
{
printf("Error: no number of slices given following -t\n");
usage();
}
ignore_top_slices=atoi(argv[i]);
}
/* }}} */
else if (!strcmp(argv[i], "-b"))
/* {{{ ignore n slices at bottom */
{
i++;
if (argc<i+1)
{
printf("Error: no number of slices given following -b\n");
usage();
}
ignore_bottom_slices=atoi(argv[i]);
}
/* }}} */
else if (!strcmp(argv[i], "-s"))
/* {{{ segmentation options */
{
i++;
segoptions[0]=0;
while(i<argc)
{
strcat(segoptions,argv[i]);
strcat(segoptions," ");
i++;
}
}
/* }}} */
else
usage();
}
/* }}} */
/* {{{ transform images and masks */
sprintf(thestring,"%s/bin/flirt -o %s_halfwayto_%s -applyisoxfm 1 -paddingsize 0 -init %s_halfwayto_%s.mat -ref %s -in %s",
fsldir,argv[1],argv[2],argv[1],argv[2],argv[1],argv[1]);
printf("%s\n",thestring); system(thestring);
sprintf(thestring,"%s/bin/flirt -o %s_halfwayto_%s -applyisoxfm 1 -paddingsize 0 -init %s_halfwayto_%s.mat -ref %s -in %s",
fsldir,argv[2],argv[1],argv[2],argv[1],argv[1],argv[2]);
printf("%s\n",thestring); system(thestring);
sprintf(thestring,"%s/bin/flirt -o %s_halfwayto_%s_mask -applyisoxfm 1 -paddingsize 0 -init %s_halfwayto_%s.mat -ref %s -in %s_brain_mask",
fsldir,argv[1],argv[2],argv[1],argv[2],argv[1],argv[1]);
printf("%s\n",thestring); system(thestring);
sprintf(thestring,"%s/bin/flirt -o %s_halfwayto_%s_mask -applyisoxfm 1 -paddingsize 0 -init %s_halfwayto_%s.mat -ref %s -in %s_brain_mask",
fsldir,argv[2],argv[1],argv[2],argv[1],argv[1],argv[2]);
printf("%s\n",thestring); system(thestring);
if (edge_masking)
{
sprintf(thestring,"%s/bin/flirt -o %s_halfwayto_%s_valid_mask -applyisoxfm 1 -paddingsize 0 -init %s_halfwayto_%s.mat -ref %s -in %s_valid_mask_with_%s",
fsldir,argv[1],argv[2],argv[1],argv[2],argv[1],argv[1],argv[2]);
printf("%s\n",thestring); system(thestring);
}
/* }}} */
/* {{{ dilate masks, read transformed images and masks, and combine to jointly-masked transformed images */
printf("reading and combining transformed masks\n");
sprintf(filename,"%s_halfwayto_%s_mask",argv[1],argv[2]);
avw_read(filename,&im);
m1=im.i;
/* {{{ process header info */
x_size=im.x;
y_size=im.y;
z_size=im.z;
size=x_size*y_size*z_size;
voxel_volume = ABS( im.xv * im.yv * im.zv );
voxel_area = pow(voxel_volume,((double)0.6666667));
printf("final image dimensions = %d %d %d, voxel volume = %.3fmm^3, voxel area = %.3fmm^2\n",
x_size,y_size,z_size,voxel_volume,voxel_area);
/* }}} */
sprintf(filename,"%s_halfwayto_%s_mask",argv[2],argv[1]);
avw_read(filename,&im);
m2=im.i;
mask = (unsigned char*) malloc(sizeof(unsigned char)*x_size*y_size*z_size);
for(i=0;i<size;i++)
if (m1[i]+m2[i]>0.5) /* mask1 OR mask2 */
mask[i]=1;
else
mask[i]=0;
free(m1);
free(m2);
printf("dilating/eroding combined mask\n");
mask_dil = (unsigned char*) malloc(sizeof(unsigned char)*x_size*y_size*z_size);
memset(mask_dil,(unsigned char)0,sizeof(unsigned char)*x_size*y_size*z_size);
if (erode_mask)
/* {{{ erode mask LOTS */
/* note - this is a hack - depends on the scale of the images! */
{
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
/* {{{ mask_dil -> mask */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask_dil,x,y,z)==1) &&
((x>0)&&(IA(mask_dil,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask_dil,x+1,y,z)==1)) &&
((y>0)&&(IA(mask_dil,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask_dil,x,y+1,z)==1)) &&
((z>0)&&(IA(mask_dil,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask_dil,x,y,z+1)==1)) )
IA(mask,x,y,z)=1;
else
IA(mask,x,y,z)=0;
}
/* }}} */
/* {{{ mask -> mask_dil */
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) &&
((x>0)&&(IA(mask,x-1,y,z)==1)) && ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) &&
((y>0)&&(IA(mask,x,y-1,z)==1)) && ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) &&
((z>0)&&(IA(mask,x,y,z-1)==1)) && ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
else
IA(mask_dil,x,y,z)=0;
}
/* }}} */
}
/* }}} */
else
/* {{{ dilate mask by one */
{
for (z=0; z<z_size; z++)
for (y=0; y<y_size; y++)
for (x=0; x<x_size; x++)
{
if ( (IA(mask,x,y,z)==1) ||
((x>0)&&(IA(mask,x-1,y,z)==1)) || ((x<x_size-1)&&(IA(mask,x+1,y,z)==1)) ||
((y>0)&&(IA(mask,x,y-1,z)==1)) || ((y<y_size-1)&&(IA(mask,x,y+1,z)==1)) ||
((z>0)&&(IA(mask,x,y,z-1)==1)) || ((z<z_size-1)&&(IA(mask,x,y,z+1)==1)) )
IA(mask_dil,x,y,z)=1;
}
}
/* }}} */
free(mask);
printf("reading transformed images and applying mask\n");
sprintf(filename,"%s_halfwayto_%s",argv[1],argv[2]);
avw_read(filename,&im);
in1=im.i;
lowest=in1[0];
for(i=0;i<size;i++)
if (in1[i]<lowest) lowest=in1[i];
for(i=0;i<size;i++)
in1[i]=(in1[i]-lowest)*mask_dil[i];
free(mask_dil);
/* }}} */
/* {{{ mallocs, recentre 1D arrays */
if ((arrA = calloc(10*(CORRWIDTH+SEARCH),sizeof(float)))==NULL)
{
printf("aaargh\n");
exit(1);
}
if ((arrB = calloc(10*(CORRWIDTH+SEARCH),sizeof(float)))==NULL)
{
printf("aaargh\n");
exit(1);
}
if ((arr1 = calloc(10*(CORRWIDTH+SEARCH),sizeof(float)))==NULL)
{
printf("aaargh\n");
exit(1);
}
if ((arr2 = calloc(10*(CORRWIDTH+SEARCH),sizeof(float)))==NULL)
{
printf("aaargh\n");
exit(1);
}
arrA = arrA + 5*(CORRWIDTH+SEARCH);
arrB = arrB + 5*(CORRWIDTH+SEARCH);
arr1 = arr1 + 5*(CORRWIDTH+SEARCH);
arr2 = arr2 + 5*(CORRWIDTH+SEARCH);
/* }}} */
/* {{{ do segmentation on image 1 */
{
FILE *tmpfp;
/* sprintf(thestring,"%s_halfwayto_%s_brain_seg.hdr",argv[1],argv[2]);*/
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* if((tmpfp=fopen(thestring,"rb"))==NULL)*/ /* test for existing segmentation output */
if(1)
{
char segtype[100];
if (seg2) sprintf(segtype,"-c 2"); else segtype[0]=0;
printf("saving image 1 to disk prior to segmentation\n");
im.i=in1;
sprintf(thestring,"%s_halfwayto_%s_brain",argv[1],argv[2]);
avw_write(thestring,im);
free(in1);
sprintf(thestring,"%s/bin/fast %s %s %s_halfwayto_%s_brain > %s_halfwayto_%s_brain.vol 2>&1",
fsldir,segtype,segoptions,argv[1],argv[2],argv[1],argv[2]);
printf("%s\n",thestring);
system(thestring);
}
else
{
printf("using previously carried out segmentation\n");
free(in1);
}
}
/* }}} */
/* {{{ read segmentation output into edges1 and simplify; reread in1 and in2 */
printf("finding brain edges\n");
sprintf(filename,"%s_halfwayto_%s_brain_seg",argv[1],argv[2]);
avw_read(filename,&im);
seg1=im.i;
for(x=0;x<size;x++)
if (seg1[x]>1)
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
seg1[x]=1;
else
seg1[x]=0;
if (edge_masking)
{
sprintf(filename,"%s_halfwayto_%s_valid_mask",argv[1],argv[2]);
avw_read(filename,&im);
m1=im.i;
}
sprintf(filename,"%s_halfwayto_%s",argv[1],argv[2]);
avw_read(filename,&im);
in1=im.i;
sprintf(filename,"%s_halfwayto_%s",argv[2],argv[1]);
avw_read(filename,&im);
in2=im.i;
if ((flow = calloc(x_size*y_size*z_size,sizeof(float)))==NULL)
{
printf("aaargh\n");
exit(1);
}
/* }}} */
/* {{{ find segmentation-based edges in image 1 and flow */
printf("finding flow\n");
count=0;
total=0;
ignore_bottom_slices=MAX(1,ignore_bottom_slices);
ignore_top_slices=MAX(1,ignore_top_slices);
for (z=ignore_bottom_slices; z<z_size-ignore_top_slices; z++)
for (y=1; y<y_size-1; y++)
for (x=1; x<x_size-1; x++)
{
if ( (IA(seg1,x,y,z)>0.5) && /* not background or CSF */
( (IA(seg1,x+1,y,z)<0.5) || (IA(seg1,x-1,y,z)<0.5) ||
(IA(seg1,x,y+1,z)<0.5) || (IA(seg1,x,y-1,z)<0.5) ||
(IA(seg1,x,y,z+1)<0.5) || (IA(seg1,x,y,z-1)<0.5) ) &&
( ( ! edge_masking ) || ( IA(m1,x,y,z)>0 ) ) )
{
int pos, neg, r, rr, rrr, d, X, Y, Z;
float ss, maxss, segvalpos=0, segvalneg=0;
image_struct im1=im, im2=im;
/* {{{ find local gradient and derive unit normal */
ex = ( 10*(IA(in1,x+1,y,z)-IA(in1,x-1,y,z)) +
5*(IA(in1,x+1,y+1,z)+IA(in1,x+1,y-1,z)+IA(in1,x+1,y,z+1)+IA(in1,x+1,y,z-1)-
IA(in1,x-1,y+1,z)-IA(in1,x-1,y-1,z)-IA(in1,x-1,y,z+1)-IA(in1,x-1,y,z-1)) +
2*(IA(in1,x+1,y+1,z+1)+IA(in1,x+1,y-1,z+1)+IA(in1,x+1,y+1,z-1)+IA(in1,x+1,y-1,z-1)-
IA(in1,x-1,y+1,z+1)-IA(in1,x-1,y-1,z+1)-IA(in1,x-1,y+1,z-1)-IA(in1,x-1,y-1,z-1)) ) / 38;
ey = ( 10*(IA(in1,x,y+1,z)-IA(in1,x,y-1,z)) +
5*(IA(in1,x+1,y+1,z)+IA(in1,x-1,y+1,z)+IA(in1,x,y+1,z+1)+IA(in1,x,y+1,z-1)-
IA(in1,x+1,y-1,z)-IA(in1,x-1,y-1,z)-IA(in1,x,y-1,z+1)-IA(in1,x,y-1,z-1)) +
2*(IA(in1,x+1,y+1,z+1)+IA(in1,x-1,y+1,z+1)+IA(in1,x+1,y+1,z-1)+IA(in1,x-1,y+1,z-1)-
IA(in1,x+1,y-1,z+1)-IA(in1,x-1,y-1,z+1)-IA(in1,x+1,y-1,z-1)-IA(in1,x-1,y-1,z-1)) ) / 38;
ez = ( 10*(IA(in1,x,y,z+1)-IA(in1,x,y,z-1)) +
5*(IA(in1,x,y+1,z+1)+IA(in1,x,y-1,z+1)+IA(in1,x+1,y,z+1)+IA(in1,x-1,y,z+1)-
IA(in1,x,y+1,z-1)-IA(in1,x,y-1,z-1)-IA(in1,x+1,y,z-1)-IA(in1,x-1,y,z-1)) +
2*(IA(in1,x+1,y+1,z+1)+IA(in1,x+1,y-1,z+1)+IA(in1,x-1,y+1,z+1)+IA(in1,x-1,y-1,z+1)-
IA(in1,x+1,y+1,z-1)-IA(in1,x+1,y-1,z-1)-IA(in1,x-1,y+1,z-1)-IA(in1,x-1,y-1,z-1)) ) / 38;
tmpf = sqrt(ex*ex+ey*ey+ez*ez);
if (tmpf>0)
{
ex/=(double)tmpf;
ey/=(double)tmpf;
ez/=(double)tmpf;
}
/* }}} */
if ( (!ignore_z) ||
( (ABS(ez)<ABS(ex)) && (ABS(ez)<ABS(ey)) ) )
{
/* {{{ fill 1D arrays and differentiate TLI */
im1.i=in1;
im2.i=in2;
memset(arrA-5*(CORRWIDTH+SEARCH),0,sizeof(float)*10*(CORRWIDTH+SEARCH));
memset(arrB-5*(CORRWIDTH+SEARCH),0,sizeof(float)*10*(CORRWIDTH+SEARCH));
memset(arr1-5*(CORRWIDTH+SEARCH),0,sizeof(float)*10*(CORRWIDTH+SEARCH));
memset(arr2-5*(CORRWIDTH+SEARCH),0,sizeof(float)*10*(CORRWIDTH+SEARCH));
/*IA(flow,x,y,z) = 1;*/ /* DEBUG colour edge point */
/*if ((x==53)&&(y==61)&&(z==78)) {*/ /* DEBUG */
/* printf("normal=(%f %f %f) ",ex,ey,ez);*/ /* DEBUG */
arrA[0]=IA(in1,x,y,z); /* most odd - these lines give -32768 if full optimisation is turned on */
arrB[0]=IA(in2,x,y,z);
/*IA(flow,x,y,z) = 3;*/ /* DEBUG colour central point */
pos=0;
d=1; X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if ( (X>0) && (X<x_size-1) && (Y>0) && (Y<y_size-1) && (Z>0) && (Z<z_size-1) )
{
arrA[1]=TLI(im1,x+d*ex,y+d*ey,z+d*ez);
arrB[1]=TLI(im2,x+d*ex,y+d*ey,z+d*ez);
pos=-1;
segvalpos = IA(seg1,X,Y,Z);
for(d=2;d<=CORRWIDTH+SEARCH+1;d++)
{
X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if ( (X>0) && (X<x_size-1) && (Y>0) && (Y<y_size-1) && (Z>0) && (Z<z_size-1) )
{
if ( (pos<0) && (IA(seg1,X,Y,Z)!=segvalpos) )
pos=d-1;
arrA[d]=TLI(im1,x+d*ex,y+d*ey,z+d*ez);
arrB[d]=TLI(im2,x+d*ex,y+d*ey,z+d*ez);
}
else
break;
}
if ( (pos<0) || (pos>CORRWIDTH) )
pos=CORRWIDTH;
if (pos==d-1)
pos=d-2;
}
/* {{{ COMMENT DEBUG draw search space */
#ifdef FoldingComment
for(d=1;d<=SEARCH+pos;d++)
{
X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if (d<=pos)
IA(flow,X,Y,Z) = 7;
else
IA(flow,X,Y,Z) = 5;
}
#endif
/* }}} */
neg=0;
d=-1; X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if ( (X>0) && (X<x_size-1) && (Y>0) && (Y<y_size-1) && (Z>0) && (Z<z_size-1) )
{
arrA[-1]=TLI(im1,x+d*ex,y+d*ey,z+d*ez);
arrB[-1]=TLI(im2,x+d*ex,y+d*ey,z+d*ez);
neg=1;
segvalneg = IA(seg1,X,Y,Z);
for(d=-2;d>=-CORRWIDTH-SEARCH-1;d--)
{
X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if ( (X>0) && (X<x_size-1) && (Y>0) && (Y<y_size-1) && (Z>0) && (Z<z_size-1) )
{
if ( (neg>0) && (IA(seg1,X,Y,Z)!=segvalneg) )
neg=d+1;
arrA[d]=TLI(im1,x+d*ex,y+d*ey,z+d*ez);
arrB[d]=TLI(im2,x+d*ex,y+d*ey,z+d*ez);
}
else
break;
}
if ( (neg>0) || (neg<-CORRWIDTH) )
neg=-CORRWIDTH;
if (neg==d+1)
neg=d+2;
}
/* {{{ COMMENT DEBUG draw search space */
#ifdef FoldingComment
for(d=-1;d>=-SEARCH+neg;d--)
{
X=FTOI(x+d*ex); Y=FTOI(y+d*ey); Z=FTOI(z+d*ez);
if (d>=neg)
IA(flow,X,Y,Z) = 7;
else
IA(flow,X,Y,Z) = 5;
}
#endif
/* }}} */
/*printf("<%d %d %d %d> ",neg,pos,(int)segvalneg,(int)segvalpos);*/ /* DEBUG*/
for(d=-SEARCH-CORRWIDTH-1;d<=SEARCH+CORRWIDTH+1;d++)
{
arr1[d]=exp(-0.5*pow((2.0*d-neg-pos)/(pos-neg),4.0)) * (arrA[d+1]-arrA[d-1]);
arr2[d]=exp(-0.5*pow((2.0*d-neg-pos)/(pos-neg),4.0)) * (arrB[d+1]-arrB[d-1]);
/*printf("[%d %d %d %d %d] ",d,(int)arrA[d],(int)arrB[d],(int)arr1[d],(int)arr2[d]);*/ /* DEBUG */
}
/* }}} */
/* {{{ find position of maximum correlation */
for(r=-SEARCH, maxss=0, rrr=0; r<=SEARCH; r++)
{
for(rr=neg, ss=0; rr<=pos; rr++)
ss+=arr1[rr]*arr2[rr+r];
arrA[r]=ss;
/* printf("[%d %.2f] ",r,ss);*/ /* DEBUG */
if ( (ss>maxss) && (r>-SEARCH) && (r<SEARCH) )
{
maxss=ss;
rrr=r;
}
}
/* now find this max to sub-voxel accuracy */
tmpf = arrA[rrr+1] + arrA[rrr-1] - 2*arrA[rrr];
if (tmpf!=0)
tmpf = 0.5 * (arrA[rrr-1]-arrA[rrr+1]) / tmpf;
if ( (tmpf<-0.5) || (tmpf>0.5) ) /* protect against sub-voxel fit not making sense */
tmpf=0;
else
tmpf+=rrr;
tmpf = (segvalneg-segvalpos)*tmpf; /* use segmentation info to get directionality */
/*printf(" tmpf=%f\n",tmpf);*/ /* DEBUG */
IA(flow,x,y,z) = tmpf; /* turn off if DEBUGging */
total += tmpf;
count ++;
/*}*/ /* DEBUG */
/* }}} */
}
}
}
/* }}} */
/* {{{ output flow image */
if (flow_output)
{
im.i=flow;
im.dt=DT_FLOAT;
sprintf(thestring,"%s_to_%s_flow",argv[1],argv[2]);
avw_write(thestring,im);
}
/* }}} */
/* {{{ final outputs */
printf("AREA %.4f mm^2\n", count*voxel_area );
printf("VOLC %.4f mm^3\n", total*voxel_volume );
printf("RATIO %.4f mm\n", (total*voxel_volume) / (count*voxel_area) ); /* mean perpendicular edge motion; l in the equations */
printf("PBVC %.4f %%\n", (calib*30*total*voxel_volume) / (count*voxel_area) );
/* }}} */
return 0;
}
/* }}} */