Bsoft  2.1.0
Bernard's software package
bdens.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "options.h"
11 #include "utilities.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 
18 /* Usage assistence */
19 const char* use[] = {
20 " ",
21 "Usage: bdens [options] input.img output.img",
22 "-------------------------------------------",
23 "Calculate density in selected regions of a map.",
24 " ",
25 "Actions:",
26 "-box 12,34,2,45,34,25 Calculate statistics within this box (start and size).",
27 "-oval 5,10,28,50,22,89 Calculate statistics within this oval (start and size).",
28 " ",
29 "Parameters:",
30 "-verbose 7 Verbose output.",
31 "-datatype u Force writing of a new data type.",
32 "-select 1 Select an image (first image = 0; default: 0).",
33 " ",
34 "Input:",
35 "-Mask mask.map Calculate statistics with a mask with any number of levels.",
36 " ",
37 NULL
38 };
39 
40 int main(int argc, char* argv[])
41 {
42  // Initialize variables
43  int stat_type(0); // Type: 1=box, 2=oval
44  Vector3<long> start, size; // Start and size of box or oval to use
45  DataType nudatatype(Unknown_Type); // Conversion to new type
46  int setimg(0); // Select first image
47  Bstring maskfile; // Mask file
48 
49  int optind;
50  Boption* option = get_option_list(use, argc, argv, optind);
51  Boption* curropt;
52  for ( curropt = option; curropt; curropt = curropt->next ) {
53  if ( curropt->tag == "box" ) {
54  if ( curropt->box(start, size) < 6 )
55  cerr << "-box: A starting position and size must be specified!" << endl;
56  else
57  stat_type = 1;
58  }
59  if ( curropt->tag == "oval" ) {
60  if ( curropt->box(start, size) < 6 )
61  cerr << "-oval: A starting position and size must be specified!" << endl;
62  else
63  stat_type = 2;
64  }
65  if ( curropt->tag == "datatype" )
66  nudatatype = curropt->datatype();
67  if ( curropt->tag == "select" )
68  if ( ( setimg = curropt->value.integer() ) < 0 )
69  cerr << "-select: An image number must be specified!" << endl;
70  if ( curropt->tag == "Mask" )
71  maskfile = curropt->filename();
72  }
73  option_kill(option);
74 
75  double ti = timer_start();
76 
77  // Read the input file
78  Bimage* p = read_img(argv[optind++], 1, -1);
79 
80  long num(0);
81  double avg(0), std(0);
82  Bimage* pmask = NULL;
83  if ( maskfile.length() ) {
84  pmask = read_img(maskfile, 1, -1);
85  p->stats_in_mask(setimg, pmask);
86  delete pmask;
87  } else if ( stat_type > 0 ) {
88  num = p->stats_in_shape(setimg, stat_type, start, start+size, avg, std);
89  cout << "Number of voxels: " << num << endl;
90  cout << "Average density: " << avg << endl;
91  cout << "Standard deviation: " << std << endl << endl;
92  }
93 
94  // Write an output file if a file name is given
95  if ( optind < argc ) {
96  p->change_type(nudatatype);
97  write_img(argv[optind], p, 0);
98  }
99 
100  delete p;
101 
102  if ( verbose & VERB_TIME )
103  timer_report(ti);
104 
105  return(0);
106 }
107 
Boption
A class for a command line input option.
Definition: options.h:26
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
main
int main(int argc, char *argv[])
Definition: bdens.cpp:40
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
options.h
Header file for option handlers.
verbose
int verbose
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Boption::box
long box(Vector3< T1 > &st, Vector3< T2 > &sz)
Definition: options.h:202
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
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
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::stats_in_mask
long stats_in_mask(long nn, Bimage *pmask)
Calculates the statistics for an image for each level in a mask.
Definition: Bimage_stats.cpp:490
use
const char * use[]
Definition: bdens.cpp:19
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::stats_in_shape
long stats_in_shape(long nn, int type, Vector3< long > start, Vector3< long > end, double &vavg, double &vstd)
Calculates the statistics for an image within the given box.
Definition: Bimage_stats.cpp:282