| Bsoft 2.0.5 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 data to average and standard deviation after changes.",
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 "-mass 342k Find threshold associated with this molecular weight (Da)",
31 " value can be given in Da, kDa (with k), MDa (with m) or Gda (with g).",
32 " Output binary mask (data type byte, values 0 and 1).",
33 "-threshold 1.54 Report mass calculated from the volume included at this threshold.",
34 " Output binary mask (data type byte, values 0 and 1).",
35 "-innervolume 2.3,15 Calculate internal volume below this threshold, output mask frequency.",
36 "-multiple 1.2 Generate multiple regions above this threshold.",
37 "-kmeans 12,0.1 Segment an image based on K-means and a density:distance ratio.",
38 "-extract 15,50,33 Extract a region around this voxel (use with -flood and -Region).",
39 " ",
40 "Parameters:",
41 "-verbose 7 Verbose output.",
42 "-datatype u Force writing of a new data type.",
43 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
44 "-sign -1 Sign for thresholding (default 0=set by sign of threshold-average;",
45 " 1=positive density; -1=negative density).",
46 "-rho 0.8 Protein density (default 0.81 Da/A3, use with -mass).",
47 " ",
48 "Input:",
49 "-Mask mask.tif Mask for extraction (must be integer and same size as input image).",
50 " ",
51 "Output:",
52 "-Region region.tif File name for the excised region.",
53 " ",
54 NULL
55 };
56
57 int main(int argc, char* argv[])
58 {
59 // Initialize variables
60 DataType nudatatype(Unknown_Type); // Conversion to new type
61 int setinvert(0); // Flag to invert density
62 double nuavg(0), nustd(0); // Rescaling to average and stdev
63 double threshold(-1e36); // Threshold for defining regions
64 int nregions(0); // Number of regions for K-means
65 double ratio(1); // Density-distance ratio
66 int region_mask(0); // Flag to generate a mask of regions
67 Vector3<double> origin; // New image origin
68 int set_origin(0); // Flag to set origin
69 int sign(0); // Sign for region mask threshold
70 double blob_threshold(-1e36); // Threshold for defining blobs
71 double min_size(0); // Minimum size of a blob to keep
72 double max_size(1000000000); // Maximum size of a blob to keep
73 double setvalue(0); // Value to set a deleted region to
74 double mol_weight(0); // Molecular weight for threshold finding
75 int internal_volume(0); // Flag to calculate internal volume
76 int mask_out_freq(0); // Frequency to output inner volume mask
77 double rho(RHO); // Protein density in Da/A3
78 int nvox(0); // Number of voxel coordinates
79 Vector3<long> voxel; // Voxel coordinates for region extraction
80 Bstring mask_file = NULL; // Mask file
81 Bstring pex_file = NULL; // Excised region file
82
83 int i, optind;
84 Boption* option = get_option_list(use, argc, argv, optind);
85 Boption* curropt;
86 for ( curropt = option; curropt; curropt = curropt->next ) {
87 if ( curropt->tag == "datatype" )
88 nudatatype = curropt->datatype();
89 if ( curropt->tag == "invert" )
90 setinvert = 1;
91 if ( curropt->tag == "rescale" )
92 if ( curropt->values(nuavg, nustd) < 2 )
93 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
94 if ( curropt->tag == "blobs" ) {
95 if ( ( i = curropt->values(blob_threshold, min_size, max_size, setvalue ) ) < 1 )
96 cerr << "-blobs: A threshold must be specified!" << endl;
97 else
98 if ( i < 4 ) setvalue = blob_threshold;
99 }
100 if ( curropt->tag == "mass" )
101 mol_weight = curropt->mass();
102 if ( curropt->tag == "threshold" )
103 if ( ( threshold = curropt->value.real() ) < -1e30 )
104 cerr << "-threshold: A threshold must be specified!" << endl;
105 if ( curropt->tag == "innervolume" ) {
106 if ( curropt->values(threshold, mask_out_freq ) < 1 )
107 cerr << "-innervolume: A threshold must be specified!" << endl;
108 else
109 internal_volume = 1;
110 }
111 if ( curropt->tag == "multiple" ) {
112 if ( curropt->values(threshold, sign) < 1 )
113 cerr << "-multiple: A threshold must be specified!" << endl;
114 else
115 region_mask = 1;
116 }
117 if ( curropt->tag == "kmeans" )
118 if ( curropt->values(nregions, ratio) < 1 )
119 cerr << "-kmeans: A number of regions must be specified!" << endl;
120 if ( curropt->tag == "extract" ) {
121 voxel = curropt->size();
122 nvox = 1;
123 }
124 if ( curropt->tag == "origin" ) {
125 if ( curropt->value[0] == 'c' ) {
126 set_origin = 2;
127 } else {
128 origin = curropt->origin();
129 set_origin = 1;
130 }
131 }
132 if ( curropt->tag == "sign" )
133 if ( ( sign = curropt->value.integer() ) == 0 )
134 cerr << "-sign: A sign value must be specified!" << endl;
135 if ( curropt->tag == "rho" )
136 if ( ( rho = curropt->value.real() ) < 0.001 )
137 cerr << "-rho: A protein density value must be specified!" << endl;
138 if ( curropt->tag == "Mask" )
139 mask_file = curropt->filename();
140 if ( curropt->tag == "Region" )
141 pex_file = curropt->filename();
142 }
143 option_kill(option);
144
145 double ti = timer_start();
146
147 // Read the input file
148 Bstring filename(argv[optind++]);
149 Bimage* p = read_img(filename, 1, -1);
150
151 p->label(get_cmd_line(argc, argv));
152
153 if ( nudatatype == Unknown_Type )
154 nudatatype = p->data_type();
155
156 if ( set_origin ) {
157 if ( set_origin == 2 ) p->origin(p->size()/2);
158 else p->origin(origin);
159 }
160
161 if ( setinvert ) p->invert();
162
163 if ( blob_threshold > -1e29 )
164 p->blobs(blob_threshold, min_size, max_size, setvalue, sign);
165
166 Bimage* pseg;
167 if ( nregions > 1 ) {
168 pseg = p->kmeans_segment(nregions, ratio);
169 delete p;
170 p = pseg;
171 }
172
173 if ( mol_weight > 0 )
174 threshold = p->mass_threshold(0, mol_weight, rho);
175 else if ( threshold )
176 p->mass_at_threshold(0, threshold, rho);
177
178 Bimage* pmask = NULL;
179 if ( region_mask ) {
180 pmask = p->regions(threshold, sign);
181 delete p;
182 p = pmask;
183 nudatatype = p->data_type();
184 }
185
186 if ( mol_weight > 0 ) {
187 p->to_mask(threshold);
188 nudatatype = p->data_type();
189 }
190
191 if ( internal_volume )
192 p->internal_volume(threshold, mask_out_freq);
193
194 Bimage* pex = NULL;
195
196 if ( mask_file.length() ) {
197 pmask = read_img(mask_file, 1, -1);
198 if ( pex_file.length() && nvox ) {
199 pmask->levelmask_select(0, voxel);
200 pex = p->mask_extract(pmask);
201 pex->change_type(nudatatype);
202 pex->label(get_cmd_line(argc, argv));
203 write_img(pex_file, pex, 0);
204 delete pex;
205 }
206 delete pmask;
207 }
208
209 // Write an output file if a file name is given
210 if ( optind < argc ) {
211 filename = argv[optind];
212 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
213 p->change_type(nudatatype);
214 p->label(get_cmd_line(argc, argv));
215 if ( internal_volume ) write_img(filename, p->next, 0);
216 else write_img(filename, p, 0);
217 }
218
219 delete p;
220
221 if ( verbose & VERB_TIME )
222 timer_report(ti);
223
224 bexit(0);
225 }
226
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::mask_extract
Bimage * mask_extract(Bimage *pmask)
Zeroes everything outside the mask and return the excised feature.
Definition: Bimage_mask.cpp:294
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
verbose
int verbose
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.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
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:551
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
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
Boption::mass
double mass()
Definition: options.h:281
main
int main(int argc, char *argv[])
Definition: bsegment.cpp:57
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
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
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:834
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:918
Bimage::levelmask_select
long levelmask_select(Bstring &select_list)
Retains the selected levels in a multi-level mask.
Definition: Bimage_mask.cpp:1650
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::internal_volume
double internal_volume(double threshold, int mask_out_freq)
Calculates the internal volume of a shell.
Definition: Bimage_segment.cpp:950
file_util.h
Header file for file checking functions.
Bimage::next
Bimage * next
Definition: Bimage.h:165
Bimage::to_mask
long to_mask()
Definition: Bimage.h:1299
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::kmeans_segment
Bimage * kmeans_segment(long nregion=2, double ratio=1)
Segments an image based on K-means.
Definition: Bimage_segment.cpp:1067
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
RHO
#define RHO
Definition: utilities.h:91
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
use
const char * use[]
Definition: bsegment.cpp:19
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
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
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563