Bsoft  2.1.0
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 
118  Bimage* pmask = NULL;
119  if ( maskfile.length() ) pmask = read_img(maskfile, 1, 0);
120 
121  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
122 
123  if ( nudatatype > p->data_type() ) p->change_type(nudatatype);
124 
125  if ( set_origin ) {
126  if ( set_origin == 2 ) p->origin(p->default_origin());
127  else p->origin(origin);
128  }
129 
130  if ( setinvert ) p->invert();
131 
132  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
133 
134  long ksize(9);
135  if ( sigma > 0.001 ) {
136  ksize = 2*( (int) (4*sigma + 0.9) ) + 1;
137  p->filter_gaussian(ksize, sigma);
138  }
139 
140  Bimage* pseg = NULL;
141  if ( type ) {
142  pseg = p->graph_segment(type, pmask, connect_type, complexity, min_size, 1);
143  delete p;
144  p = pseg;
145  }
146 
147  if ( voxel[0] >= 0 ) {
148  p->levelmask_select(0, voxel);
149  if ( mask_invert ) p->mask_invert();
150  }
151 
152  if ( colorize ) p->levelmask_colorize();
153 
154  // Write an output file if a file name is given
155  if ( optind < argc ) {
156  write_img(argv[optind], p, 0);
157  }
158 
159  delete p;
160  delete pmask;
161 
162  if ( verbose & VERB_TIME )
163  timer_report(ti);
164 
165  bexit(0);
166 }
167 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:324
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::levelmask_select
long levelmask_select(Bstring &select_list, int flag=0)
Retains the selected levels in a multi-level mask.
Definition: Bimage_mask.cpp:1650
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
main
int main(int argc, char *argv[])
Definition: bgraphseg.cpp:47
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
use
const char * use[]
Definition: bgraphseg.cpp:19
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
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:1443
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::mask_invert
long mask_invert()
Inverts a mask.
Definition: Bimage_mask.cpp:220
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bimage::levelmask_colorize
long levelmask_colorize()
Colorizes a multi-level mask with random color assignments.
Definition: Bimage_mask.cpp:2095
verbose
int verbose
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