| Bsoft 2.0.5 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->sampling();
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->label(get_cmd_line(argc, argv));
113 p->change_type(nudatatype);
114 write_img(argv[optind], p, 0);
115 }
116
117 delete p;
118
119 if ( verbose & VERB_TIME )
120 timer_report(ti);
121
122 bexit(0);
123 }
124
125
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
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
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:784
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
use
const char * use[]
Definition: bbif.cpp:18
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
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
main
int main(int argc, char **argv)
Definition: bbif.cpp:42
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.
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