| Bsoft 2.0.5 Bernard's software package

baddmask.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "img_combine.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 Bimage* img_composite_mask(Bstring* file_list, int mask_type);
19
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: baddmask [options] input.images",
24 "--------------------------------------",
25 "Adds multiple masks to generate a new composite mask.",
26 "Any number of input images may be given and may include the wild card \"*\".",
27 " ",
28 "Actions:",
29 "-type bit Type of composite mask: uniform/bit/level.",
30 " ",
31 "Parameters:",
32 "-verbose 7 Verbosity of output.",
33 "-datatype u Force writing of a new data type.",
34 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
35 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
36 " ",
37 "Output:",
38 "-output output.img Output image (default sum.map).",
39 " ",
40 NULL
41 };
42
43 int main(int argc, char **argv)
44 {
45 // Initialize variables
46 int mask_type(0); // Default: simply add images
47 DataType nudatatype(Unknown_Type); // Conversion to new type
48 Vector3<double> sam; // Units for the three axes (A/pixel)
49 Vector3<double> origin; // New image origin
50 int set_origin(0); // Flag to set origin
51 Bstring maskfile("mask.tiff"); // Default output image file name
52
53 int optind;
54 Boption* option = get_option_list(use, argc, argv, optind);
55 Boption* curropt;
56 for ( curropt = option; curropt; curropt = curropt->next ) {
57 if ( curropt->tag == "type" ) {
58 if ( curropt->value[0] == 'u' ) mask_type = 1;
59 if ( curropt->value[0] == 'b' ) mask_type = 2;
60 if ( curropt->value[0] == 'l' ) mask_type = 3;
61 }
62 if ( curropt->tag == "datatype" )
63 nudatatype = curropt->datatype();
64 if ( curropt->tag == "sampling" )
65 sam = curropt->sampling();
66 if ( curropt->tag == "origin" ) {
67 if ( curropt->value[0] == 'c' ) {
68 set_origin = 2;
69 } else {
70 origin = curropt->origin();
71 set_origin = 1;
72 }
73 }
74 if ( curropt->tag == "output" )
75 maskfile = curropt->filename();
76 }
77 option_kill(option);
78
79 if ( verbose && argc < 3 ) {
80 cerr << "Error: No input files given!" << endl;
81 bexit(-1);
82 }
83
84 double ti = timer_start();
85
86 if ( optind >= argc ) {
87 cerr << "No input files!" << endl;
88 bexit(-1);
89 }
90
91 // Set up image file names and weights
92 int nfiles(0);
93 Bstring* file_list = NULL;
94 while ( optind < argc ) {
95 string_add(&file_list, argv[optind++]);
96 nfiles++;
97 }
98
99 Bimage* pmask = NULL;
100 vector<double> weight;
101
102 switch ( mask_type ) {
103 case 1:
104 pmask = img_add_weighed(file_list, weight);
105 pmask->truncate_to_min_max(0, 1);
106 break;
107 case 2:
108 case 3:
109 pmask = img_composite_mask(file_list, mask_type);
110 break;
111 default:
112 pmask = img_add_weighed(file_list, weight);
113 }
114
115 if ( sam.volume() > 0 ) pmask->sampling(sam);
116
117 if ( set_origin ) {
118 if ( set_origin == 2 ) pmask->origin(pmask->default_origin());
119 else pmask->origin(origin);
120 }
121 pmask->mask_stats();
122
123 if ( maskfile.length() ) {
124 pmask->change_type(nudatatype);
125 pmask->label(get_cmd_line(argc, argv));
126 write_img(maskfile, pmask, 0);
127 }
128
129 delete pmask;
130 string_kill(file_list);
131
132 if ( verbose & VERB_TIME )
133 timer_report(ti);
134
135 bexit(0);
136 }
137
148 Bimage* img_composite_mask(Bstring* file_list, int mask_type)
149 {
150 long nimg(0);
151 Bimage* pmask = img_setup_combined(file_list, nimg);
152
153 if ( !pmask ) return NULL;
154
155 pmask->change_type(Integer);
156
157 Bstring* filename;
158 Bimage* p = NULL;
159 int val, bit, base_bit;
160 long i, j, n, nf, nfiles(0), base_level, max_level;
161
162 for ( nfiles=0, filename = file_list; filename; filename = filename->next ) nfiles++;
163
164 if ( verbose ) {
165 if ( mask_type == 2 )
166 cout << "Generating a multi-bit mask from " << nfiles << " images:" << endl;
167 else
168 cout << "Generating a multi-level mask from " << nfiles << " images:" << endl;
169 }
170
171 if ( verbose & VERB_PROCESS )
172 cout << "New mask size: " << pmask->size() << endl;
173
174 long imgsize(pmask->image_size());
175
176 for ( nf=0, base_bit=0, base_level=max_level=0, filename = file_list;
177 filename && nf<nfiles; filename = filename->next, nf++ ) {
178 p = read_img(*filename, 1, -1);
179 if ( p != NULL ) {
180 if ( verbose & VERB_LABEL )
181 cout << "Adding mask " << nf << endl;
182 p->change_type(Float) ;
183 for ( n=j=0; n<p->images(); n++ ) {
184 pmask->origin(n, p->image[n].origin());
185 for ( i=0; i<imgsize; i++, j++ ) {
186 if ( (*p)[j] > 0.5 ) { // What is zero stay whatever it currently is
187 val = (int) ((*p)[j] + 0.5);
188 if ( mask_type == 2 ) {
189 bit = val;
190 if ( base_bit > 0 ) bit <<= base_bit;
191 pmask->set(j, (char)(*pmask)[j] | bit);
192 } else {
193 pmask->set(j, base_level + val);
194 }
195 if ( max_level < (*pmask)[j] ) max_level = (int) (*pmask)[j];
196 }
197 }
198 }
199 delete p;
200 for ( base_bit = 0, val = max_level; val > 0; val >>= 1, base_bit++ ) ;
201 base_level = max_level;
202 }
203 }
204
205 pmask->statistics();
206
207 return pmask;
208 }
209
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
img_composite_mask
Bimage * img_composite_mask(Bstring *file_list, int mask_type)
Generates a composite of multiple masks.
Definition: baddmask.cpp:148
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::image_size
long image_size()
Definition: Bimage.h:267
Bimage::mask_stats
long mask_stats()
Calculates statistics for a mask.
Definition: Bimage_mask.cpp:165
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Integer
Definition: utilities.h:154
verbose
int verbose
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::truncate_to_min_max
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:331
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.
Float
Definition: utilities.h:157
img_add_weighed
Bimage * img_add_weighed(Bstring *file_list, vector< double > weight, double newavg=0, double newstd=0, int flags=0)
Adds multiple images together with given weights.
Definition: img_combine.cpp:288
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bstring::next
Bstring * next
Definition: Bstring.h:31
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
img_combine.h
Header file for combining two images in various ways.
img_setup_combined
Bimage * img_setup_combined(Bstring *file_list, long &nimg, int cat=0)
Sets up a list of images for concatenation or summation.
Definition: img_combine.cpp:107
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
use
const char * use[]
Definition: baddmask.cpp:21
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
main
int main(int argc, char **argv)
Definition: baddmask.cpp:43
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
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
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::images
long images()
Definition: Bimage.h:491
Vector3::volume
double volume()
Definition: Vector3.h:235
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
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::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760