-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdilate_volume_completely.c
171 lines (141 loc) · 5.62 KB
/
dilate_volume_completely.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
#include <volume_io/internal_volume_io.h>
#include <bicpl.h>
private void usage(
STRING executable )
{
STRING usage_str = "\n\
Usage: %s input.mnc output.mnc [max_dilations|-1] [6|26] [min_outside max_outside]\n\
[min_inside max_inside]\n\
Dilates all regions of volume until there are no values in the range\n\
(min_outside, max_outside), which defaults to 0 0\n\n";
print_error( usage_str, executable );
}
int main(
int argc,
char *argv[] )
{
STRING input_filename, output_filename;
Volume volume;
int n_neighs, iter, max_dilations;
int sizes[N_DIMENSIONS];
int x, y, z, n_changed;
int tx, ty, tz, n_dirs, *dx, *dy, *dz, dir;
BOOLEAN found;
Real max_value, test_value, value;
Real min_outside, max_outside;
Real min_inside, max_inside;
Real **output_buffer[2], **tmp;
Neighbour_types connectivity;
progress_struct progress;
initialize_argument_processing( argc, argv );
if( !get_string_argument( NULL, &input_filename ) ||
!get_string_argument( NULL, &output_filename ) )
{
usage( argv[0] );
return( 1 );
}
(void) get_int_argument( -1, &max_dilations );
(void) get_int_argument( 26, &n_neighs );
(void) get_real_argument( 0.0, &min_outside );
(void) get_real_argument( 0.0, &max_outside );
(void) get_real_argument( 0.0, &min_inside );
(void) get_real_argument( -1.0, &max_inside );
switch( n_neighs )
{
case 6: connectivity = FOUR_NEIGHBOURS; break;
case 26: connectivity = EIGHT_NEIGHBOURS; break;
default: print_error( "# neighs must be 6 or 26.\n" ); return( 1 );
}
if( input_volume( input_filename, 3, File_order_dimension_names,
NC_UNSPECIFIED, FALSE, 0.0, 0.0, TRUE, &volume,
(minc_input_options *) NULL ) != OK )
return( 1 );
n_dirs = get_3D_neighbour_directions( connectivity, &dx, &dy, &dz );
iter = 0;
get_volume_sizes( volume, sizes );
ALLOC2D( output_buffer[0], sizes[Y], sizes[Z] );
ALLOC2D( output_buffer[1], sizes[Y], sizes[Z] );
n_changed = 1;
while( (max_dilations < 0 || iter < max_dilations) && n_changed > 0 )
{
++iter;
n_changed = 0;
initialize_progress_report( &progress, FALSE, sizes[X], "Dilating" );
for_less( x, 0, sizes[X] )
{
for_less( y, 0, sizes[Y] )
{
for_less( z, 0, sizes[Z] )
{
value = get_volume_real_value( volume, x, y, z, 0, 0 );
found = FALSE;
if( min_outside <= value && value <= max_outside )
{
max_value = 0.0;
for_less( dir, 0, n_dirs )
{
tx = x + dx[dir];
ty = y + dy[dir];
tz = z + dz[dir];
if( tx >= 0 && tx < sizes[0] &&
ty >= 0 && ty < sizes[1] &&
tz >= 0 && tz < sizes[2] )
{
test_value = get_volume_real_value( volume,
tx, ty, tz, 0, 0 );
if( min_inside > max_inside ||
min_inside <= test_value &&
test_value <= max_inside )
{
if( !found )
{
max_value = test_value;
found = TRUE;
}
else
max_value = MAX(max_value,test_value);
}
}
}
}
if( !found )
max_value = value;
output_buffer[1][y][z] = max_value;
if( max_value != value )
++n_changed;
}
}
if( x > 0 )
{
for_less( y, 0, sizes[Y] )
for_less( z, 0, sizes[Z] )
{
set_volume_real_value( volume, x-1, y, z, 0, 0,
output_buffer[0][y][z] );
}
}
if( x == sizes[X]-1 )
{
for_less( y, 0, sizes[Y] )
for_less( z, 0, sizes[Z] )
{
set_volume_real_value( volume, x, y, z, 0, 0,
output_buffer[1][y][z] );
}
}
tmp = output_buffer[0];
output_buffer[0] = output_buffer[1];
output_buffer[1] = tmp;
update_progress_report( &progress, x + 1 );
}
terminate_progress_report( &progress );
print( "Iter: %d N changed: %d\n", iter, n_changed );
}
FREE2D( output_buffer[0] );
FREE2D( output_buffer[1] );
(void) output_modified_volume( output_filename, NC_UNSPECIFIED, FALSE,
0.0, 0.0, volume, input_filename,
"dilate_volume_completely\n",
NULL );
return( 0 );
}