Bsoft  2.1.0
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->scale();
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->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  bexit(0);
106 }
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
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
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.
Bimage::filter_rank
int filter_rank(long kernel_size, double rank)
Applies a median filter to an image.
Definition: Bimage_filter.cpp:1021
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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
verbose
int verbose
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
use
const char * use[]
Definition: bmedian.cpp:18
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
main
int main(int argc, char **argv)
Definition: bmedian.cpp:37
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
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