| Bsoft 2.0.5 Bernard's software package

bnad.cpp
Go to the documentation of this file.
1
10 #include "rwimg.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: bnad [options] input.img output.img",
23 "------------------------------------------",
24 "Denoises by nonlinear anisotropic diffusion: Coherence and edge enhancing diffusion.",
25 "The default is edge enhancing diffusion.",
26 "Coherence enhancing diffusion is used if the coherence parameter is specified.",
27 "Diffusion is faster for higher lambda (1e-6 to 10 have been tested).",
28 " ",
29 "Actions:",
30 "-lambda 0.3 Lambda: edge enhancing diffusion (default 0.1).",
31 "-coherence 0.002 Coherence: coherence enhancing diffusion (default 0.001).",
32 "-sigma 2.5 Initial gaussian smoothing coefficient (default no smoothing).",
33 " ",
34 "Parameters:",
35 "-verbose 7 Verbosity of output.",
36 "-datatype u Force writing of a new data type.",
37 "-iterations 80 Iterations (default 1).",
38 "-slabsize 20 Slab size for piece-wise processing (default z size).",
39 "-output 5 Number of iterations between writing output maps (default 100).",
40 " ",
41 "Parameters for coherence enhancing diffusion:",
42 "-alpha 0.001 Alpha (default 0.001).",
43 "-rho 4 Gaussian smoothing of structure tensor (default 6).",
44 " ",
45 NULL
46 };
47
48 int main (int argc, char **argv)
49 {
50 DataType nudatatype(Unknown_Type); // Conversion to new type
51 long i; /* loop variables */
52 long imax(1); /* largest iteration number */
53 long slabsize(0); // Slab size for threaded runs
54 long iout(100); /* iterations between writing output maps */
55 double ht(0.1); /* time step size */
56 double sigma(0); /* noise scale - Presmoothing */
57 double lambda(0.1); /* lamda Parameter */
58 double alpha(0.001); /* linear diffusion fraction */
59 double rho(6); /* integration scale */
60 double C(0); /* coherence parameter */
61
62 int optind;
63 Boption* option = get_option_list(use, argc, argv, optind);
64 Boption* curropt;
65 for ( curropt = option; curropt; curropt = curropt->next ) {
66 if ( curropt->tag == "lambda" )
67 if ( ( lambda = curropt->value.real() ) <= 0 )
68 cerr << "-lambda: Lambda must be specified" << endl;
69 if ( curropt->tag == "coherence" )
70 if ( ( C = curropt->value.real() ) <= 0 )
71 cerr << "-coherence: Coherence must be specified" << endl;
72 if ( curropt->tag == "sigma" )
73 if ( ( sigma = curropt->value.real() ) <= 0 )
74 cerr << "-sigma: A value must be specified" << endl;
75 if ( curropt->tag == "datatype" )
76 nudatatype = curropt->datatype();
77 if ( curropt->tag == "iterations" )
78 if ( ( imax = curropt->value.integer() ) < 1 )
79 cerr << "-iterations: A number of iterations must be specified!" << endl;
80 if ( curropt->tag == "slabsize" )
81 if ( ( slabsize = curropt->value.integer() ) < 3 )
82 cerr << "-slabsize: A slab size must be specified!" << endl;
83 if ( curropt->tag == "output" )
84 if ( ( iout = curropt->value.integer() ) < 1 )
85 cerr << "-output: A number of iterations must be specified!" << endl;
86 if ( curropt->tag == "alpha" )
87 if ( ( alpha = curropt->value.real() ) <= 0 )
88 cerr << "-alpha: Alpha must be specified" << endl;
89 if ( curropt->tag == "rho" )
90 if ( ( rho = curropt->value.real() ) < 1 )
91 cerr << "-rho: A value must be specified" << endl;
92 }
93 option_kill(option);
94
95 double ti = timer_start();
96
97 int dataflag(0);
98 if ( optind < argc - 1 ) dataflag = 1;
99 Bimage* p = read_img(argv[optind++], dataflag, -1);
100 if ( p == NULL ) {
101 cerr << "Error: No input file read!" << endl;
102 bexit(-1);
103 }
104
105 if ( nudatatype == Unknown_Type )
106 nudatatype = p->data_type();
107
108 p->change_type(Float);
109
110 if ( slabsize < 1 ) slabsize = p->sizeZ();
111
112 long memreq(p->sizeX()*p->sizeY()*sizeof(float));
113 if ( p->sizeZ() == 1 )
114 memreq *= 3;
115 else {
116 memreq *= 6*slabsize;
117 memreq *= system_processors();
118 }
119 memreq += 2*p->data_size()*sizeof(float);
120
121 if ( verbose ) {
122 cout << "Denoising by non-linear anisotropic diffusion: ";
123 if ( C ) cout << "Coherence enhancing diffusion" << endl;
124 else cout << "Edge enhancing diffusion" << endl;
125 cout << "Image size: " << p->size() << endl;
126 if ( p->sizeZ() == 1 )
127 cout << "Images: " << p->images() << endl;
128 else
129 cout << "Slab thickness: " << slabsize << endl;
130 }
131
132 memory_check(memreq);
133
134 if ( sigma > 0 ) {
135 if ( verbose )
136 cout << "Gaussian filter sigma: " << sigma << endl;
137 p->filter_gaussian((long) (6*sigma), sigma);
138 }
139
140 Bstring filename;
141 Bimage* pd = NULL;
142 p->label(get_cmd_line(argc, argv));
143
144 time_t t = time(NULL);
145
146 if ( verbose ) {
147 cout << "Iter\tAvg\tStd\tTime" << endl;
148 cout << "0\t" << p->average() << tab << p->standard_deviation() << tab << (unsigned long)(time(NULL) - t) << endl << flush;
149 }
150
151 for ( i=1; i<=imax; i++ ) {
152 if ( p->sizeZ() == 1 )
153 pd = p->nad_2D(ht, lambda, C, alpha);
154 else
155 pd = p->nad(ht, slabsize, lambda, C, alpha);
156 p->replace(pd);
157 p->statistics();
158
159 /* check minimum, maximum, mean, variance */
160 if ( verbose )
161 cout << i << tab << setprecision(4) << p->average() << tab <<
162 p->standard_deviation() << tab << (size_t)(time(NULL) - t) << endl << flush;
163
164 if ( verbose & VERB_PROCESS )
165 p->information();
166
167 if ( i%iout == 0 && i != imax ) {
168 filename = argv[optind];
169 filename = filename.pre_rev('.') + Bstring(i, "_%04d.") + filename.post_rev('.');
170 pd->change_type(nudatatype);
171 write_img(filename, pd, 0);
172 }
173
174 delete pd;
175
176 if ( verbose & VERB_TIME )
177 timer_report(ti);
178
179 }
180
181 if ( optind < argc ) {
182 p->change_type(nudatatype);
183 write_img(argv[optind], p, 0);
184 }
185
186 delete p;
187
188 if ( verbose & VERB_TIME )
189 timer_report(ti);
190
191 bexit(0);
192 }
193
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2828
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::information
int information()
Prints out header information for an image.
Definition: Bimage.cpp:2489
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
use
const char * use[]
Definition: bnad.cpp:20
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
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.
Float
Definition: utilities.h:157
Bimage::nad
Bimage * nad(double ht, long zw, double lambda, double C, double alpha)
Denoises a 3D density map by non-linear anisotropic diffusion.
Definition: Bimage_nad.cpp:578
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:325
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
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
main
int main(int argc, char **argv)
Definition: bnad.cpp:48
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
memory_check
int memory_check(long mem_required)
Checking if there is enough memory, abort if not.
Definition: utilities.cpp:248
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
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
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1349
verbose
int verbose
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::images
long images()
Definition: Bimage.h:491
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Bimage::nad_2D
Bimage * nad_2D(double ht, double lambda, double C, double alpha)
Denoises a 2D image by non-linear anisotropic diffusion.
Definition: Bimage_nad.cpp:552
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
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
Bimage::data_size
long data_size()
Definition: Bimage.h:255
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21