-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclean_surface_labels.c
389 lines (314 loc) · 10.7 KB
/
clean_surface_labels.c
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
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
/*
Clean up the surface labels obtained using volume_object_evaluate when
intersecting a cortical surface with the volumetric ANIMAL segmentation.
Sometimes, there are vertices on the surface that get tagged as CSF but
these should be projected to the nearest non-CSF tissue label.
fix_surface_labels surface.obj labels.txt new_labels.txt
Values: surface.obj = the surface
labels.txt = input labels (range 0 to 255)
new_labels.txt = corrected labels
By: Claude Lepage, February 2007.
COPYRIGHT: McConnell Brain Imaging Center,
Montreal Neurological Institute,
Department of Psychology,
McGill University, Montreal, Quebec, Canada.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
*/
#include <stdio.h>
#include <volume_io.h>
#include <bicpl.h>
#define BG 0
#define CSF 255
// Prototypes of functions in this file.
static void usage( char * );
private Status read_surface_obj( STRING, int *, Point *[],
Vector *[], int *[], int **[] );
private Status get_surface_neighbours( polygons_struct *, int *[],
int ** [] );
private void read_scalar( int n_points, int scalar[], char * in_file );
private void save_scalar( int n_points, int scalar[], char * out_file );
// Main program.
int main( int argc, char * argv[] ) {
int i, j, nn, count, min_lbl, max_lbl, min_count, max_count, modified;
int n_points; // number of grid points
Point * coords; // coordinates
Vector * normals; // normal vectors
int * n_ngh = NULL; // node neighbours (inverse connectivity)
int ** ngh = NULL;
FILE * fp;
if( argc < 3 ) {
usage( argv[0] );
return( ERROR );
}
// Parse the command line arguments for the file names.
// Read the surface file.
if( read_surface_obj( argv[1], &n_points, &coords, &normals,
&n_ngh, &ngh ) != OK ) {
return( ERROR );
}
int * labels = (int *)malloc( n_points * sizeof( int ) );
read_scalar( n_points, labels, argv[2] );
do {
count = 0;
modified = 0;
for( i = 0; i < n_points; i++ ) {
min_lbl = 999999;
max_lbl = -1;
for( j = 0; j < n_ngh[i]; j++ ) {
nn = ngh[i][j];
if( labels[nn] < min_lbl && labels[nn] != BG ) min_lbl = labels[nn];
if( labels[nn] > max_lbl && labels[nn] != CSF ) max_lbl = labels[nn];
}
if( min_lbl == max_lbl ) {
if( labels[i] != min_lbl ) {
if( min_lbl != CSF && min_lbl != BG ) {
labels[i] = min_lbl;
modified++;
}
}
#if 1
} else {
if( labels[i] != min_lbl && labels[i] != max_lbl ) {
min_count = 0;
max_count = 0;
for( j = 0; j < n_ngh[i]; j++ ) {
nn = ngh[i][j];
if( labels[nn] == min_lbl ) min_count++;
if( labels[nn] == max_lbl ) max_count++;
}
if( min_count + max_count == n_ngh[i] ) {
modified++;
if( min_count >= max_count ) {
labels[i] = min_lbl;
} else {
labels[i] = max_lbl;
}
}
}
#endif
}
if( labels[i] == CSF ) {
count++;
for( j = 0; j < n_ngh[i]; j++ ) {
nn = ngh[i][j];
if( labels[nn] != BG && labels[nn] != CSF ) {
labels[i] = labels[nn];
modified++;
break;
}
}
}
}
printf( "modified %d labels\n", modified );
} while( modified > 0 );
if( count > 0 ) printf( "There are %d CSF labels left\n", count );
save_scalar( n_points, labels, argv[3] );
free( labels );
if( coords ) FREE( coords );
if( normals ) FREE( normals );
if( n_ngh ) FREE( n_ngh );
if( ngh ) {
FREE( ngh[0] ); // this is ngh_array
FREE( ngh );
}
return 0;
}
// -------------------------------------------------------------------
// Help message on how to use this module.
//
static void usage( char * executable_name ) {
STRING usage_format = "\
Usage: %s surface.obj labels.txt new_labels.txt\n\
Values: surface.obj = the surface\n\
labels.txt = input labels (range 0 to 255)\n\
new_labels.txt = corrected labels\n\n";
print_error( usage_format, executable_name );
}
// -------------------------------------------------------------------
// Load the cortical surface.
//
// filename: name of the .obj file
// n_points: the number of the vertices
// points: (x,y,z) coordinates
// normals: normal vectors
// n_neighbours: number of vertices around each node
// neighbours: the set of ordered triangle consisting of the vertices
//
private Status read_surface_obj( STRING filename,
int * n_points,
Point * points[],
Vector * normals[],
int * n_neighbours[],
int ** neighbours[] ) {
int i, n_objects;
object_struct ** object_list;
polygons_struct * surface;
File_formats format;
STRING expanded;
expanded = expand_filename( filename ); // why?????
int err = input_graphics_file( expanded, &format, &n_objects,
&object_list );
if( err != OK ) {
print_error( "Error reading file %s\n", expanded );
return( ERROR );
}
if( n_objects != 1 ||
( n_objects == 1 && get_object_type(object_list[0]) != POLYGONS ) ) {
print_error( "Error in contents of file %s\n", expanded );
return( ERROR );
}
delete_string( expanded );
surface = get_polygons_ptr( object_list[0] );
// Make a copy of the coordinates and the normals, since
// delete_object_list will destroy them.
*n_points = surface->n_points;
ALLOC( *points, surface->n_points );
ALLOC( *normals, surface->n_points );
for( i = 0; i < *n_points; i++ ) {
(*points)[i].coords[0] = surface->points[i].coords[0];
(*points)[i].coords[1] = surface->points[i].coords[1];
(*points)[i].coords[2] = surface->points[i].coords[2];
(*normals)[i].coords[0] = surface->normals[i].coords[0];
(*normals)[i].coords[1] = surface->normals[i].coords[1];
(*normals)[i].coords[2] = surface->normals[i].coords[2];
}
get_surface_neighbours( surface, n_neighbours, neighbours );
delete_object_list( n_objects, object_list );
return( OK );
}
// -------------------------------------------------------------------
// Construct the edges around each node. The edges are sorted to
// make an ordered closed loop.
//
private Status get_surface_neighbours( polygons_struct * surface,
int * n_neighbours_return[],
int ** neighbours_return[] ) {
int i, j, k, jj;
int * tri;
int * n_ngh;
int ** ngh;
int * ngh_array;
// Check if all polygons are triangles.
if( 3 * surface->n_items != surface->end_indices[surface->n_items-1] ) {
printf( "Surface must contain only triangular polygons.\n" );
return ERROR;
}
// Check if the node numbering starts at 0 or 1.
int min_idx, max_idx;
min_idx = 100*surface->n_points; // anything big
max_idx = 0; // anything small
for( i = 0; i < 3*surface->n_items; i++ ) {
if( surface->indices[i] < min_idx ) min_idx = surface->indices[i];
if( surface->indices[i] > max_idx ) max_idx = surface->indices[i];
}
// Shift numbering to start at zero, for array indexing. Note
// that we don't care if surface->indices array is modified.
if( min_idx != 0 ) {
for( i = 0; i < 3*surface->n_items; i++ ) {
surface->indices[i] -= min_idx;
}
}
// Count number of triangles attached to each node.
ALLOC( n_ngh, surface->n_points );
ALLOC( ngh, surface->n_points );
ALLOC( ngh_array, 3*surface->n_items );
for( i = 0; i < surface->n_points; i++ ) {
n_ngh[i] = 0;
}
for( i = 0; i < 3*surface->n_items; i++ ) {
n_ngh[surface->indices[i]]++;
ngh_array[i] = -1;
}
int max_ngh = 0;
int sum_ngh = 0;
for( i = 0; i < surface->n_points; i++ ) {
ngh[i] = &(ngh_array[sum_ngh]);
sum_ngh += n_ngh[i];
max_ngh = MAX( max_ngh, n_ngh[i] );
}
// At first, store the indices of the triangles in the neighbours.
for( i = 0; i < surface->n_items; i++ ) {
for( j = 0; j < 3; j++ ) {
jj = surface->indices[3*i+j];
for( k = 0; k < n_ngh[jj]; k++ ) {
if( ngh[jj][k] == -1 ) {
ngh[jj][k] = i;
break;
}
}
}
}
// Now create a sort closed loop of the node neighbours.
// This is needed by the parametric=0 FEM algorithm.
//
// 1 ----- 2
// /\ /\
// / \ / \
// 0 ----P---- 3
// \ / \ /
// \/ \/
// 5 ----- 4
//
int * tmp;
ALLOC( tmp, 2*max_ngh );
for( i = 0; i < surface->n_points; i++ ) {
for( k = 0; k < n_ngh[i]; k++ ) {
tri = &(surface->indices[3*ngh[i][k]]);
for( j = 0; j < 3; j++ ) {
if( tri[j] == i ) break;
}
tmp[2*k+0] = tri[(j+1)%3];
tmp[2*k+1] = tri[(j+2)%3];
}
ngh[i][0] = tmp[0];
ngh[i][1] = tmp[1];
for( k = 2; k < n_ngh[i]; k++ ) {
for( j = 1; j < n_ngh[i]; j++ ) {
if( tmp[2*j] == ngh[i][k-1] || tmp[2*j+1] == ngh[i][k-1] ) {
if( tmp[2*j] == ngh[i][k-1] ) {
ngh[i][k] = tmp[2*j+1];
} else {
ngh[i][k] = tmp[2*j];
}
tmp[2*j] = -1;
tmp[2*j+1] = -1;
break;
}
}
}
}
*n_neighbours_return = n_ngh;
*neighbours_return = ngh;
FREE( tmp );
return OK;
}
// -------------------------------------------------------------------
// Read a scalar from a file.
//
private void read_scalar( int n_points, int scalar[], char * in_file ) {
int i;
float val;
FILE * fp = fopen( in_file, "r" );
for( i = 0; i < n_points; i++ ) {
fscanf( fp, "%f", &val );
scalar[i] = (int)( val + 0.25 ); // truncate to nearest if noise from minc ranges.
}
fclose( fp );
}
// -------------------------------------------------------------------
// Save a scalar to a file.
//
private void save_scalar( int n_points, int scalar[], char * out_file ) {
int i;
FILE * fp = fopen( out_file, "w" );
for( i = 0; i < n_points; i++ ) {
fprintf( fp, "%d\n", scalar[i] );
}
fclose( fp );
}