Bsoft  2.1.0
Bernard's software package
bkernel.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "FSI_Kernel.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 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: bkernel [options] output.krn",
22 "-----------------------------------",
23 "Calculates filter kernels.",
24 "The default kernel just calculates the mean.",
25 "The different types of kernel are mutually exclusive.",
26 " ",
27 "Actions:",
28 "-gaussian 1.7,8.4 Gaussian kernel with the indicated sigma and maximum.",
29 "-log 2.5,5 Laplacian of gaussian kernel with the indicated sigma and maximum.",
30 "-frequency 8,2 Frequency space kernel: width and power.",
31 " ",
32 "Parameters:",
33 "-verbose 7 Verbosity of output.",
34 "-size 5,7,9 Size of kernel (voxels).",
35 " ",
36 NULL
37 };
38 
39 int main(int argc, char **argv)
40 {
41  // Initialize variables
42  Vector3<long> size; // Kernel size
43  double sigma(0); // Gaussian sigma
44  double log_sigma(0); // LoG sigma
45  double max(1); // Gaussian or LoG maximum
46  int kernel_width(0); // Frequency space kernel width
47  int kernel_power(2); // Frequency space kernel power
48 
49  int optind;
50  Boption* option = get_option_list(use, argc, argv, optind);
51  Boption* curropt;
52  for ( curropt = option; curropt; curropt = curropt->next ) {
53  if ( curropt->tag == "gaussian" )
54  if ( curropt->values(sigma, max) < 1 )
55  cerr << "-gaussian: A sigma value must be specified!" << endl;
56  if ( curropt->tag == "log" )
57  if ( curropt->values(log_sigma, max) < 1 )
58  cerr << "-log: A sigma value must be specified!" << endl;
59  if ( curropt->tag == "frequency" )
60  if ( curropt->values(kernel_width, kernel_power) < 1 )
61  cerr << "-frequency: At least the kernel size must be specified!" << endl;
62  if ( curropt->tag == "size" )
63  size = curropt->size();
64  }
65  option_kill(option);
66 
67  double ti = timer_start();
68 
69  int side;
70  FSI_Kernel ker;
71 
72  if ( size.volume() < 1 ) {
73  if ( log_sigma > 0.001 ) {
74  side = 2*( (int) (4*log_sigma + 0.9) ) + 1;
75  size = {side, side, side};
76  } else if ( sigma > 0.001 ) {
77  side = 2*( (int) (4*sigma + 0.9) ) + 1;
78  size = {side, side, side};
79  } else if ( kernel_width ) {
80  ker = FSI_Kernel(kernel_width, kernel_power);
81  size = {ker.width(), ker.divisions() + 1, 1};
82  } else {
83  size = {3, 3, 3};
84  }
85  }
86 
87  Bimage* p = new Bimage(Float, TSimple, size, 1);
88  p->origin(p->default_origin());
89 
90  if ( log_sigma ) {
91  p->kernel_laplacian_of_gaussian(log_sigma, max);
92  } else if ( sigma > 0.001 ) {
93  p->kernel_gaussian(sigma, max);
94  } else if ( kernel_width ) {
95  for ( long i=0; i<p->data_size(); ++i ) p->set(i, ker[i]);
96  } else {
97  p->fill(1);
98  }
99 
100  if ( p && optind < argc ) {
101  write_img(argv[optind], p, 0);
102  }
103 
104  delete p;
105 
106  if ( verbose & VERB_TIME )
107  timer_report(ti);
108 
109  return 0;
110 }
111 
Boption
A class for a command line input option.
Definition: options.h:26
FSI_Kernel::width
long width()
Definition: FSI_Kernel.h:70
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
FSI_Kernel.h
Header file for frequency space interpolation kernel.
options.h
Header file for option handlers.
main
int main(int argc, char **argv)
Definition: bkernel.cpp:39
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
TSimple
@ TSimple
Definition: Bimage.h:59
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::data_size
long data_size()
Definition: Bimage.h:270
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
FSI_Kernel::divisions
long divisions()
Definition: FSI_Kernel.h:72
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
verbose
int verbose
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Bimage::kernel_gaussian
int kernel_gaussian(double sigma, double max)
Generates a gaussian kernel image.
Definition: Bimage_filter.cpp:34
Boption::size
Vector3< long > size()
Definition: options.h:163
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
use
const char * use[]
Definition: bkernel.cpp:19
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::kernel_laplacian_of_gaussian
int kernel_laplacian_of_gaussian(double sigma, double max)
Generates a laplacian-of-gaussian kernel.
Definition: Bimage_filter.cpp:77
Float
@ Float
Definition: utilities.h:161
FSI_Kernel
Definition: FSI_Kernel.h:14