Bsoft  2.1.0
Bernard's software package
radon.cpp
Go to the documentation of this file.
1 
13 #include "rwimg.h"
14 #include "img_radon.h"
15 #include "utilities.h"
16 #include "options.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Usage assistence
22 const char *use[] = {
23  " ",
24  "Usage: radon [options] input.img output.img",
25  "-------------------------------------------",
26  "Program to do radon transforms.",
27  "Full transform: 2PI*2PI",
28  "Quarter transform: PI*PI",
29  " ",
30  "Types of forward radon transformation:",
31  "1 From 3D structure to single axis proj.",
32  "2 From single axis proj. to quarter radon transform",
33  "3 From 3D structure to full radon transform",
34  "4 From 3D structure to quarter radon transform",
35  " ",
36  "Types of backward radon transformation:",
37  "1 From single axis proj. to 3D structure",
38  "2 From quarter radon transform to single axis proj.",
39  "3 From full radon transform to 3D structure",
40  "4 From quarter radon transform to 3D structure",
41  " ",
42  "POCS specifications:",
43  "POCS filter works only on cube quarter radon transform,",
44  "that means theta = 2 * side of the cubic 3D structure map.",
45  "Side must be equal to a power of 2.",
46  "If radon transform has holes, POCS filter needs mask file.",
47  " ",
48  "Actions:",
49  "-forward 4 Forward radon transform (default not)",
50  "-pocs 5,5,30,30,0 POCS density modification on radon transform (default not)",
51  " Parameters (in order): outer cycles, inner cycles, ",
52  " 3D limit, projection limit, support for finite extension",
53  "-backward 4 Backward radon transform (default not)",
54  " ",
55  "Parameters:",
56  "-verbose 7 Verbosity of output",
57  "-datatype u Force writing of a new data type (default output floating point)",
58  "-theta 64 Number of angles (default 2*ncol)",
59  "-kernel 15,4 Kernel size and power (default 11,2)",
60  "-nopadding Turn off padding (default on)",
61  " ",
62  "Input:",
63  "-mask mask.map Binary mask for POCS filter",
64  " ",
65  NULL
66 };
67 
68 int
69 main (int argc, char **argv)
70 {
71  // Initialize variables
72  DataType newdatatype = Unknown_Type; // Conversion to new data type
73 
74  // Radon transformation types
75  int forward(0); // Forward radon transform
76  int backward(0); // Backward radon transform
77  int n_theta(0); // Number of angular samples per 2*PI
78  int nkernel(11); // Kernel width
79  int kernel_power(2); // Kernel power
80  int padd(1); // Padding??
81 
82  // POCS parameters: Both numbers of cycles must be specified to turn this on
83  int n_cyc_out(0); // cycles of swopping between r,phi and r,theta
84  int n_cyc_in(0); // pocs cycles within r,phi and r,theta planes
85  double rad_3D(0); // maximum radius in 3D
86  double rad_plane(0); // maximum radius on single axis proj
87  int support(0); // if (1) the condition of finite extension is imposed also in direct space. (use with care)
88  Bstring maskfile; // File name for mask file
89  Bimage *pmask = NULL; // Binary mask for POCS filter
90 
91  int optind;
92  Boption *option = get_option_list (use, argc, argv, optind);
93  Boption *curropt;
94  for (curropt = option; curropt; curropt = curropt->next)
95  {
96  if ( curropt->tag == "datatype" )
97  newdatatype = curropt->datatype();
98  if ( curropt->tag == "forward" )
99  {
100  if ( ( forward = curropt->value.integer() ) < 1)
101  cerr << "-forward: A type of forward transformation must be specified!" << endl;
102  else if (forward > 4)
103  forward = 4;
104  }
105  if ( curropt->tag == "pocs" )
106  if (sscanf (curropt->value.c_str(), "%d,%d,%lf,%lf,%d", &n_cyc_out, &n_cyc_in, &rad_3D, &rad_plane, &support) < 2)
107  cerr << "-pocs: At least the outer and inner cycles must be specified!" << endl;
108  if ( curropt->tag == "backward" )
109  {
110  if ( ( backward = curropt->value.integer() ) < 1)
111  cerr << "-backward: A type of backward transformation must be specified!" << endl;
112  else if (backward > 4)
113  backward = 4;
114  }
115  if ( curropt->tag == "theta" )
116  if ( ( n_theta = curropt->value.integer() ) < 1)
117  cerr << "-theta: The number of angles must be specified!" << endl;
118  if ( curropt->tag == "kernel" )
119  if ( curropt->values(nkernel, kernel_power) < 1)
120  cerr << "-kernel: At least the kernel size must be specified!" << endl;
121  if ( curropt->tag == "nopadding" )
122  padd = 0;
123  if ( curropt->tag == "mask" )
124  {
125  maskfile = curropt->filename();
126  if (maskfile.length())
127  {
128  pmask = read_img (maskfile, 1, -1);
129  pmask->change_type(Float);
130  }
131  }
132  }
133  option_kill (option);
134 
135  Bimage *p = read_img (argv[optind++], 1, -1);
136 
137  if(backward!=3)
138  {
141  }
142 
143  if (!p)
144  exit (-1);
145 
146  if (n_theta < 1)
147  n_theta = p->sizeX() * 2;
148 
149  if (rad_3D < 1)
150  rad_3D = p->sizeX() / 2 - 1;
151 
152  if (rad_plane < 1)
153  rad_plane = p->sizeX() / 2 - 1;
154 
155  Bimage *prad = NULL;
156 
157  if (forward)
158  {
159  prad = img_radon_transform (p, forward, nkernel, kernel_power, padd, n_theta);
160  delete p;
161  p = prad;
162  }
163 
164  if (n_cyc_out && n_cyc_in) {
165  if ( n_cyc_in > 1 && !pmask ) {
166  cout << "Error: To use POCS effectively, a mask file must be given!" << endl;
167  exit(-1);
168  }
169  img_radon_pocs_filter (p, n_cyc_out, n_cyc_in, rad_3D, rad_plane, support, pmask);
170  }
171 
172  if (backward)
173  {
174  prad = img_radon_inverse_transform (p, backward, nkernel, kernel_power, padd);
175  delete p;
176  p = prad;
177  }
178 
179  if (p && optind < argc)
180  {
181  p->change_type(newdatatype);
182  write_img (argv[optind], p, 0);
183  }
184 
185  delete p;
186  delete pmask;
187 
188  return 0;
189 }
Boption
A class for a command line input option.
Definition: options.h:26
Bstring
Definition: Bstring.h:29
img_radon_transform
Bimage * img_radon_transform(Bimage *p, int type, int nkernel, int kernel_power, int padd, int ntheta)
Calculates the Radon transform of an image.
Definition: img_radon.cpp:54
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
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::background
double background(long nn)
Definition: Bimage.h:549
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
img_radon_pocs_filter
int img_radon_pocs_filter(Bimage *p, int n_cyc_out, int n_cyc_in, double rad_3D, double rad_plane, int support, Bimage *pmask)
Filters a radon transform using the POCS method.
Definition: img_radon.cpp:221
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
img_radon.h
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
img_resize_to_next_power2
int img_resize_to_next_power2(Bimage *p, int fill_type, double fill)
Resizes without interpolation or rescaling to the next power of 2.
Definition: img_radon.cpp:459
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
main
int main(int argc, char **argv)
Definition: radon.cpp:69
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.
Boption::filename
Bstring filename()
Definition: options.h:85
img_radon_inverse_transform
Bimage * img_radon_inverse_transform(Bimage *p, int type, int nkernel, int kernel_power, int padd)
Calculates a 3D image from a Radon transform.
Definition: img_radon.cpp:141
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
verbose
int verbose
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
use
const char * use[]
Definition: radon.cpp:22