Bsoft  2.1.0
Bernard's software package
bhisto.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bhisto [options] input.img output.img",
21 "--------------------------------------------",
22 "Calculates the histogram of an image and associated statistics.",
23 " ",
24 "Actions:",
25 "-bins 256 Calculate histogram over a number of bins.",
26 "-fit gauss,2 Do a fit to the histogram (gaussian, poisson).",
27 " Second argument indicates number of gaussians.",
28 "-percentiles Calculate the percentiles for an image.",
29 " ",
30 "Parameters:",
31 "-verbose 7 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-select 1 Select an image (default all).",
34 "-sampling 2,3.5,1 Sampling (angstrom/voxel, a single value sets all three).",
35 " ",
36 "Output:",
37 "-Postscript histo.ps Postscript output file name.",
38 " ",
39 NULL
40 };
41 
42 int main(int argc, char **argv)
43 {
44  // Initialize variables
45  DataType nudatatype(Unknown_Type); // Conversion to new type
46  long bins(0); // Number of bins, also selects histogram
47  int setimg(-1); // Select all images
48  Bstring setfit; // Fitting specification: gau,poi,cou
49  int fitprm(1); // Fit parameter: gaussians or counting flags
50  int percentiles(0); // Calculate the percentiles for an image
51  Vector3<double> sam; // Units for the three axes (A/pixel)
52  Bstring psfile; // Postscript output file
53 
54  int optind;
55  Boption* option = get_option_list(use, argc, argv, optind);
56  Boption* curropt;
57  for ( curropt = option; curropt; curropt = curropt->next ) {
58  if ( curropt->tag == "bins" ) {
59  if ( ( bins = curropt->value.integer() ) < 1 )
60  cerr << "-bins: The number of bins must be specified!" << endl;
61  else if ( bins < 2 ) {
62  bins = 10;
63  cerr << "-bins: Too few bins, changed to 10!" << endl;
64  } else if ( bins > 100000 ) {
65  bins = 1000;
66  cerr << "-bins: Too many bins, changed to 1000!" << endl;
67  }
68  }
69  if ( curropt->tag == "fit" ) {
70  setfit = curropt->value;
71  if ( setfit.contains(",") ) {
72  setfit = setfit.pre(',');
73  fitprm = curropt->value.post(',').integer();
74  }
75  }
76  if ( curropt->tag == "percentiles" ) percentiles = 1;
77  if ( curropt->tag == "datatype" )
78  nudatatype = curropt->datatype();
79  if ( curropt->tag == "select" )
80  if ( ( setimg = curropt->value.integer() ) < 0 )
81  cerr << "-select: An image number must be specified!" << endl;
82  if ( curropt->tag == "sampling" )
83  sam = curropt->scale();
84  if ( curropt->tag == "Postscript" )
85  psfile = curropt->filename();
86  }
87  option_kill(option);
88 
89  double ti = timer_start();
90 
91  // Read image file
92  int dataflag(0);
93  if ( bins || percentiles || optind < argc - 1 ) dataflag = 1;
94  Bimage* p = read_img(argv[optind++], dataflag, setimg);
95  if ( p == NULL ) {
96  cerr << "Error: No input file read!" << endl;
97  bexit(-1);
98  }
99 
100  if ( sam.volume() ) p->sampling(sam);
101 
102  Bplot* plot = NULL;
103 
104  if ( bins ) {
105  if ( setfit.length() ) {
106  if ( setfit[0] == 'g' )
107  plot = p->histogram_gauss_plot(bins, fitprm);
108  else if ( setfit[0] == 'p' )
109  plot = p->histogram_poisson_fit(bins, 1);
110  else if ( setfit[0] == 'c' )
111  plot = p->histogram_counts(3);
112  else
113  cerr << "Incorrect fitting specification: " << setfit << endl;
114  } else {
115  plot = p->histogram(bins);
116  }
117  } else if ( percentiles ) {
118  plot = p->percentiles();
119  }
120 
121  if ( psfile.length() && plot )
122  ps_plot(psfile, plot);
123 
124  if ( optind < argc ) {
125  p->change_type(nudatatype);
126  write_img(argv[optind], p, 0);
127  }
128 
129  if ( plot ) delete plot;
130  delete p;
131 
132  if ( verbose & VERB_TIME )
133  timer_report(ti);
134 
135  bexit(0);
136 }
137 
Boption
A class for a command line input option.
Definition: options.h:26
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
Boption::scale
Vector3< double > scale()
Definition: options.h:190
use
const char * use[]
Definition: bhisto.cpp:18
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< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::histogram_counts
Bplot * histogram_counts(int flags=0)
Finds the peaks in a quantized image from the histogram.
Definition: Bimage_histogram.cpp:207
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
Bplot
Definition: Bplot.h:146
options.h
Header file for option handlers.
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
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
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
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::histogram_poisson_fit
Bplot * histogram_poisson_fit(long bins, int flag=0)
Fits a poisson function to a histogram of an image.
Definition: Bimage_histogram.cpp:977
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::percentiles
Bplot * percentiles()
Calculates the percentiles from the histogram of an image.
Definition: Bimage_histogram.cpp:344
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::histogram_gauss_plot
Bplot * histogram_gauss_plot(long bins, long ngauss=1)
Fits a gaussian function to a histogram of an image.
Definition: Bimage_histogram.cpp:702
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.
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
main
int main(int argc, char **argv)
Definition: bhisto.cpp:42
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bimage::histogram
vector< long > histogram(long bins, double &scale, double &offset)
Calculates the histogram of an image.
Definition: Bimage_histogram.cpp:32