Bsoft  2.1.0
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 
143  time_t t = time(NULL);
144 
145  if ( verbose ) {
146  cout << "Iter\tAvg\tStd\tTime" << endl;
147  cout << "0\t" << p->average() << tab << p->standard_deviation() << tab << (unsigned long)(time(NULL) - t) << endl << flush;
148  }
149 
150  for ( i=1; i<=imax; i++ ) {
151  if ( p->sizeZ() == 1 )
152  pd = p->nad_2D(ht, lambda, C, alpha);
153  else
154  pd = p->nad(ht, slabsize, lambda, C, alpha);
155  p->replace(pd);
156  p->statistics();
157 
158  /* check minimum, maximum, mean, variance */
159  if ( verbose )
160  cout << i << tab << setprecision(4) << p->average() << tab <<
161  p->standard_deviation() << tab << (size_t)(time(NULL) - t) << endl << flush;
162 
163  if ( verbose & VERB_PROCESS )
164  p->information();
165 
166  if ( i%iout == 0 && i != imax ) {
167  filename = argv[optind];
168  filename = filename.pre_rev('.') + Bstring(i, "_%04d.") + filename.post_rev('.');
169  pd->change_type(nudatatype);
170  write_img(filename, pd, 0);
171  }
172 
173  delete pd;
174 
175  if ( verbose & VERB_TIME )
176  timer_report(ti);
177 
178  }
179 
180  if ( optind < argc ) {
181  p->change_type(nudatatype);
182  write_img(argv[optind], p, 0);
183  }
184 
185  delete p;
186 
187  if ( verbose & VERB_TIME )
188  timer_report(ti);
189 
190  bexit(0);
191 }
192 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:324
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
main
int main(int argc, char **argv)
Definition: bnad.cpp:48
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
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
Bimage::information
int information()
Prints out header information for an image.
Definition: Bimage.cpp:2508
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
memory_check
int memory_check(long mem_required)
Checking if there is enough memory, abort if not.
Definition: utilities.cpp:255
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
use
const char * use[]
Definition: bnad.cpp:20
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
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:391
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage::data_size
long data_size()
Definition: Bimage.h:270
Bimage
General image parameter class.
Definition: Bimage.h:177
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:1371
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
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.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
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
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
verbose
int verbose
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22