| Bsoft 2.0.5 Bernard's software package

bmedian.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: bmedian [options] input.img output.img",
21 "---------------------------------------------",
22 "Filters images.",
23 " ",
24 "Actions:",
25 "-rescale -0.1,5.2 Rescale data to average and standard deviation after filtering.",
26 " ",
27 "Parameters:",
28 "-verbose 1 Verbosity of output.",
29 "-datatype u Force writing of a new data type.",
30 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
31 "-kernel 5 Median filter kernel edge size (default 3).",
32 "-iterations 5 Number of iterations (default 1).",
33 " ",
34 NULL
35 };
36
37 int main(int argc, char **argv)
38 {
39 // Initialize variables
40 DataType nudatatype(Unknown_Type); // Conversion to new type
41 Vector3<double> sam; // Units for the three axes (A/pixel)
42 double nuavg(0), nustd(0); // Values for rescaling
43 int median_kernel(3); // Median filter kernel size
44 int i, iterations(1); // Number of filtering iterations
45
46 int optind;
47 Boption* option = get_option_list(use, argc, argv, optind);
48 Boption* curropt;
49 for ( curropt = option; curropt; curropt = curropt->next ) {
50 if ( curropt->tag == "datatype" )
51 nudatatype = curropt->datatype();
52 if ( curropt->tag == "sampling" )
53 sam = curropt->sampling();
54 if ( curropt->tag == "rescale" ) {
55 if ( curropt->values(nuavg, nustd) < 2 )
56 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
57 else if ( nustd <= 0 )
58 cerr << "-rescale: A positive standard deviation must be specified!" << endl;
59 }
60 if ( curropt->tag == "kernel" )
61 if ( ( median_kernel = curropt->value.integer() ) < 1 )
62 cerr << "-kernel: The kernel edge size must be specified!" << endl;
63 if ( curropt->tag == "iterations" )
64 if ( ( iterations = curropt->value.integer() ) < 1 )
65 cerr << "-iterations: A number of iterations must be specified!" << endl;
66 }
67 option_kill(option);
68
69 double ti = timer_start();
70
71 // Read image file
72 int dataflag(0);
73 if ( optind < argc - 1 ) dataflag = 1;
74 Bimage* p = read_img(argv[optind++], dataflag, -1);
75 if ( p == NULL ) bexit(-1);
76
77 if ( nudatatype == Unknown_Type )
78 nudatatype = p->data_type(); // Preserve old type
79
80 if ( nudatatype > p->data_type() )
81 p->change_type(nudatatype);
82
83 if ( sam.volume() ) p->sampling(sam);
84
85 if (verbose )
86 cout << "Iteration\tStDev" << endl;
87 for ( i=0; i<iterations; i++ ) {
88 p->filter_rank(median_kernel, 0.5);
89 if ( verbose )
90 cout << i+1 << tab << p->standard_deviation() << endl;
91 }
92
93 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
94
95 if ( optind < argc ) {
96 p->label(get_cmd_line(argc, argv));
97 p->change_type(nudatatype);
98 write_img(argv[optind], p, 0);
99 }
100
101 delete p;
102
103 if ( verbose & VERB_TIME )
104 timer_report(ti);
105
106 bexit(0);
107 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
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
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
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
verbose
int verbose
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
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
main
int main(int argc, char **argv)
Definition: bmedian.cpp:37
Bimage::filter_rank
int filter_rank(long kernel_size, double rank)
Applies a median filter to an image.
Definition: Bimage_filter.cpp:1022
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
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: bmedian.cpp:18
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21