Bsoft  2.1.0
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->scale();
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  write_img(maskfile, pmask, 0);
126  }
127 
128  delete pmask;
129  string_kill(file_list);
130 
131  if ( verbose & VERB_TIME )
132  timer_report(ti);
133 
134  bexit(0);
135 }
136 
147 Bimage* img_composite_mask(Bstring* file_list, int mask_type)
148 {
149  long nimg(0);
150  Bimage* pmask = img_setup_combined(file_list, nimg);
151 
152  if ( !pmask ) return NULL;
153 
154  pmask->change_type(Integer);
155 
156  Bstring* filename;
157  Bimage* p = NULL;
158  int val, bit, base_bit;
159  long i, j, n, nf, nfiles(0), base_level, max_level;
160 
161  for ( nfiles=0, filename = file_list; filename; filename = filename->next ) nfiles++;
162 
163  if ( verbose ) {
164  if ( mask_type == 2 )
165  cout << "Generating a multi-bit mask from " << nfiles << " images:" << endl;
166  else
167  cout << "Generating a multi-level mask from " << nfiles << " images:" << endl;
168  }
169 
170  if ( verbose & VERB_PROCESS )
171  cout << "New mask size: " << pmask->size() << endl;
172 
173  long imgsize(pmask->image_size());
174 
175  for ( nf=0, base_bit=0, base_level=max_level=0, filename = file_list;
176  filename && nf<nfiles; filename = filename->next, nf++ ) {
177  p = read_img(*filename, 1, -1);
178  if ( p != NULL ) {
179  if ( verbose & VERB_LABEL )
180  cout << "Adding mask " << nf << endl;
181  p->change_type(Float) ;
182  for ( n=j=0; n<p->images(); n++ ) {
183  pmask->origin(n, p->image[n].origin());
184  for ( i=0; i<imgsize; i++, j++ ) {
185  if ( (*p)[j] > 0.5 ) { // What is zero stay whatever it currently is
186  val = (int) ((*p)[j] + 0.5);
187  if ( mask_type == 2 ) {
188  bit = val;
189  if ( base_bit > 0 ) bit <<= base_bit;
190  pmask->set(j, (char)(*pmask)[j] | bit);
191  } else {
192  pmask->set(j, base_level + val);
193  }
194  if ( max_level < (*pmask)[j] ) max_level = (int) (*pmask)[j];
195  }
196  }
197  }
198  delete p;
199  for ( base_bit = 0, val = max_level; val > 0; val >>= 1, base_bit++ ) ;
200  base_level = max_level;
201  }
202  }
203 
204  pmask->statistics();
205 
206  return pmask;
207 }
208 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
main
int main(int argc, char **argv)
Definition: baddmask.cpp:43
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
use
const char * use[]
Definition: baddmask.cpp:21
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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:281
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:291
Bimage::mask_stats
long mask_stats()
Calculates statistics for a mask.
Definition: Bimage_mask.cpp:165
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Integer
@ Integer
Definition: utilities.h:158
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bstring::next
Bstring * next
Definition: Bstring.h:31
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
img_combine.h
Header file for combining two images in various ways.
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
img_composite_mask
Bimage * img_composite_mask(Bstring *file_list, int mask_type)
Generates a composite of multiple masks.
Definition: baddmask.cpp:147
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
verbose
int verbose
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22