| Bsoft 2.0.5 Bernard's software package

bradial.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "ps_plot.h"
11 #include "Matrix.h"
12 #include "utilities.h"
13 #include "options.h"
14 #include "timer.h"
15
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: bradial [options] input.img output.img",
23 "---------------------------------------------",
24 "Calculates radial profiles of 2D and 3D images.",
25 "The origin within the image will be used by default to calculate a radial profile.",
26 "To use the radial fit, the sampling (voxel sizes) for the two images",
27 " must be within 20 % correct. Successive calibrations with the same input",
28 " will not necessarily give the exact same factors on output because ",
29 " the simplex algorithm with random initial parameters is used.",
30 " ",
31 "Actions:",
32 "-subtractbackground Calculate and subtract the background before calculating radial profiles.",
33 "-radial Radial average profile.",
34 "-elliptical 1.2,20 Elliptical average profile with given ellipticity and angle.",
35 //"-volume 0.66 Radial volume profile above a threshold.",
36 "-Radialps 15.5,1 Radial power spectrum up to a given resolution (in angstrom).",
37 " and a flag to average multiple power spectra.",
38 "-polar 156,24,35 Polar image: annuli, phi steps, theta steps.",
39 "-cylindrical 124,36 Cylindrical image: annuli, phi steps.",
40 "-shells Calculate radial shells projected along the z-axis.",
41 "-expand 150,150,1 Size to generate a 2D or 3D image from a radial profile (use with -origin).",
42 "-average Average multiple images before writing an output image.",
43 "-logarithm Calculate logarithm of radial power spectrum.",
44 "-reslice -z+xy Reslice (switch axes) after all other operations.",
45 "-coverage 1.4 Shell coverage for the given threshold.",
46 " ",
47 "Parameters:",
48 "-verbose 7 Verbosity of output.",
49 "-datatype u Force writing of a new data type.",
50 "-sampling 1.5,1.5,1.5 Sampling (default from image; a single value can be given).",
51 "-origin 0,-10,30 Set the origin for radial profile (default image origin).",
52 "-minmax 4,123 Minimum and maximum radii for radial and volume profiles (angstrom).",
53 "-step 2.68 Step size for radial profiles (default pixel size in angstrom).",
54 "-wrap Flag to wrap around image boundaries (default not).",
55 " ",
56 "Input:",
57 "-fit file.img Map to fit radial profile of input image to.",
58 "-Mask mask.tif Mask to limit calculations.",
59 " ",
60 "Output:",
61 "-Postscript rad.ps Postscript output file name.",
62 " ",
63 NULL
64 };
65
66 int main(int argc, char **argv)
67 {
68 // Initialize variables
69 DataType nudatatype(Unknown_Type); // Conversion to new type
70 Vector3<double> sam;
71 Vector3<double> origin; // Origin
72 int set_origin(0); // Flag to set origin
73 int subtract_background(0); // Flag to subtract backgrounds
74 int setradial(0);
75 int avg_flag(0); // Flag for averaging radial power spectra
76 int log_flag(0); // Flag for logarithm of radial power spectra
77 Vector3<long> expand_size; // Size for expansion
78 double resolution(0); // Resolution limit for power spectrum
79 double minrad(0), maxrad(0); // Radii for radial profiles (angstrom)
80 double ellipticity(1); // Elliptical averaging parameters
81 double angle(0);
82 // double threshold(-1e37); // Threshold for volume profiles
83 int nannuli(-1), ntheta(-1), nphi(-1); // Polar dimensions
84 int set_shells(0); // Radial shells flag
85 int setreslice(0); // Reslice after all other operations
86 Bstring order("xyz");
87 int coverage(0); // Flag for shell coverage
88 double threshold(-1e37); // Threshold for shell coverage
89 double step(1); // Radial step size
90 int wrap(0); // Flag to wrap
91 Bstring mapfile; // Map to fit to
92 Bstring maskfile; // Mask file
93 Bstring outfile; // Output image file
94 Bstring psfile; // Postscript output file
95
96 int i, j, optind;
97 Boption* option = get_option_list(use, argc, argv, optind);
98 Boption* curropt;
99 for ( curropt = option; curropt; curropt = curropt->next ) {
100 if ( curropt->tag == "subtractbackground" )
101 subtract_background = 1;
102 if ( curropt->tag == "datatype" )
103 nudatatype = curropt->datatype();
104 if ( curropt->tag == "origin" ) {
105 if ( curropt->value[0] == 'c' ) {
106 set_origin = 2;
107 } else {
108 origin = curropt->origin();
109 set_origin = 1;
110 }
111 }
112 if ( curropt->tag == "sampling" )
113 sam = curropt->sampling();
114 if ( curropt->tag == "polar" )
115 if ( curropt->values(nannuli, nphi, ntheta) < 3 )
116 cerr << "-polar: All three dimensions must be specified!" << endl;
117 if ( curropt->tag == "cylindrical" )
118 if ( curropt->values(nannuli, nphi) < 2 )
119 cerr << "-cylindrical: Both dimensions must be specified!" << endl;
120 if ( curropt->tag == "shells" )
121 set_shells = 1;
122 if ( curropt->tag == "radial" ) setradial = 1;
123 if ( curropt->tag == "elliptical" ) {
124 if ( curropt->values(ellipticity, angle) < 2 )
125 cerr << "-elliptical: Both ellipticity and angle must be specified!" << endl;
126 else {
127 setradial = 1;
128 angle *= M_PI/180;
129 }
130 }
131 if ( curropt->tag == "volume" ) {
132 if ( ( threshold = curropt->value.real() ) < -1e30 )
133 cerr << "-volume: A threshold must be specified!" << endl;
134 else
135 setradial = 1;
136 }
137 if ( curropt->tag == "Radialps" )
138 if ( curropt->values(resolution, avg_flag) < 1 )
139 cerr << "-Radialps: A resolution limit must be specified!" << endl;
140 if ( curropt->tag == "expand" )
141 expand_size = curropt->size();
142 if ( curropt->tag == "average" )
143 avg_flag = 1;
144 if ( curropt->tag == "logarithm" )
145 log_flag = 1;
146 if ( curropt->tag == "reslice" ) {
147 if ( curropt->value.length() < 3 )
148 cerr << "-reslice: The order of x, y and z must be specified!" << endl;
149 else {
150 order = curropt->value;
151 setreslice = 1;
152 }
153 }
154 if ( curropt->tag == "coverage" ) {
155 coverage = 1;
156 threshold = curropt->value.real();
157 }
158 if ( curropt->tag == "fit" ) {
159 mapfile = curropt->filename();
160 if ( mapfile.length() ) setradial = 1;
161 }
162 if ( curropt->tag == "minmax" )
163 if ( curropt->values(minrad, maxrad) < 1 )
164 cerr << "-minmax: At least a minimum must be specified!" << endl;
165 if ( curropt->tag == "step" )
166 if ( ( step = curropt->value.real() ) < 0.1 )
167 cerr << "-step: A step size must be specified!" << endl;
168 if ( curropt->tag == "wrap" )
169 wrap = 1;
170 if ( curropt->tag == "Mask" )
171 maskfile = curropt->filename();
172 if ( curropt->tag == "Postscript" )
173 psfile = curropt->filename();
174 }
175 option_kill(option);
176
177 double ti = timer_start();
178
179 int dataflag(0);
180 if ( setradial || resolution || optind < argc - 1 ) dataflag = 1;
181 Bimage* p = read_img(argv[optind++], dataflag, -1);
182 if ( p == NULL ) {
183 cerr << "Error: No input file read!" << endl;
184 bexit(-1);
185 }
186 if ( !p->data_pointer() ) {
187 delete p;
188 bexit(0);
189 }
190
191 // Get the output image file name
192 if ( optind < argc ) outfile = argv[optind];
193
194 // if ( nudatatype == Unknown_Type )
195 // nudatatype = p->data_type(); // Preserve the old type
196
197 if ( subtract_background ) {
198 if ( p->background(long(0)) == 0 ) p->calculate_background();
199 p->subtract_background();
200 }
201
202 long nx, n, k;
203 double f;
204 double* fit_result = NULL;
205 Vector3<double> scale(1,1,1);
206 Vector3<double> translate;
207 Matrix3 mat(1);
208 Bimage* pmap = NULL;
209 Bimage* prad = NULL;
210 Bimage* pmaprad = NULL;
211 Bimage* pmask = NULL;
212 Bstring title;
213 Bplot* plot = NULL;
214
215 if ( mapfile.length() )
216 pmap = read_img(mapfile, 1, -1);
217
218 if ( maskfile.length() )
219 pmask = read_img(maskfile, 1, -1);
220
221 // Sampling is always positive
222 if ( sam.volume() > 0 ) p->sampling(sam);
223
224 if ( set_origin ) {
225 if ( set_origin == 2 ) p->origin(p->size()/2);
226 else p->origin(origin);
227 } else origin = p->image->origin();
228
229 long minpix(minrad/(step*p->sampling()[0]));
230 long maxpix(maxrad/(step*p->sampling()[0]));
231
232 if ( setradial ) {
233 prad = p;
234 if ( p->sizeY() > 1 ) {
235 prad = p->radial(minpix, maxpix, step, ellipticity, angle, pmask, wrap);
236 } else
237 p->change_type(Float);
238
239 nx = prad->sizeX();
240 if ( pmap ) {
241 n = 3;
242 title = "Radial fit";
243 } else {
244 n = prad->images() + 1;
245 title = "Radial plot";
246 }
247
248 // cout << "Radial size = " << nx << endl;
249
250 plot = new Bplot(1, nx, n);
251 plot->title(title);
252 plot->page(0).title(title);
253 plot->page(0).columns(n);
254 for ( i=0; i<n; i++ ) plot->page(0).column(i).number(i);
255 plot->page(0).column(0).label("Distance(A)");
256 plot->page(0).column(0).axis(1);
257 for ( i=1; i<n; i++ ) {
258 plot->page(0).column(i).type(2);
259 plot->page(0).column(i).label("Average");
260 plot->page(0).column(i).axis(3);
261 }
262 if ( pmap ) {
263 plot->page(0).column(1).color(1,0,0);
264 plot->page(0).column(2).color(0,0,1);
265 }
266 plot->page(0).axis(1).min(minrad);
267 plot->page(0).axis(1).max(maxrad);
268 plot->page(0).axis(3).min(prad->minimum());
269 plot->page(0).axis(3).max(prad->maximum());
270
271 if ( pmap ) {
272 pmaprad = pmap;
273 if ( pmap->sizeY() > 1 ) {
274 pmaprad = pmap->radial(minpix, maxpix, step, ellipticity, angle, pmask, wrap);
275 } else
276 p->change_type(Float);
277 fit_result = prad->radial_fit(pmaprad);
278 if ( plot ) {
279 plot->page(0).axis(3).min(pmaprad->minimum());
280 plot->page(0).axis(3).max(pmaprad->maximum());
281 for ( i=0; i<nx; i++ ) (*plot)[i] = prad->sampling()[0]*i;
282 for ( i=0, k=nx; i<nx; i++, k++ ) (*plot)[k] = (*pmaprad)[i];
283 for ( i=0; i<nx; i++, k++ ) {
284 f = i/fit_result[0];
285 j = (int) f;
286 f -= j;
287 if ( j >= 0 && j < nx-1 )
288 (*plot)[k] = fit_result[1]*((1-f)*(*prad)[j] + f*(*prad)[j+1]) + fit_result[2];
289 }
290 }
291 scale[0] = scale[1] = scale[2] = fit_result[0];
292 delete[] fit_result;
293 if ( verbose & VERB_DEBUG )
294 cout << "DEBUG bradial: Scale = " << scale[0] << endl;
295 if ( outfile.length() > 0 ) {
296 translate = pmap->image->origin() - origin;
297 p->transform(scale, origin, translate, mat, FILL_BACKGROUND, 0);
298 p->sampling(pmap->sampling());
299 }
300 } else {
301 if ( verbose & VERB_PROCESS ) {
302 cout << "Radial averages:" << endl;
303 cout << "Radius";
304 for ( j=0; j<prad->images(); j++ ) cout << tab << j+1;
305 cout << endl;
306 }
307 cout << "Radial size = " << nx << endl;
308 for ( i=0; i<nx; i++ ) {
309 if ( prad->sampling()[0]*i >= minrad ) {
310 cout << prad->sampling()[0]*i;
311 for ( j=0; j<prad->images(); j++ ) cout << tab << (*prad)[nx*j+i];
312 cout << endl;
313 }
314 }
315 if ( plot ) {
316 for ( i=0; i<nx; i++ ) (*plot)[i] = prad->sampling()[0]*i;
317 for ( i=0, k=nx; i<nx*prad->images(); i++, k++ ) (*plot)[k] = (*prad)[i];
318 }
319 delete p;
320 p = prad; // Assign for output
321 prad = NULL;
322 }
323 } else if ( resolution > 0 ) {
324 if ( p->compound_type() != TComplex ) p->fft();
325 prad = p->fspace_radial_power(resolution);
326 delete p;
327 p = prad; // Assign for output
328 prad = NULL;
329 if ( avg_flag ) p->average_images();
330 if ( log_flag ) p->logarithm();
331 if ( psfile.length() ) {
332 title = "Radial power spectrum";
333 n = p->images() + 1;
334 nx = p->sizeX();
335 plot = new Bplot(1, nx, n);
336 plot->title(title);
337 plot->page(0).title(title);
338 plot->page(0).columns(n);
339 for ( i=0; i<n; i++ ) plot->page(0).column(i).number(i);
340 plot->page(0).column(0).label("s(1/A)");
341 plot->page(0).column(0).axis(1);
342 for ( i=1; i<n; i++ ) {
343 plot->page(0).column(i).type(2);
344 if ( log_flag )
345 plot->page(0).column(i).label("log Power");
346 else
347 plot->page(0).column(i).label("Power");
348 plot->page(0).column(i).axis(3);
349 }
350 if ( log_flag ) {
351 plot->page(0).axis(3).flags(2);
352 } else {
353 plot->page(0).axis(3).min(0);
354 plot->page(0).axis(3).max(p->maximum());
355 }
356 for ( i=0; i<nx; i++ ) (*plot)[i] = p->sampling()[0]*i;
357 for ( i=0, k=nx; i<nx*p->images(); i++, k++ ) (*plot)[k] = (*p)[i];
358 }
359 } else if ( expand_size.volume() > 0 ) {
360 pmap = p->radial_to_full(expand_size, origin);
361 delete p;
362 p = pmap;
363 pmap = NULL;
364 } else if ( nannuli > -1 ) {
365 if ( ntheta > 0 && p->sizeZ() > 1 )
366 pmap = p->cartesian_to_spherical(nannuli, nphi, ntheta);
367 else
368 pmap = p->cartesian_to_cylindrical(nannuli, nphi);
369 delete p;
370 p = pmap;
371 pmap = NULL;
372 } else if ( set_shells ) {
373 p->radial_shells();
374 } else if ( coverage ) {
375 p->radial_coverage(threshold, step);
376 }
377
378 if ( psfile.length() && plot )
379 ps_plot(psfile, plot);
380
381 if ( avg_flag ) p->average_images();
382
383 p->label(get_cmd_line(argc, argv));
384
385 if ( outfile.length() ) {
386 if ( setreslice ) p->reslice(order);
387 p->change_type(nudatatype);
388 write_img(outfile, p, 0);
389 }
390
391 delete p;
392 order = 0;
393 if ( pmap ) delete pmap;
394 if ( prad ) delete prad;
395 if ( pmaprad ) delete pmaprad;
396 if ( pmask ) delete pmask;
397 if ( plot ) delete plot;
398
399 if ( verbose & VERB_TIME )
400 timer_report(ti);
401
402 bexit(0);
403 }
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
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Baxis::min
double min()
Definition: Bplot.h:30
Bplot::title
Bstring & title()
Definition: Bplot.h:189
Bimage::fft
int fft()
Definition: Bimage.h:822
Bimage::radial_to_full
Bimage * radial_to_full(Vector3< long > nusize, Vector3< double > origin)
Generates a full 2D or 3D image from a radial profile in a 1D image.
Definition: Bimage_polar.cpp:338
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
Matrix3
Definition: Matrix3.h:17
Bstring::length
long length() const
Definition: Bstring.cpp:263
Matrix.h
Generalized matrix class.
Bimage::logarithm
void logarithm()
Calculates the logarithm of the image data.
Definition: Bimage_rescale.cpp:728
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bcolumn::color
double * color()
Definition: Bplot.h:76
TComplex
Definition: Bimage.h:59
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
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
Bimage::average_images
void average_images()
Definition: Bimage.h:716
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
Bimage::subtract_background
int subtract_background()
Subtracts the background for each sub-image.
Definition: Bimage_background.cpp:337
timer.h
Utilities for timing functions.
Baxis::max
double max()
Definition: Bplot.h:32
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::fspace_radial_power
Bimage * fspace_radial_power(double resolution)
Calculates the radial power spectrum from a Fourier transform.
Definition: Bimage_fspace.cpp:612
Bimage::maximum
double maximum()
Definition: Bimage.h:535
Bcolumn::number
long number()
Definition: Bplot.h:61
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::radial_fit
double * radial_fit(Bimage *pref)
Fits a radial profile to a reference radial profile.
Definition: Bimage_polar.cpp:254
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bcolumn::type
long type()
Definition: Bplot.h:66
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
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
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
verbose
int verbose
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:709
Bimage::cartesian_to_spherical
Bimage * cartesian_to_spherical(long nannuli, long nphi, long ntheta)
Calculates an image with spherical coordinates.
Definition: Bimage_polar.cpp:428
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Boption::next
Boption * next
Definition: options.h:28
Baxis::flags
long flags()
Definition: Bplot.h:38
use
const char * use[]
Definition: bradial.cpp:20
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:211
ps_plot.h
Header file for postscript output functions.
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
main
int main(int argc, char **argv)
Definition: bradial.cpp:66
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bimage::radial_shells
int radial_shells()
Calculates an image with slices representing radial shell projections.
Definition: Bimage_polar.cpp:783
Bimage::cartesian_to_cylindrical
Bimage * cartesian_to_cylindrical(long nannuli, long nphi, int flag=0)
Calculates an image with cylindrical coordinates.
Definition: Bimage_polar.cpp:519
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::radial_coverage
Bimage * radial_coverage(double threshold, double rad_step=1)
Calculates the coverage in each radial shell.
Definition: Bimage_polar.cpp:1079
Bimage::images
long images()
Definition: Bimage.h:491
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::radial
Bimage * radial(long minrad, long maxrad, double rad_step=1, int wrap=0)
Calculates the radial average of an image.
Definition: Bimage_polar.cpp:33
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::minimum
double minimum()
Definition: Bimage.h:534
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
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bplot
Definition: Bplot.h:146