| Bsoft 2.0.5 Bernard's software package

bgraphseg.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "options.h"
11 #include "utilities.h"
12 #include "timer.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17
18 /* Usage assistance */
19 const char* use[] = {
20 " ",
21 "Usage: bgraphseg [options] input.img output.img",
22 "-----------------------------------------------",
23 "Segment images into density regions using a nondirected graph.",
24 " ",
25 "Actions:",
26 "-invert Invert density in the image before other operations.",
27 "-rescale -0.1,5.2 Rescale data to average and standard deviation before other operations.",
28 "-smooth 0.8 Smooth using a gaussian kernel with the given width.",
29 "-type srm Type of segmentation (simple, srm).",
30 "-minimum 2300 Join segments smaller than this size.",
31 "-voxel 23,241,193,1 Select the segment containing this voxel with an optional inversion flag.",
32 "-colorize Generate random segment colors.",
33 " ",
34 "Parameters:",
35 "-verbose 7 Verbose output.",
36 "-datatype u Force writing of a new data type.",
37 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
38 "-connectivity Flag to use full neighbor connectivity (default 2*dimension).",
39 "-complexity 1.6 Segmentation complexity: larger value gives more regions (default 1).",
40 " ",
41 "Input:",
42 "-Mask mask.tif Mask to limit segmentation.",
43 " ",
44 NULL
45 };
46
47 int main(int argc, char* argv[])
48 {
49 // Initialize variables
50 DataType nudatatype(Unknown_Type); // Conversion to new type
51 int setinvert(0); // Flag to invert density
52 double nuavg(0), nustd(0); // Rescaling to average and stdev
53 double sigma(0); // Gaussian kernel width
54 int type(0); // Flag and segmentation type
55 int connect_type(0); // Flag for full connectivity
56 long min_size(0); // Minimum size of a blob to keep
57 Vector3<long> voxel(-1,0,0); // Voxel to select a segement
58 int colorize(0); // Generate color mask
59 int mask_invert(0); // Invert voxel-selected mask
60 double complexity(1); // Complexity value
61 Vector3<double> origin; // New image origin
62 int set_origin(0); // Flag to set origin
63 Bstring maskfile; // Mask file
64 Bstring pexfile; // Excised feature file
65
66 int optind;
67 Boption* option = get_option_list(use, argc, argv, optind);
68 Boption* curropt;
69 for ( curropt = option; curropt; curropt = curropt->next ) {
70 if ( curropt->tag == "datatype" )
71 nudatatype = curropt->datatype();
72 if ( curropt->tag == "invert" )
73 setinvert = 1;
74 if ( curropt->tag == "rescale" )
75 if ( curropt->values(nuavg, nustd) < 2 )
76 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
77 if ( curropt->tag == "smooth" )
78 if ( ( sigma = curropt->value.real() ) < 0.001 )
79 cerr << "-smooth: A sigma value must be specified!" << endl;
80 if ( curropt->tag == "type" ) {
81 if ( curropt->value.contains("sim") ) type = 1;
82 if ( curropt->value.contains("srm") ) type = 2;
83 }
84 if ( curropt->tag == "connectivity" )
85 connect_type = 1;
86 if ( curropt->tag == "minimum" )
87 if ( ( min_size = curropt->value.integer() ) < 1 )
88 cerr << "-minimum: A minimu segment size must be specified!" << endl;
89 if ( curropt->tag == "voxel" )
90 if ( curropt->values(voxel[0], voxel[1], voxel[2], mask_invert) < 3 )
91 cerr << "-voxel: A voxel must be specified!" << endl;
92 if ( curropt->tag == "colorize" )
93 colorize = 1;
94 if ( curropt->tag == "complexity" )
95 if ( ( complexity = curropt->value.real() ) < 0.001 )
96 cerr << "-complexity: A complexity value must be specified!" << endl;
97 if ( curropt->tag == "origin" ) {
98 if ( curropt->value[0] == 'c' ) {
99 set_origin = 2;
100 } else {
101 origin = curropt->origin();
102 set_origin = 1;
103 }
104 }
105 if ( curropt->tag == "Mask" )
106 maskfile = curropt->filename();
107 if ( curropt->tag == "Feature" )
108 pexfile = curropt->filename();
109 }
110 option_kill(option);
111
112 double ti = timer_start();
113
114 // Read the input file
115 Bimage* p = read_img(argv[optind++], 1, 0);
116
117 p->label(get_cmd_line(argc, argv));
118
119 Bimage* pmask = NULL;
120 if ( maskfile.length() ) pmask = read_img(maskfile, 1, 0);
121
122 if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
123
124 if ( nudatatype > p->data_type() ) p->change_type(nudatatype);
125
126 if ( set_origin ) {
127 if ( set_origin == 2 ) p->origin(p->default_origin());
128 else p->origin(origin);
129 }
130
131 if ( setinvert ) p->invert();
132
133 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
134
135 long ksize(9);
136 if ( sigma > 0.001 ) {
137 ksize = 2*( (int) (4*sigma + 0.9) ) + 1;
138 p->filter_gaussian(ksize, sigma);
139 }
140
141 Bimage* pseg = NULL;
142 if ( type ) {
143 pseg = p->graph_segment(type, pmask, connect_type, complexity, min_size, 1);
144 delete p;
145 p = pseg;
146 }
147
148 if ( voxel[0] >= 0 ) {
149 p->levelmask_select(0, voxel);
150 if ( mask_invert ) p->mask_invert();
151 }
152
153 if ( colorize ) p->levelmask_colorize();
154
155 // Write an output file if a file name is given
156 if ( optind < argc ) {
157 p->label(get_cmd_line(argc, argv));
158 write_img(argv[optind], p, 0);
159 }
160
161 delete p;
162 delete pmask;
163
164 if ( verbose & VERB_TIME )
165 timer_report(ti);
166
167 bexit(0);
168 }
169
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::mask_invert
long mask_invert()
Inverts a mask.
Definition: Bimage_mask.cpp:221
use
const char * use[]
Definition: bgraphseg.cpp:19
Bstring::length
long length() const
Definition: Bstring.cpp:263
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:325
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
Bimage::levelmask_select
long levelmask_select(Bstring &select_list)
Retains the selected levels in a multi-level mask.
Definition: Bimage_mask.cpp:1650
Bimage::levelmask_colorize
long levelmask_colorize()
Colorizes a multi-level mask with random color assignments.
Definition: Bimage_mask.cpp:1887
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Vector3< long >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
verbose
int verbose
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
main
int main(int argc, char *argv[])
Definition: bgraphseg.cpp:47
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::graph_segment
Bimage * graph_segment(int type=1, Bimage *pmask=NULL, int connect_type=0, double complexity=0, long min_size=0, int return_type=0)
Graph-based segmentation of an image.
Definition: Bimage_segment.cpp:1383
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563