Bsoft  2.0.5
Bernard's software package
blevel.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "img_combine.h"
11 #include "symmetry.h"
12 #include "options.h"
13 #include "utilities.h"
14 #include "timer.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: blevel [options] input.img output.img",
24 "--------------------------------------------",
25 "Manipulates level masks.",
26 "The input image is considered to have multiple integer levels (even for floating point).",
27 " ",
28 "Actions for mask creation:",
29 "-invert Invert density before other operations.",
30 "-threshold 1.34 Generate a level mask from disjoint regions (inverted threshold with -invert).",
31 "-bands 250,-1,150,1,30,0 Banded mask for reciprocal space (pairs of numbers: angstrom,value).",
32 " Band with 0: Not used.",
33 " Band with 1: Use for orientation determination.",
34 " Band with -1: Use for cross-validation.",
35 " ",
36 "Actions for mask modification:",
37 "-level 134 Convert a mask to multiple levels, with the given voxels per level.",
38 "-symmetry C5 Set all symmetry-related segments to the same index.",
39 "-color size Generate a RGBA image (random/size).",
40 "-size Convert level mask to region size map.",
41 "-dilate 2 Dilate the level mask.",
42 "-add mask.pif,3 Add a bilevel mask to the indicated level (default 1).",
43 "-select 1,5-7 Select levels in a multi-level mask.",
44 "-voxel 23,241,193 Select the level including this voxel.",
45 "-collapse Convert a multi-level mask to a binary mask.",
46 " ",
47 "Parameters:",
48 "-verbose 1 Verbosity of output.",
49 "-datatype u Force writing of a new data type.",
50 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
51 "-origin 110,50,44 Origin for the mask (default from input image).",
52 //"-wrap Flag to wrap around image boundaries (default not).",
53 " ",
54 "Input:",
55 "-Mask file.img Template mask to select levels.",
56 " ",
57 "Output:",
58 "-Postscript his.ps Output plot file of level size histogram.",
59 " ",
60 NULL
61 };
62 
63 int main(int argc, char **argv)
64 {
65  // Initialize variables
66  int invert(0); // Inversion flag
67  double threshold(-1e37); // Threshold to generate level mask
68  Bstring color; // Generate RGBA image: random or size-based colors
69  int convert_size(0); // Flag to cenvert level mask to region size
70  int dilate(0); // Flag to dilate the mask
71  int collapse(0); // Collapse a multi-level mask to a binary mask
72  Bstring add_mask; // Bilevel mask to add
73  int add_level(1); // Level to add bilevel mask
74  Bstring level_select; // Selection of levels
75  Vector3<long> voxel(-1,0,0); // Voxel to select a level
76  long level(0); // Number of voxels per level
77  vector<double> band; // Reciprocal space bands
78  Bsymmetry* sym = NULL; // Point group
79  DataType nudatatype(Unknown_Type); // Conversion to new type
80  Vector3<double> sam; // Units for the three axes (A/pixel)
81  Vector3<double> origin; // Mask origin
82  int set_origin(0); // Flag to set origin
83 // int wrap(0); // Flag to wrap
84  Bstring mask_file; // Mask file
85  Bstring ps_file; // Poscript level size histogram plot
86 
87  int optind;
88  Boption* option = get_option_list(use, argc, argv, optind);
89  Boption* curropt;
90  for ( curropt = option; curropt; curropt = curropt->next ) {
91  if ( curropt->tag == "invert" )
92  invert = 1;
93  if ( curropt->tag == "threshold" )
94  if ( ( threshold = curropt->value.real() ) < -1e30 )
95  cerr << "-threshold: A threshold value must be specified!" << endl;
96  if ( curropt->tag == "color" )
97  color = curropt->value;
98  if ( curropt->tag == "size" )
99  convert_size = 1;
100  if ( curropt->tag == "dilate" )
101  if ( ( dilate = curropt->value.integer() ) < 1 )
102  cerr << "-dilate: A number of times to dilate must be specified!" << endl;
103  if ( curropt->tag == "collapse" )
104  collapse = 1;
105  if ( curropt->tag == "add" ) {
106  add_mask = curropt->filename();
107  if ( curropt->value.contains(",") ) add_level = curropt->value.post(',').integer();
108  }
109  if ( curropt->tag == "select" ) {
110  level_select = curropt->value;
111  if ( level_select.length() < 1 )
112  cerr << "-select: Level numbers must be specified!" << endl;
113  }
114  if ( curropt->tag == "voxel" )
115  voxel = curropt->size();
116  if ( curropt->tag == "level" )
117  if ( ( level = curropt->value.integer() ) < 1 )
118  cerr << "-level: The voxels per level must be specified!" << endl;
119  if ( curropt->tag == "bands" ) {
120  band = curropt->value.split_into_doubles(",");
121  if ( band.size() < 2 )
122  cerr << "-bands: At least two pairs of numbers must be specified!" << endl;
123  band[band.size()] = 0;
124  }
125  if ( curropt->tag == "symmetry" )
126  sym = curropt->symmetry();
127  if ( curropt->tag == "datatype" )
128  nudatatype = curropt->datatype();
129  if ( curropt->tag == "sampling" )
130  sam = curropt->scale();
131  if ( curropt->tag == "origin" ) {
132  if ( curropt->value[0] == 'c' ) {
133  set_origin = 2;
134  } else {
135  origin = curropt->origin();
136  set_origin = 1;
137  }
138  }
139  if ( curropt->tag == "Mask" )
140  mask_file = curropt->filename();
141  if ( curropt->tag == "Postscript" )
142  ps_file = curropt->filename();
143  }
144  option_kill(option);
145 
146  double ti = timer_start();
147 
148  Bimage* p = read_img(argv[optind++], 1, -1);
149  if ( p == NULL ) bexit(-1);
150 
151  if ( set_origin ) {
152  if ( set_origin == 2 ) p->origin(p->default_origin());
153  else p->origin(origin);
154  }
155 
156  if ( sam.volume() > 0 ) p->sampling(sam);
157 
158  if ( invert ) p->invert();
159 
160  Bimage* pmask = NULL;
161 
162  if ( threshold > -1e30 ) {
163  pmask = p->regions(threshold, 1);
164  delete p;
165  p = pmask;
166  }
167 
168  if ( add_mask.length() ) {
169  pmask = read_img(add_mask, 1, -1);
170  p->levelmask_add(pmask, add_level);
171  delete pmask;
172  }
173 
174  if ( sym ) p->mask_symmetrize(sym);
175 
176  if ( dilate ) p->levelmask_dilate(dilate);
177 
178  if ( level_select.length() ) p->levelmask_select(level_select);
179 
180  if ( voxel[0] >= 0 ) p->levelmask_select(0, voxel);
181 
182  if ( band[0] )
183  p->mask_fspace_banded(band);
184 
185  if ( level ) p->mask_split(level);
186 
187  if ( mask_file.length() ) {
188  pmask = read_img(mask_file, 1, -1);
189  if ( sam.volume() > 0 ) pmask->sampling(sam);
190  p->levelmask_select(pmask);
191  delete pmask;
192  }
193 
194  if ( collapse ) p->levelmask_collapse();
195 
196  if ( color.length() ) {
197  if ( color[0] == 'r' ) pmask->simple_to_rgba();
198  else if ( color[0] == 's' ) p = pmask->levelmask_color_by_size();
199  } else if ( convert_size ) pmask->levelmask_region_size();
200 
201 
202  Bplot* plot = NULL;
203  if ( ps_file.length() ) {
204  plot = pmask->levelmask_size_histogram();
205  ps_plot(ps_file, plot);
206  delete plot;
207  }
208 
209  p->mask_stats();
210 
211  if ( optind < argc ) {
212  p->change_type(nudatatype);
213  p->label(get_cmd_line(argc, argv));
214  write_img(argv[optind], p, 0);
215  }
216 
217  delete p;
218  kill_symmetry(sym);
219 
220  if ( verbose & VERB_TIME )
221  timer_report(ti);
222 
223  bexit(0);
224 }
225 
226 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1735
double real()
Definition: Bstring.cpp:553
Bstring post(char c)
Definition: Bstring.cpp:402
DataType
Base data type specifier.
Definition: utilities.h:149
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3088
Header file for general symmetry functions.
long mask_stats()
Calculates statistics for a mask.
Bimage * regions(double threshold, int sign)
Segments an image into contiguous regions.
int levelmask_region_size()
Convert a mask to reflect region sizes.
Header file for general utilities.
Bstring value
Definition: options.h:30
Bstring tag
Definition: options.h:29
int verbose
long levelmask_add(Bimage *pmask, int add_level=1)
Adds a bilevel mask to a level mask.
long length() const
Definition: Bstring.cpp:263
int mask_fspace_banded(vector< double > &band)
Generate reciprocal space mask based on a specification of bands.
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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 * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Bstring filename()
Definition: options.h:85
Utilities for timing functions.
long levelmask_collapse()
Collapse a multi-level mask to a binary mask.
General image parameter class.
Definition: Bimage.h:176
Vector3< long > size()
Definition: options.h:163
int main(int argc, char **argv)
Definition: blevel.cpp:63
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
A class for a command line input option.
Definition: options.h:26
void label(string s)
Definition: Bimage.h:248
#define VERB_TIME
Definition: utilities.h:113
const char * use[]
Definition: blevel.cpp:21
Header file for combining two images in various ways.
long mask_symmetrize(Bsymmetry *sym)
Symetrizes a mask.
Vector3< double > scale()
Definition: options.h:190
Bsymmetry * symmetry()
Definition: options.h:240
long levelmask_select(Bstring &select_list)
Retains the selected levels in a multi-level mask.
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
size_t level(0)
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
long mask_split(long voxels_per_level)
Converts a mask to a multilevel mask with a given number of voxels per level.
Bimage * levelmask_color_by_size()
Color a multi-level mask based on the volumes of regions.
DataType datatype()
Definition: options.h:93
Boption * next
Definition: options.h:28
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Vector3< double > default_origin()
Definition: Bimage.h:581
Vector3< double > origin()
Definition: options.h:180
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
long levelmask_dilate(int times)
Dilates a level mask.
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
long integer()
Definition: Bstring.cpp:547
Header file for 2D and 3D image I/O.
Header file for option handlers.
Bplot * levelmask_size_histogram()
Color a multi-level mask based on the volumes of regions.
double volume()
Definition: Vector3.h:237
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
void origin(double ox, double oy, double oz)
Definition: Bimage.h:559
Definition: Bplot.h:146
void simple_to_rgba()
A simple image is converted to a color image.