| Bsoft 2.0.5 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 "-series 1.3,4.7,0.05 Segment through a series of thresholds: start,finish,increment.",
33 "-peaks 12,0.65,1,1 Find peaks within a kernel of a given size, above a threshold,",
34 " with optional flooding to the threshold and wrapping.",
35 "-threshold 3.4,0.6,-0.03 Segment by flooding from a high to a low threshold,",
36 " with a given increment (default increment = (high - low)/100).",
37 "-addmodel Add input model components as additional regions (use with -threshold).",
38 "-list List regions or regions (use with -threshold).",
39 "-color Generate a color image based on region size.",
40 " ",
41 "Parameters:",
42 "-verbose 7 Verbose output.",
43 "-datatype u Force writing of a new data type.",
44 "-fill Fill in borders between regions or regions.",
45 "-multilevel Flag for multi-level output for extracted segments.",
46 " ",
47 "Input:",
48 "-Map map.pif Replacement for the map specified in the model file.",
49 "-Mask mask.tif Mask for flooding (use with -threshold, must be integer and same size as input image).",
50 "-Model mod.star Seed model for flooding (use with -threshold).",
51 " ",
52 "Output:",
53 //"-output regions.star Model of segments.",
54 "-Extract mask.pif Extract regions marked by the model (see flag for multi-level).",
55 "-Region region.pif Base name to generate numbered output file names.",
56 "-Postscript his.ps Output plot file of level size histogram.",
57 " ",
58 NULL
59 };
60
61 int main(int argc, char* argv[])
62 {
63 // Initialize variables
64 DataType nudatatype(Unknown_Type); // Conversion to new type
65 int setinvert(0); // Flag to invert density
66 int seg_type(0); // 0=none, 1=series, 2=flooding(peaks), 3=flooding(threshold)
67 double threshold_hi(0); // Segmentation thresholds
68 double threshold_lo(0);
69 double threshold_inc(0);
70 long kernel_size(3); // Kernel for peak definition
71 int flood(0); // Flag to flood from peaks
72 int wrap(0); // Flag to turn wrapping on for finding peaks
73 int addmodel(0);
74 int list(0);
75 int color(0);
76 int fill_borders(0); // Borders between regions left
77 Bstring seed_file; // Model file
78 Bstring paramfile; // Input parameter file name
79 Bstring map_file; // Map file
80 Bstring mask_file; // Mask file
81 Bstring seg_file; // Model of segments
82 Bstring ext_file; // Output for extracted regions
83 int multilevel(0); // Flag for multi-level extraction
84 Bstring region_file; // region file base name
85 Bstring ps_file; // Poscript level size histogram plot
86
87 int i, 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 == "datatype" )
92 nudatatype = curropt->datatype();
93 if ( curropt->tag == "invert" )
94 setinvert = 1;
95 if ( curropt->tag == "series" ) {
96 if ( ( i = curropt->values(threshold_hi, threshold_lo, threshold_inc ) ) < 1 )
97 cerr << "-series: At least one threshold must be specified!" << endl;
98 else {
99 seg_type = 1;
100 if ( i < 2 ) threshold_lo = threshold_hi;
101 if ( i < 3 ) threshold_inc = threshold_lo - threshold_hi;
102 }
103 }
104 if ( curropt->tag == "peaks" ) {
105 if ( curropt->values(kernel_size, threshold_lo, flood, wrap ) < 1 )
106 cerr << "-peaks: A kernel size must be specified!" << endl;
107 else
108 seg_type = 2;
109 }
110 if ( curropt->tag == "threshold" ) {
111 if ( curropt->values(threshold_hi, threshold_lo, threshold_inc ) < 2 )
112 cerr << "-threshold: The two thresholds must be specified!" << endl;
113 else
114 seg_type = 3;
115 }
116 if ( curropt->tag == "list" ) list = 1;
117 if ( curropt->tag == "color" ) color = 1;
118 if ( curropt->tag == "addmodel" ) addmodel = 1;
119 if ( curropt->tag == "fill" ) fill_borders = 1;
120 if ( curropt->tag == "multilevel" ) multilevel = 1;
121 if ( curropt->tag == "Model" )
122 seed_file = curropt->filename();
123 if ( curropt->tag == "Map" )
124 map_file = curropt->filename();
125 if ( curropt->tag == "Mask" )
126 mask_file = curropt->filename();
127 if ( curropt->tag == "output" )
128 seg_file = curropt->filename();
129 if ( curropt->tag == "Extract" )
130 ext_file = curropt->filename();
131 if ( curropt->tag == "Region" )
132 region_file = curropt->filename();
133 if ( curropt->tag == "Postscript" )
134 ps_file = curropt->filename();
135 }
136 option_kill(option);
137
138 double ti = timer_start();
139
140 // Read the input file
141 Bstring filename(argv[optind++]);
142 Bproject* project = NULL;
143 Bmodel* model = NULL;
144 Bmodel* seed = NULL;
145
146 if ( file_type(filename) == Micrograph ) {
147 project = read_project(filename);
148 if ( project->rec ) filename = project->rec->frec;
149 } else if ( file_type(filename) == Model ) {
150 model = read_model(filename, paramfile);
151 filename = model->fmap;
152 }
153
154 if ( map_file.length() ) filename = map_file;
155 if ( model ) model->fmap = filename;
156
157 if ( filename.length() < 1 ) {
158 cerr << "Error: No image file name specified!" << endl;
159 bexit(-1);
160 }
161
162 if ( !model && ext_file.length() ) {
163 cerr << "Error: A model file is required to extract segments!" << endl;
164 bexit(-1);
165 }
166
167 Bimage* p = read_img(filename, 1, -1);
168 if ( !p ) {
169 cerr << "Error: No image file " << filename << " read!" << endl;
170 bexit(-1);
171 }
172
173 if ( nudatatype == Unknown_Type ) {
174 if ( color )
175 nudatatype = UCharacter;
176 else
177 nudatatype = p->data_type();
178 }
179
180 if ( setinvert ) p->invert();
181
182 if ( seed_file.length() ) seed = read_model(seed_file, paramfile);
183
184 Bimage* pmask = p;
185 Bimage* pseed = NULL;
186 Bimage* pex = NULL;
187 Bplot* plot = NULL;
188 Vector3<double> origin(p->image->origin());
189 Vector3<long> size = {p->sizeX(), p->sizeY(), p->sizeZ()};
190 Vector3<double> sam(p->sampling(0));
191
192 if ( seg_type == 1 ) {
193 p->region_threshold_series(threshold_hi, threshold_lo, threshold_inc);
194 } else if ( seg_type > 1 ) {
195 if ( seg_type == 2 ) {
196 pmask = p->region_peaks(kernel_size, threshold_lo, flood, wrap);
197 } else {
198 if ( mask_file.length() ) {
199 pmask = read_img(mask_file, 1, -1);
200 } else if ( seed ) {
201 pseed = img_from_model(seed, origin, size, sam, 1);
202 pmask = pseed->regions(0.5, 1);
203 delete pseed;
204 } else {
205 pmask = p->regions(threshold_hi, 0);
206 }
207 if ( model && addmodel ) img_add_model_to_mask(pmask, model);
208 p->region_flood(pmask, threshold_hi, threshold_lo, threshold_inc, fill_borders);
209 }
210 }
211
212 if ( list ) pmask->mask_stats();
213 if ( model && ext_file.length() ) {
214 pex = img_extract_segments_using_model(pmask, model, multilevel);
215 pex->label(get_cmd_line(argc, argv));
216 write_img(ext_file, pex, 0);
217 delete pex;
218 }
219 if ( region_file.length() ) {
220 pex = p->level_mask_extract(pmask);
221 pex->label(get_cmd_line(argc, argv));
222 write_img(region_file, pex, 0);
223 delete pex;
224 }
225 if ( ps_file.length() ) {
226 plot = pmask->levelmask_size_histogram();
227 ps_plot(ps_file, plot);
228 delete plot;
229 }
230 if ( color ) {
231 if ( p != pmask ) delete p;
232 p = pmask->levelmask_color_by_size();
233 delete pmask;
234 pmask = p;
235 }
236
237 // Write an output file if a file name is given
238 if ( optind < argc ) {
239 pmask->change_type(nudatatype);
240 pmask->label(get_cmd_line(argc, argv));
241 write_img(argv[optind], pmask, 0);
242 }
243
244 if ( p != pmask ) delete p;
245 delete pmask;
246 project_kill(project);
247 model_kill(model);
248
249 if ( verbose & VERB_TIME )
250 timer_report(ti);
251
252 bexit(0);
253 }
254
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
rwmg.h
Header file for reading and writing micrograph parameters.
model_map.h
Header file for model to map conversions.
Model
Definition: file_util.h:25
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
Bimage::mask_stats
long mask_stats()
Calculates statistics for a mask.
Definition: Bimage_mask.cpp:165
Bimage::regions
Bimage * regions(double threshold, int sign)
Segments an image into contiguous regions.
Definition: Bimage_segment.cpp:58
utilities.h
Header file for general utilities.
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:334
Boption::tag
Bstring tag
Definition: options.h:29
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
verbose
int verbose
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
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:202
Bimage
General image parameter class.
Definition: Bimage.h:163
Micrograph
Definition: file_util.h:23
main
int main(int argc, char *argv[])
Definition: bflood.cpp:61
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1124
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:257
Bimage::level_mask_extract
Bimage * level_mask_extract(Bimage *pmask)
Extracts all the regions associated with a multi-level mask.
Definition: Bimage_mask.cpp:1785
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
file_util.h
Header file for file checking functions.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
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:1924
model
Bmodel * model
Definition: bshow.cpp:32
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:28
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
rwmodel.h
Header file for reading and writing atomic model files.
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
model_mask.h
Functions to generate masks from models.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bmodel
Definition: rwmodel.h:119
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
use
const char * use[]
Definition: bflood.cpp:23
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::levelmask_size_histogram
Bplot * levelmask_size_histogram()
Color a multi-level mask based on the volumes of regions.
Definition: Bimage_mask.cpp:2018
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:470
img_add_model_to_mask
int img_add_model_to_mask(Bimage *p, Bmodel *model)
Adds to the mask the component locations.
Definition: model_mask.cpp:405
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
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
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bplot
Definition: Bplot.h:146