Bsoft  2.1.0
Bernard's software package
bmultimask.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmodel.h"
11 #include "model_mask.h"
12 #include "model_links.h"
13 #include "model_util.h"
14 #include "options.h"
15 #include "utilities.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 
22 // Usage assistance
23 const char* use[] = {
24 " ",
25 "Usage: bmultimask [options] input.img output.img",
26 "------------------------------------------------",
27 "Manipulates composite/level masks.",
28 "The input image is considered to have multiple integer levels (even for floating point).",
29 " ",
30 "Actions:",
31 "-invert Invert mask before other operations.",
32 "-dilate 2 Dilate mask a number of times.",
33 "-switch 3,6 Switch two levels.",
34 "-color size Generate a RGBA image (random/level/size).",
35 "-size Convert level mask to region size map.",
36 "-level 134 Convert a mask to multiple levels, with the given voxels per level.",
37 "-bands 250,-1,150,1,30,0 Banded mask for reciprocal space (pairs of numbers: angstrom,value).",
38 " Band with 0: Not used.",
39 " Band with 1: Use for orientation determination.",
40 " Band with -1: Use for cross-validation.",
41 "-collapse Convert a multi-level mask to a binary mask.",
42 "-interfaces 3 Calculates interfaces for the indicated region (-1 gives whole matrix).",
43 "-merge 25,4 Calculates a region interface matrix and deletes/merges",
44 " regions smaller than the given size and interface size.",
45 "-symmetry C5 Set all symmetry-related segments to the same index.",
46 " ",
47 "Selections:",
48 "-select 1,5-7 Select levels in a multi-level mask.",
49 "-combine 1,5-7 Combine levels to create a binary mask.",
50 "-voxel 23,241,193 Select the level including this voxel.",
51 " ",
52 "Parameters:",
53 "-verbose 1 Verbosity of output.",
54 "-datatype u Force writing of a new data type.",
55 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
56 "-origin 110,50,44 Origin for the mask (default from input image).",
57 " ",
58 "Parameters for model generation:",
59 "-componentradius 8.4 Set display radius for all components.",
60 "-linkradius 5.1 Set display radius for all links.",
61 " ",
62 "Input:",
63 "-Mask file.img Template mask to select levels.",
64 " ",
65 "Output:",
66 "-Model model.star Output model file.",
67 "-Postscript his.ps Output plot file of level size histogram.",
68 " ",
69 NULL
70 };
71 
72 int main(int argc, char **argv)
73 {
74  // Initialize variables
75  int invert(0); // Inversion flag
76  int dilate(0); // Flag to dilate the mask
77  long index1(-1000), index2(-1000); // Indices to switch
78  Bstring color; // RGBA: random/level/size-based colors
79  int convert_size(0); // Flag to cenvert level mask to region size
80  Bstring level_select; // Selection of levels
81  int select_flag(1); // 0=combine, 1=multi-level
82  Vector3<long> voxel(-1,0,0); // Voxel to select a level
83  int level(0); // Number of voxels per level
84  vector<double> band; // Reciprocal space bands
85  int collapse(0); // Collapse a multi-level mask to a binary mask
86  int reg_if(-1000000); // Calculate region interfaces
87  int min_size(0); // Minimum size and flag to calculate interface matrix
88  int min_if(1); // Minimum interface size to accept
89  Bsymmetry* sym = NULL; // Point group
90  DataType nudatatype(Unknown_Type); // Conversion to new type
91  Vector3<double> sampling; // Units for the three axes (A/pixel)
92  Vector3<double> origin; // Mask origin
93  int set_origin(0); // Flag to set origin
94  double comprad(0); // Component display radius
95  double linkrad(0); // Link display radius
96  Bstring mask_file; // Secondary mask file
97  Bstring mod_file; // Output model file
98  Bstring ps_file; // Poscript level size histogram plot
99 
100  int optind;
101  Boption* option = get_option_list(use, argc, argv, optind);
102  Boption* curropt;
103  for ( curropt = option; curropt; curropt = curropt->next ) {
104  if ( curropt->tag == "invert" )
105  invert = 1;
106  if ( curropt->tag == "dilate" )
107  if ( ( dilate = curropt->value.integer() ) < 1 )
108  cerr << "-dilate: A number of times to dilate must be specified!" << endl;
109  if ( curropt->tag == "switch" )
110  if ( curropt->values(index1, index2) < 2 )
111  cerr << "-switch: Both indices must be specified!" << endl;
112  if ( curropt->tag == "color" )
113  color = curropt->value;
114  if ( curropt->tag == "size" )
115  convert_size = 1;
116  if ( curropt->tag == "select" ) {
117  level_select = curropt->value;
118  if ( level_select.length() < 1 )
119  cerr << "-select: Level numbers must be specified!" << endl;
120  else
121  select_flag = 1;
122  }
123  if ( curropt->tag == "combine" ) {
124  level_select = curropt->value;
125  if ( level_select.length() < 1 )
126  cerr << "-combine: Level numbers must be specified!" << endl;
127  else
128  select_flag = 0;
129  }
130  if ( curropt->tag == "voxel" ) {
131  voxel = curropt->size();
132  if ( voxel.volume() < 0 )
133  cerr << "-voxel: A voxel must be specified!" << endl;
134  }
135  if ( curropt->tag == "level" )
136  if ( ( level = curropt->value.integer() ) < 1 )
137  cerr << "-level: The voxels per level must be specified!" << endl;
138  if ( curropt->tag == "bands" ) {
139  band = curropt->value.split_into_doubles(",");
140  if ( band.size() < 2 )
141  cerr << "-bands: At least two pairs of numbers must be specified!" << endl;
142  band[band.size()] = 0;
143  }
144  if ( curropt->tag == "collapse" )
145  collapse = 1;
146  if ( curropt->tag == "interfaces" )
147  if ( ( reg_if = curropt->value.integer() ) < -1000000 )
148  cerr << "-interfaces: A region number must be specified!" << endl;
149  if ( curropt->tag == "merge" )
150  if ( curropt->values(min_size, min_if) < 1 )
151  cerr << "-merge: A minimum size must be specified!" << endl;
152  if ( curropt->tag == "symmetry" )
153  sym = curropt->symmetry();
154  if ( curropt->tag == "datatype" )
155  nudatatype = curropt->datatype();
156  if ( curropt->tag == "sampling" )
157  sampling = curropt->scale();
158  if ( curropt->tag == "origin" ) {
159  if ( curropt->value[0] == 'c' ) {
160  set_origin = 2;
161  } else {
162  origin = curropt->origin();
163  set_origin = 1;
164  }
165  }
166  if ( curropt->tag == "Mask" )
167  mask_file = curropt->filename();
168  if ( curropt->tag == "Model" )
169  mod_file = curropt->filename();
170  if ( curropt->tag == "Postscript" )
171  ps_file = curropt->filename();
172  }
173  option_kill(option);
174 
175  double ti = timer_start();
176 
177  Bimage* p = read_img(argv[optind++], 1, -1);
178 
179  if ( p == NULL ) bexit(-1);
180 
181  if ( set_origin ) {
182  if ( set_origin == 2 ) p->origin(p->size()/2);
183  else p->origin(origin);
184  }
185 
186  if ( sampling.volume() > 0 ) p->sampling(sampling);
187 
188  if ( invert ) p->invert();
189 
190  if ( level_select.length() )
191  p->levelmask_select(level_select, select_flag);
192 
193  if ( voxel[0] >= 0 ) p->levelmask_select(0, voxel);
194 
195  if ( index1 > 0 ) p->levelmask_switch(index1, index2);
196 
197  if ( band.size() && band[0] )
198  p->mask_fspace_banded(band);
199 
200  if ( level ) p->mask_split(level);
201 
202  if ( reg_if > -1000000 ) p->mask_region_interfaces(reg_if);
203 
204  if ( min_size > 0 ) p->mask_merge_delete(min_size, min_if);
205 
206  if ( dilate ) p->levelmask_dilate(dilate);
207 
208  if ( sym ) p->levelmask_symmetrize(sym);
209 
210  Bimage* pmask = NULL;
211  if ( mask_file.length() ) {
212  pmask = read_img(mask_file, 1, -1);
213  if ( sampling.volume() > 0 ) pmask->sampling(sampling);
214  p->levelmask_select(pmask);
215  delete pmask;
216  }
217 
218  if ( collapse ) p->levelmask_collapse();
219 
220  p->levelmask_clean();
221 
222  if ( color.length() ) {
223  if ( color[0] == 'r' ) p->levelmask_colorize();
224  else if ( color[0] == 'l' ) p->simple_to_rgba();
225  else if ( color[0] == 's' ) {
226  pmask = p->levelmask_color_by_size();
227  delete p;
228  p = pmask;
229  }
230  } else if ( convert_size ) {
232  }
233 
234  Bplot* plot = NULL;
235  if ( ps_file.length() ) {
236  plot = p->levelmask_size_histogram();
237  ps_plot(ps_file, plot);
238  delete plot;
239  }
240 
241  p->mask_stats();
242 
243  if ( mod_file.length() ) {
245  if ( comprad ) models_process(model, comprad, model_set_component_radius);
246  if ( linkrad ) models_process(model, linkrad, model_set_link_radius);
247  write_model(mod_file, model);
248  model_kill(model);
249  }
250 
251  if ( optind < argc ) {
252  p->change_type(nudatatype);
253  write_img(argv[optind], p, 0);
254  }
255 
256  delete p;
257 
258  if ( verbose & VERB_TIME )
259  timer_report(ti);
260 
261  bexit(0);
262 }
263 
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: bmultimask.cpp:23
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
Boption::scale
Vector3< double > scale()
Definition: options.h:190
rwmodel.h
Header file for reading and writing atomic model files.
Bstring
Definition: Bstring.h:29
Bimage::levelmask_color_by_size
Bimage * levelmask_color_by_size()
Color a multi-level mask based on the volumes of regions.
Definition: Bimage_mask.cpp:2131
Bstring::integer
long integer()
Definition: Bstring.cpp:547
model_from_multilevel_mask
Bmodel * model_from_multilevel_mask(Bimage *p)
Generates components at the center of mass of each mask level.
Definition: model_mask.cpp:453
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
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
Bplot
Definition: Bplot.h:146
Bimage::levelmask_symmetrize
long levelmask_symmetrize(Bsymmetry *sym)
Symetrizes a multi-level mask.
Definition: Bimage_mask.cpp:1852
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bsymmetry
Definition: symmetry.h:38
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
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
Bimage::levelmask_clean
long levelmask_clean()
Removes empty levels from a multi-level mask.
Definition: Bimage_mask.cpp:2061
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::mask_stats
long mask_stats()
Calculates statistics for a mask.
Definition: Bimage_mask.cpp:165
Boption::value
Bstring value
Definition: options.h:30
Bimage::levelmask_region_size
int levelmask_region_size()
Convert a mask to reflect region sizes.
Definition: Bimage_mask.cpp:2184
Bimage::levelmask_dilate
long levelmask_dilate(int times)
Dilates a level mask.
Definition: Bimage_mask.cpp:1562
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::mask_region_interfaces
long mask_region_interfaces(int reg_num)
Reports the whole interface matrix or interfaces for one region only.
Definition: Bimage_mask.cpp:2365
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::levelmask_size_histogram
Bplot * levelmask_size_histogram()
Color a multi-level mask based on the volumes of regions.
Definition: Bimage_mask.cpp:2215
Bmodel
Definition: rwmodel.h:119
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
Boption::size
Vector3< long > size()
Definition: options.h:163
model_mask.h
Functions to generate masks from models.
Bimage::levelmask_collapse
long levelmask_collapse()
Collapse a multi-level mask to a binary mask.
Definition: Bimage_mask.cpp:1500
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
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
main
int main(int argc, char **argv)
Definition: bmultimask.cpp:72
Bimage::mask_split
long mask_split(long voxels_per_level)
Converts a mask to a multilevel mask with a given number of voxels per level.
Definition: Bimage_mask.cpp:1450
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
model_util.h
Library routines used for model processing.
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::levelmask_switch
long levelmask_switch(long index1, long index2)
Switches two segments in a multi-level mask.
Definition: Bimage_mask.cpp:1774
verbose
int verbose
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::levelmask_colorize
long levelmask_colorize()
Colorizes a multi-level mask with random color assignments.
Definition: Bimage_mask.cpp:2095
model_set_component_radius
long model_set_component_radius(Bmodel *model, double comprad)
Set the display radius for all components to a specific value.
Definition: rwmodel.cpp:1247
Bimage::simple_to_rgba
void simple_to_rgba()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:58
level
size_t level(0)
Bimage::mask_fspace_banded
int mask_fspace_banded(vector< double > &band)
Generate reciprocal space mask based on a specification of bands.
Definition: Bimage_mask.cpp:808
Bimage::mask_merge_delete
long mask_merge_delete(long min_size, long min_if)
Calculates the interfaces between regions and deletes/merges small ones.
Definition: Bimage_mask.cpp:2416