| Bsoft 2.0.5 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->sampling();
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 p->label(get_cmd_line(argc, argv));
127 write_img(argv[optind], p, 0);
128 }
129
130 if ( plot ) delete plot;
131 delete p;
132
133 if ( verbose & VERB_TIME )
134 timer_report(ti);
135
136 bexit(0);
137 }
138
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::histogram
long * histogram(long bins, double &scale, double &offset)
Calculates the histogram of an image.
Definition: Bimage_histogram.cpp:32
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
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
Bstring::length
long length() const
Definition: Bstring.cpp:263
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::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:695
Bimage
General image parameter class.
Definition: Bimage.h:163
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
main
int main(int argc, char **argv)
Definition: bhisto.cpp:42
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:965
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Vector3< double >
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
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
use
const char * use[]
Definition: bhisto.cpp:18
Bimage::histogram_counts
Bplot * histogram_counts(int flags=0)
Finds the peaks in a quantized image from the histogram.
Definition: Bimage_histogram.cpp:206
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::percentiles
Bplot * percentiles()
Calculates the percentiles from the histogram of an image.
Definition: Bimage_histogram.cpp:337
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
verbose
int verbose
Bplot
Definition: Bplot.h:146