Bsoft  2.1.0
Bernard's software package
bflood.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmg.h"
11 #include "rwmodel.h"
12 #include "model_map.h"
13 #include "model_mask.h"
14 #include "file_util.h"
15 #include "options.h"
16 #include "utilities.h"
17 #include "timer.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 /* Usage assistance */
23 const char* use[] = {
24 " ",
25 "Usage: bflood [options] input.img/input.star [output.img]",
26 "---------------------------------------------------------",
27 "Segments images using the watershed or flooding algorithm.",
28 "Note: -series, -peaks and -threshold are mutually exclusive.",
29 " ",
30 "Actions:",
31 "-invert Invert density in the image before other operations.",
32 "-list List regions or regions (use with -threshold).",
33 "-color Generate a color image based on region size.",
34 "-series 1.3,4.7,0.05 Segment through a series of thresholds: start,finish,increment.",
35 " ",
36 "Actions for seeding:",
37 "-peaks 12,1.4,1,1 Find peaks within a kernel size, above a threshold,",
38 " with optional flooding and wrapping.",
39 "-threshold 3.4 Segment at a threshold,",
40 //"-addmodel Add input model components as additional regions.",
41 " ",
42 "Actions for flooding:",
43 "-flood 0.6,-0.03 Segment by flooding to a low threshold,",
44 " with increment (default increment = range/100).",
45 " ",
46 "Parameters:",
47 "-verbose 7 Verbose output.",
48 "-datatype u Force writing of a new data type.",
49 "-fill Fill in borders between regions or regions.",
50 "-multilevel Flag for multi-level output for extracted segments.",
51 " ",
52 "Input:",
53 "-Map map.pif Replacement for the map specified in the model file.",
54 "-Mask mask.tif Mask for flooding (use with -threshold, must be integer and same size as input image).",
55 "-Model mod.star Seed model for flooding (use with -threshold).",
56 " ",
57 "Output:",
58 //"-output regions.star Model of segments.",
59 "-Extract mask.pif Extract regions marked by the model (see flag for multi-level).",
60 "-Region region.pif Base name to generate numbered output file names.",
61 "-Postscript his.ps Output plot file of level size histogram.",
62 " ",
63 NULL
64 };
65 
66 int main(int argc, char* argv[])
67 {
68  // Initialize variables
69  DataType nudatatype(Unknown_Type); // Conversion to new type
70  int setinvert(0); // Flag to invert density
71  int seg_type(0); // 0=none, 1=series, 2=flooding(peaks), 3=flooding(threshold)
72  double threshold_hi(0); // Segmentation thresholds
73  double threshold_lo(0);
74  double threshold_inc(0);
75  long kernel_size(3); // Kernel for peak definition
76  int flood(0); // Flag to flood from peaks
77  int wrap(0); // Flag to turn wrapping on for finding peaks
78 // int addmodel(0);
79  int list(0);
80  int color(0);
81  int fill_borders(0); // Borders between regions left
82  Bstring seed_file; // Model file
83  Bstring paramfile; // Input parameter file name
84  Bstring map_file; // Map file
85  Bstring mask_file; // Mask file
86  Bstring seg_file; // Model of segments
87  Bstring ext_file; // Output for extracted regions
88  int multilevel(0); // Flag for multi-level extraction
89  Bstring region_file; // region file base name
90  Bstring ps_file; // Poscript level size histogram plot
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 == "series" ) {
101  if ( ( i = curropt->values(threshold_hi, threshold_lo, threshold_inc ) ) < 1 )
102  cerr << "-series: At least one threshold must be specified!" << endl;
103  else {
104  seg_type = 1;
105  if ( i < 2 ) threshold_lo = threshold_hi;
106  if ( i < 3 ) threshold_inc = threshold_lo - threshold_hi;
107  }
108  }
109  if ( curropt->tag == "peaks" ) {
110  if ( curropt->values(kernel_size, threshold_hi, flood, wrap ) < 1 )
111  cerr << "-peaks: A kernel size must be specified!" << endl;
112  else {
113  seg_type = 2;
114  if ( flood ) flood = 1;
115  }
116  }
117  if ( curropt->tag == "threshold" ) {
118  threshold_hi = curropt->value.real();
119  seg_type = 3;
120  }
121  if ( curropt->tag == "flood" ) {
122  if ( curropt->values(threshold_lo, threshold_inc ) < 1 )
123  cerr << "-threshold: A lower threshold must be specified!" << endl;
124  else
125  flood = 2;
126  }
127  if ( curropt->tag == "list" ) list = 1;
128  if ( curropt->tag == "color" ) color = 1;
129 // if ( curropt->tag == "addmodel" ) addmodel = 1;
130  if ( curropt->tag == "fill" ) fill_borders = 1;
131  if ( curropt->tag == "multilevel" ) multilevel = 1;
132  if ( curropt->tag == "Model" )
133  seed_file = curropt->filename();
134  if ( curropt->tag == "Map" )
135  map_file = curropt->filename();
136  if ( curropt->tag == "Mask" )
137  mask_file = curropt->filename();
138  if ( curropt->tag == "output" )
139  seg_file = curropt->filename();
140  if ( curropt->tag == "Extract" )
141  ext_file = curropt->filename();
142  if ( curropt->tag == "Region" )
143  region_file = curropt->filename();
144  if ( curropt->tag == "Postscript" )
145  ps_file = curropt->filename();
146  }
147  option_kill(option);
148 
149  double ti = timer_start();
150 
151  // Read the input file
152  Bstring filename(argv[optind++]);
153  Bproject* project = NULL;
154  Bmodel* model = NULL;
155  Bmodel* seed = NULL;
156 
157  if ( file_type(filename) == Micrograph ) {
158  project = read_project(filename);
159  if ( project->rec ) filename = project->rec->frec;
160  } else if ( file_type(filename) == Model ) {
161  model = read_model(filename, paramfile);
162  filename = model->fmap;
163  }
164 
165  if ( map_file.length() ) filename = map_file;
166  if ( model ) model->fmap = filename;
167 
168  if ( filename.length() < 1 ) {
169  cerr << "Error: No image file name specified!" << endl;
170  bexit(-1);
171  }
172 
173  if ( !model && ext_file.length() ) {
174  cerr << "Error: A model file is required to extract segments!" << endl;
175  bexit(-1);
176  }
177 
178  Bimage* p = read_img(filename, 1, -1);
179  if ( !p ) {
180  cerr << "Error: No image file " << filename << " read!" << endl;
181  bexit(-1);
182  }
183 
184  if ( nudatatype == Unknown_Type ) {
185  if ( color )
186  nudatatype = UCharacter;
187  else
188  nudatatype = p->data_type();
189  }
190 
191  if ( setinvert ) p->invert();
192 
193  Bimage* pmask = p;
194  Bimage* pseed = NULL;
195  Bimage* pex = NULL;
196  Bplot* plot = NULL;
197  Vector3<double> origin(p->image->origin());
198  Vector3<long> size = {p->sizeX(), p->sizeY(), p->sizeZ()};
199  Vector3<double> sam(p->sampling(0));
200 
201  // Mask generation or reading
202  if ( mask_file.length() ) {
203  pmask = read_img(mask_file, 1, -1);
204  } else if ( seed_file.length() ) {
205  seed = read_model(seed_file, paramfile);
206  pseed = img_from_model(seed, origin, size, sam, 1);
207  pmask = pseed->regions(0.5, 1);
208  delete pseed;
209  } else if ( seg_type == 3 ) {
210  pmask = p->regions(threshold_hi, 0);
211  } else if ( seg_type == 2 ) {
212  pmask = p->region_peaks(kernel_size, threshold_hi, flood, wrap);
213  }
214 
215 // if ( pmask && model && addmodel )
216 // img_add_model_to_mask(pmask, model);
217 
218  if ( seg_type == 1 ) {
219  p->region_threshold_series(threshold_hi, threshold_lo, threshold_inc);
220  } else if ( flood == 2 ) {
221  p->region_flood(pmask, threshold_hi, threshold_lo, threshold_inc, fill_borders);
222  }
223 
224  if ( list ) p->level_masked_stats(pmask);
225 
226  // Extract masked regions
227  if ( pmask && model && ext_file.length() ) {
228  pex = img_extract_segments_using_model(pmask, model, multilevel);
229  write_img(ext_file, pex, 0);
230  delete pex;
231  }
232 
233  if ( pmask && region_file.length() ) {
234  pex = p->level_mask_extract(pmask);
235  pex->change_type(nudatatype);
236  write_img(region_file, pex, 0);
237  delete pex;
238  }
239 
240  if ( ps_file.length() ) {
241  plot = pmask->levelmask_size_histogram();
242  ps_plot(ps_file, plot);
243  delete plot;
244  }
245  if ( color ) {
246  if ( p != pmask ) delete p;
247  p = pmask->levelmask_color_by_size();
248  delete pmask;
249  pmask = p;
250  }
251 
252  // Write an output file if a file name is given
253  if ( optind < argc ) {
254  pmask->change_type(nudatatype);
255  write_img(argv[optind], pmask, 0);
256  }
257 
258  if ( p != pmask ) delete p;
259  delete pmask;
261  model_kill(model);
262 
263  if ( verbose & VERB_TIME )
264  timer_report(ti);
265 
266  bexit(0);
267 }
268 
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
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Bimage::level_masked_stats
int level_masked_stats(Bimage *pmask)
Caclulates statistics for all the regions defined by a multi-level mask.
Definition: Bimage_mask.cpp:1800
Bimage::region_threshold_series
int region_threshold_series(double threshold_first, double threshold_last, double threshold_step)
Segments a map through a series of thresholds and reports results.
Definition: Bimage_segment.cpp:200
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
img_from_model
Bimage * img_from_model(Bmodel *model, Vector3< double > ori, Vector3< long > size, Vector3< double > sam, double sigma)
Calculates a map from model component coordinates.
Definition: model_map.cpp:59
verbose
int verbose
UCharacter
@ UCharacter
Definition: utilities.h:153
rwmodel.h
Header file for reading and writing atomic model files.
file_util.h
Header file for file checking functions.
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
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
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
model_map.h
Header file for model to map conversions.
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
Bplot
Definition: Bplot.h:146
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::region_flood
long region_flood(Bimage *pmask, double threshold_hi, double threshold_lo, double threshold_step, int fill_borders)
Creates and expands a region map from a starting to ending threshold value.
Definition: Bimage_segment.cpp:256
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
project
Bproject * project
Definition: bshow.cpp:33
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
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
img_extract_segments_using_model
Bimage * img_extract_segments_using_model(Bimage *p, Bmodel *model, int multi_level)
Extracts segmentss from a multi-level mask around points defined by a model.
Definition: model_mask.cpp:335
Micrograph
@ Micrograph
Definition: file_util.h:23
Boption::value
Bstring value
Definition: options.h:30
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.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
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
Bimage::region_peaks
Bimage * region_peaks(long kernel_size, double threshold, int flood=0, int wrap=0)
Generates a segmented image from peaks above a threshold value.
Definition: Bimage_segment.cpp:457
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
model_mask.h
Functions to generate masks from models.
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::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
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
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
main
int main(int argc, char *argv[])
Definition: bflood.cpp:66
Model
@ Model
Definition: file_util.h:25
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
use
const char * use[]
Definition: bflood.cpp:23