Bsoft  2.1.0
Bernard's software package
bbif.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: bbif [options] input.img output.img",
21 "------------------------------------------",
22 "Denoises by bilateral filtering.",
23 " ",
24 "Actions:",
25 "-rescale -0.1,5.2 Rescale data to average and standard deviation after filtering.",
26 "-kernel gaussian Kernel used for for the intensity space:",
27 " 1=gaussian (default)",
28 " 2=lorentz ",
29 " 3=tukey",
30 "-size 5 kernel size (odd number, default 6*spacesigma)",
31 "-spacesigma 1.5 Standard deviation of spatial filter (pixels, default 1).",
32 "-rangesigma 23.8 Standard deviation of range filter (default image standard deviation).",
33 " ",
34 "Parameters:",
35 "-verbose 1 Verbosity of output.",
36 "-datatype u Force writing of a new data type.",
37 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
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  Vector3<double> sam; // Units for the three axes (A/pixel)
47  double nuavg(0), nustd(0); // Values for rescaling
48  double spacesigma(1); // Standard deviation of space filter
49  double rangesigma(-1); // Standard deviation of range filter
50  int kernel_type(1); // kernel type for range filter
51  // 1=gaussian; 2=lorentz; 3=tukey biweight
52  int kernel_size(0); // kernel size
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 == "datatype" )
59  nudatatype = curropt->datatype();
60  if ( curropt->tag == "sampling" )
61  sam = curropt->scale();
62  if ( curropt->tag == "rescale" ) {
63  if ( curropt->values(nuavg, nustd) < 2 )
64  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
65  else if ( nustd <= 0 )
66  cerr << "-rescale: A positive standard deviation must be specified!" << endl;
67  }
68  if ( curropt->tag == "kernel" ) {
69  if ( ( kernel_type = curropt->value.integer() ) < 1 ) {
70  for ( int i=0; i<(int)curropt->value.length(); i++ )
71  curropt->value[i] = tolower(curropt->value[i]);
72  if ( curropt->value[0] == 'g' ) {
73  kernel_type = 1;
74  } else if ( curropt->value[0] == 'l' ) {
75  kernel_type =2;
76  } else if ( curropt->value[0] == 't' ) {
77  kernel_type =3;
78  }
79  }
80  if ( kernel_type < 1 || kernel_type > 3 ) kernel_type = 1;
81  }
82  if ( curropt->tag == "spacesigma" )
83  if ( ( spacesigma = curropt->value.real() ) < 0.001 )
84  cerr << "-spacesigma: a space sigma must be specified!" << endl;
85  if ( curropt->tag == "rangesigma" )
86  if ( ( rangesigma = curropt->value.real() ) < 0.001 )
87  cerr << "-rangesigma: a range sigma must be specified!" << endl;
88  if ( curropt->tag == "size" )
89  if ( ( kernel_size = curropt->value.integer() ) < 1 )
90  cerr << "-size: a kernel size must be specified!" << endl;
91  }
92  option_kill(option);
93 
94  double ti = timer_start();
95 
96  // Read image file
97  int dataflag(0);
98  if ( optind < argc - 1 ) dataflag = 1;
99  Bimage* p = read_img(argv[optind++], dataflag, -1);
100  if ( p == NULL ) bexit(-1);
101 
102  if ( nudatatype == Unknown_Type )
103  nudatatype = p->data_type(); // Preserve old type
104 
105  if ( sam.volume() ) p->sampling(sam);
106 
107  p->filter_bilateral(spacesigma, rangesigma, kernel_type, kernel_size/2);
108 
109  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
110 
111  if ( optind < argc ) {
112  p->change_type(nudatatype);
113  write_img(argv[optind], p, 0);
114  }
115 
116  delete p;
117 
118  if ( verbose & VERB_TIME )
119  timer_report(ti);
120 
121  bexit(0);
122 }
123 
124 
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
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
verbose
int verbose
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
main
int main(int argc, char **argv)
Definition: bbif.cpp:42
Boption::value
Bstring value
Definition: options.h:30
use
const char * use[]
Definition: bbif.cpp:18
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
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.
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
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::filter_bilateral
int filter_bilateral(double sigma1, double sigma2, int kernel_type, long kernel_radius)
Denoise an image with combined gaussian distance and density difference kernel.
Definition: Bimage_filter.cpp:783
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