Bsoft  2.1.0
Bernard's software package
bsegment.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "file_util.h"
11 #include "options.h"
12 #include "utilities.h"
13 #include "timer.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 /* Usage assistance */
19 const char* use[] = {
20 " ",
21 "Usage: bsegment [options] input.img/input.star [output.img]",
22 "-----------------------------------------------------------",
23 "Analyzes images for segmentation.",
24 " ",
25 "Actions:",
26 "-invert Invert density in the image before other operations.",
27 "-rescale -0.1,5.2 Rescale extracted regions to average and standard deviation.",
28 "-blobs 0.45,14,201,1.4 Show blobs above threshold (0.45) and set outside minimum (14) ",
29 " and maximum (201) sizes to value (1.4).",
30 " ",
31 "Mask generating actions:",
32 "-mass 342k Find threshold associated with this molecular weight (Da)",
33 " value can be given in Da, kDa (with k), MDa (with m) or Gda (with g).",
34 " Output binary mask (data type byte, values 0 and 1).",
35 "-threshold 1.54 Report mass calculated from the volume included at this threshold.",
36 " Output binary mask (data type byte, values 0 and 1).",
37 "-multiple 1.2 Generate multiple regions above this threshold.",
38 "-kmeans 12,25,0.8 K-means segmentation: K, iterations and density:distance ratio.",
39 "-innervolume 2.3,15 Calculate internal volume below this threshold, output mask frequency.",
40 " ",
41 "Selections:",
42 "-select 1,5-7 Select levels in a multi-level mask.",
43 "-combine 1,5-7 Combine levels to create a binary mask.",
44 "-voxel 15,50,33 Select a region around this voxel.",
45 " ",
46 "Parameters:",
47 "-verbose 7 Verbose output.",
48 "-datatype u Force writing of a new data type.",
49 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
50 "-sign -1 Sign for thresholding (default 0=set by sign of threshold-average;",
51 " 1=positive density; -1=negative density).",
52 "-rho 0.8 Protein density (default 0.81 Da/A3, use with -mass).",
53 " ",
54 "Input:",
55 "-Mask mask.tif Input mask (must be integer and same size as input image).",
56 " ",
57 "Output:",
58 "-Region region.tif File name for the excised region.",
59 " ",
60 NULL
61 };
62 
63 int main(int argc, char* argv[])
64 {
65  // Initialize variables
66  DataType nudatatype(Unknown_Type); // Conversion to new type
67  int setinvert(0); // Flag to invert density
68  double nuavg(0), nustd(0); // Rescaling to average and stdev
69  double threshold(-1e36); // Threshold for defining regions
70  long nregions(0); // Number of regions for K-means
71  long max_iter(10); // Maximum kmeans iterations
72  double ratio(1); // Density-distance ratio
73  int region_mask(0); // Flag to generate a mask of regions
74  Vector3<double> origin; // New image origin
75  int set_origin(0); // Flag to set origin
76  int sign(0); // Sign for region mask threshold
77  double blob_threshold(-1e36); // Threshold for defining blobs
78  double min_size(0); // Minimum size of a blob to keep
79  double max_size(1000000000); // Maximum size of a blob to keep
80  double setvalue(0); // Value to set a deleted region to
81  double mol_weight(0); // Molecular weight for threshold finding
82  int internal_volume(0); // Flag to calculate internal volume
83  int mask_out_freq(0); // Frequency to output inner volume mask
84  double rho(RHO); // Protein density in Da/A3
85  int nvox(0); // Number of voxel coordinates
86  Bstring level_select; // Selection of levels
87  int select_flag(1); // 0=combine, 1=multi-level
88  Vector3<long> voxel; // Voxel coordinates for region extraction
89  Bstring mask_file = NULL; // Mask file
90  Bstring region_file = NULL; // Excised region file
91 
92  int i, optind;
93  Boption* option = get_option_list(use, argc, argv, optind);
94  Boption* curropt;
95  for ( curropt = option; curropt; curropt = curropt->next ) {
96  if ( curropt->tag == "datatype" )
97  nudatatype = curropt->datatype();
98  if ( curropt->tag == "invert" )
99  setinvert = 1;
100  if ( curropt->tag == "rescale" )
101  if ( curropt->values(nuavg, nustd) < 2 )
102  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
103  if ( curropt->tag == "blobs" ) {
104  if ( ( i = curropt->values(blob_threshold, min_size, max_size, setvalue ) ) < 1 )
105  cerr << "-blobs: A threshold must be specified!" << endl;
106  else
107  if ( i < 4 ) setvalue = blob_threshold;
108  }
109  if ( curropt->tag == "mass" )
110  mol_weight = curropt->mass();
111  if ( curropt->tag == "threshold" )
112  if ( ( threshold = curropt->value.real() ) < -1e30 )
113  cerr << "-threshold: A threshold must be specified!" << endl;
114  if ( curropt->tag == "innervolume" ) {
115  if ( curropt->values(threshold, mask_out_freq ) < 1 )
116  cerr << "-innervolume: A threshold must be specified!" << endl;
117  else
118  internal_volume = 1;
119  }
120  if ( curropt->tag == "multiple" ) {
121  if ( curropt->values(threshold, sign) < 1 )
122  cerr << "-multiple: A threshold must be specified!" << endl;
123  else
124  region_mask = 1;
125  }
126  if ( curropt->tag == "kmeans" )
127  if ( curropt->values(nregions, max_iter, ratio) < 1 )
128  cerr << "-kmeans: A number of regions must be specified!" << endl;
129  if ( curropt->tag == "select" ) {
130  level_select = curropt->value;
131  if ( level_select.length() < 1 )
132  cerr << "-select: Level numbers must be specified!" << endl;
133  else
134  select_flag = 1;
135  }
136  if ( curropt->tag == "combine" ) {
137  level_select = curropt->value;
138  if ( level_select.length() < 1 )
139  cerr << "-combine: Level numbers must be specified!" << endl;
140  else
141  select_flag = 0;
142  }
143  if ( curropt->tag == "voxel" ) {
144  voxel = curropt->size();
145  nvox = 1;
146  }
147  if ( curropt->tag == "origin" ) {
148  if ( curropt->value[0] == 'c' ) {
149  set_origin = 2;
150  } else {
151  origin = curropt->origin();
152  set_origin = 1;
153  }
154  }
155  if ( curropt->tag == "sign" )
156  if ( ( sign = curropt->value.integer() ) == 0 )
157  cerr << "-sign: A sign value must be specified!" << endl;
158  if ( curropt->tag == "rho" )
159  if ( ( rho = curropt->value.real() ) < 0.001 )
160  cerr << "-rho: A protein density value must be specified!" << endl;
161  if ( curropt->tag == "Mask" )
162  mask_file = curropt->filename();
163  if ( curropt->tag == "Region" )
164  region_file = curropt->filename();
165  }
166  option_kill(option);
167 
168  double ti = timer_start();
169 
170  // Read the input file
171  Bstring filename(argv[optind++]);
172  Bimage* p = read_img(filename, 1, -1);
173 
174 
175  if ( nudatatype == Unknown_Type )
176  nudatatype = p->data_type();
177 
178  if ( set_origin ) {
179  if ( set_origin == 2 ) p->origin(p->size()/2);
180  else p->origin(origin);
181  }
182 
183  if ( setinvert ) p->invert();
184 
185  if ( blob_threshold > -1e29 )
186  p->blobs(blob_threshold, min_size, max_size, setvalue, sign);
187 
188  if ( mol_weight > 0 )
189  threshold = p->mass_threshold(0, mol_weight, rho);
190  else if ( threshold )
191  p->mass_at_threshold(0, threshold, rho);
192 
193  // Mask generation or reading
194  Bimage* pmask = NULL;
195  if ( mask_file.length() ) {
196  pmask = read_img(mask_file, 1, -1);
197  } else if ( nregions > 1 ) {
198  pmask = p->kmeans_segment(nregions, max_iter, ratio);
199  } else if ( region_mask ) {
200  pmask = p->regions(threshold, sign);
201  } else if ( mol_weight > 0 ) {
202  pmask = p->mask_by_threshold(threshold);
203  } else if ( internal_volume ) {
204  pmask = p->internal_volume(threshold, mask_out_freq);
205  }
206 
207  // Mask level selection
208  if ( pmask ) {
209  if ( level_select.length() ) pmask->levelmask_select(level_select, select_flag);
210  if ( nvox ) pmask->levelmask_select(0, voxel);
211  }
212 
213  // Mask analysis
214 // if ( pmask )
215 // p->level_masked_stats(pmask);
216 
217  // Extract mask regions
218 
219  // Extract masked regions
220  if ( pmask && region_file.length() ) {
221  Bimage* pex = p->level_mask_extract(pmask);
222  pex->change_type(nudatatype);
223  if ( nustd > 0 ) pex->rescale_to_avg_std(nuavg, nustd);
224  write_img(region_file, pex, 0);
225  delete pex;
226  }
227 
228  // Write an output mask file if a file name is given
229  if ( !pmask ) pmask = p;
230  if ( optind < argc ) {
231  filename = argv[optind];
232  write_img(filename, pmask, 0);
233  }
234 
235  delete p;
236  if ( pmask && pmask != p ) delete pmask;
237 
238  if ( verbose & VERB_TIME )
239  timer_report(ti);
240 
241  bexit(0);
242 }
243 
Bimage::regions
Bimage * regions(double threshold, int sign)
Segments an image into contiguous regions.
Definition: Bimage_segment.cpp:56
Boption
A class for a command line input option.
Definition: options.h:26
RHO
#define RHO
Definition: utilities.h:95
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Bimage::level_mask_extract
Bimage * level_mask_extract(Bimage *pmask)
Extracts all the regions associated with a multi-level mask.
Definition: Bimage_mask.cpp:1998
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< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::mass_at_threshold
double mass_at_threshold(long img_num, double threshold, double rho)
Calculates the mass from the density threshold.
Definition: Bimage_segment.cpp:904
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
main
int main(int argc, char *argv[])
Definition: bsegment.cpp:63
Bimage::kmeans_segment
Bimage * kmeans_segment(long nregion=2, long max_iter=10, double ratio=1)
Segments an image based on K-means.
Definition: Bimage_segment.cpp:1125
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::internal_volume
Bimage * internal_volume(double threshold)
Calculates the internal volume of a shell.
Definition: Bimage_segment.cpp:934
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::mass_threshold
double mass_threshold(long img_num, double mol_weight, double rho)
Finds the density threshold associated with a particular molecular weight.
Definition: Bimage_segment.cpp:820
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::mask_by_threshold
Bimage * mask_by_threshold(double threshold)
Generates a mask based on an image at a given threshold.
Definition: Bimage_mask.cpp:93
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
use
const char * use[]
Definition: bsegment.cpp:19
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
Boption::mass
double mass()
Definition: options.h:269
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
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::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
Bimage::blobs
long blobs(double threshold, double min_size, double max_size, double setvalue, int sign)
Identifies contiguous regions above a given threshold (blobs) and eliminate those less than a minimum...
Definition: Bimage_segment.cpp:537
verbose
int verbose
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
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