| Bsoft 2.0.5 Bernard's software package

bscale.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 /* Usage assistance */
18 const char* use[] = {
19 " ",
20 "Usage: bscale [options] input.pif output.map",
21 "--------------------------------------------",
22 "Scales a map with respect to a reference.",
23 " ",
24 "Actions:",
25 "-bin 3 Bin the images before comparison.",
26 "-average 7 Averaging/smoothing filter: kernel size.",
27 "-symmetry C5 Find point group symmetry equivalent.",
28 "-fit Fit the map to the reference.",
29 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
30 " ",
31 "Parameters:",
32 "-verbose 7 Verbosity of output.",
33 "-datatype u Force writing of a new data type.",
34 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
35 "-sampling 1.5,1.5,1.5 Dimension scaling (default 1,1,1 A/pixel).",
36 "-range 0.95,1.08,0.01 Scaling range and step size (default 0.99,1.01,0.01).",
37 " ",
38 "Input:",
39 "-reference temp.map Reference map to scale to.",
40 "-mask mask.mrc Mask (must be the same size as the reference).",
41 " ",
42 NULL
43 };
44
45 int main(int argc, char **argv)
46 {
47 /* Initialize variables */
48 int bin(0); // Binning
49 Bsymmetry* sym = NULL; // Point group
50 int fit(0);
51 double nuavg(0), nustd(0); // Values for rescaling
52 DataType nudatatype(Unknown_Type); // Conversion to new type
53 Vector3<double> origin; // New image origin
54 int set_origin(0); // Flag to set origin
55 Vector3<double> sam; // Pixel size
56 long average_kernel(0); // Average filter kernel size
57 double smin(0.99), smax(1.01); // Scaling search range
58 double step(0); // Scaling search step
59 Bstring reffile; // Reference file name
60 Bstring maskfile; // Mask file name
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 == "bin" )
67 if ( ( bin = curropt->value.integer() ) < 1 )
68 cerr << "-bin: The kernel edge size must be specified!" << endl;
69 if ( curropt->tag == "symmetry" )
70 sym = curropt->symmetry();
71 if ( curropt->tag == "fit" ) fit = 1;
72 if ( curropt->tag == "rescale" ) {
73 if ( curropt->values(nuavg, nustd) < 2 )
74 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
75 if ( nustd <= 0 )
76 cerr << "-rescale: A positive standard deviation must be specified!" << endl;
77 }
78 if ( curropt->tag == "datatype" )
79 nudatatype = curropt->datatype();
80 if ( curropt->tag == "origin" ) {
81 if ( curropt->value[0] == 'c' ) {
82 set_origin = 2;
83 } else {
84 origin = curropt->origin();
85 set_origin = 1;
86 }
87 }
88 if ( curropt->tag == "sampling" )
89 sam = curropt->sampling();
90 if ( curropt->tag == "average" )
91 if ( ( average_kernel = curropt->value.integer() ) < 1 )
92 cerr << "-average: The kernel edge size must be specified!" << endl;
93 if ( curropt->tag == "range" )
94 if ( curropt->values(smin, smax, step) < 2 )
95 cerr << "-range: Minimum and maximum scales must be specified!" << endl;
96 if ( curropt->tag == "reference" )
97 reffile = curropt->filename();
98 if ( curropt->tag == "mask" )
99 maskfile = curropt->filename();
100 }
101 option_kill(option);
102
103 double ti = timer_start();
104
105 // Read the input file if a file name is given
106 Bimage* p = read_img(argv[optind++], 1, -1);
107 Bimage* pref = NULL;
108 Bimage* pmask = NULL;
109 Bimage* pscale = NULL;
110 Matrix3 mat(1);
111
112 p->change_type(Float);
113 if ( sam.volume() ) p->sampling(sam);
114
115 if ( set_origin > 1 ) p->origin(p->size()/2);
116 else if ( set_origin ) p->origin(origin);
117
118 if ( bin > 1 ) {
119 pscale = p->bin_around_origin(bin);
120 delete p;
121 p = pscale;
122 pscale = NULL;
123 }
124
125 if ( average_kernel ) p->filter_average(average_kernel);
126
127 if ( reffile.length() ) {
128 pref = read_img(reffile, 1, -1);
129
130 if ( average_kernel ) pref->filter_average(average_kernel);
131
132 if ( maskfile.length() )
133 pmask = read_img(maskfile, 1, -1);
134
135 if ( sym && sym->point > 101 && sym->point != 432 ) {
136 mat = p->symmetry_equivalent(pref, pmask, sym);
137 if ( sym->point < 200 ) p->rotate(-p->image->origin()+p->size()/2, mat);
138 else p->rotate(mat);
139 // } else {
140 // mat = img_align(p, pref, pmask);
141 // p->rotate(-p->image->origin()+p->size()/2, mat);
142 }
143
144 if ( step ) pscale = p->scale_to_reference(pref, pmask, smin, smax, step);
145 else if ( fit ) pscale = p->scale_to_reference(pref, pmask);
146 else pscale = p->scale_to_same_size(pref);
147 }
148
149 if ( pscale ) {
150 delete p;
151 p = pscale;
152 }
153
154 delete pref;
155 delete pmask;
156
157 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
158
159 // Write an output file if a file name is given
160 if ( argc > optind ) {
161 p->change_type(nudatatype);
162 p->label(get_cmd_line(argc, argv));
163 write_img(argv[optind], p, 0);
164 }
165
166 delete p;
167
168 if ( verbose & VERB_TIME )
169 timer_report(ti);
170
171 bexit(0);
172 }
173 /*
174 Matrix3 img_align(Bimage* p, Bimage* pref, Bimage* pmask)
175 {
176 Matrix3 mat(1);
177 double accuracy(2/pref->sizeX());
178
179 while ( dang >= accuracy ) {
180 ccbest = -1;
181 cout << "∆ang = " << dang*180.0/M_PI << tab << "res = " << hires << endl;
182 cout << "Shift\t\t\tAngle\tCC" << endl;
183 for ( angle = angmin; angle <= angmax; angle += dang ) {
184 mat = Matrix3(axis, angle);
185 pt = transform(pref->size(), vscale, ori, translate, mat, FILL_BACKGROUND);
186 // cc = pt->correlate(pref, 0, pref->sizeX()/2, pmask);
187 shift = pt->find_shift(pref, NULL, hires, 0, 10, 0, 1, cc);
188 if ( ccbest < cc ) {
189 ccbest = cc;
190 best_angle = angle;
191 best_shift = shift;
192 // if ( verbose & VERB_PROCESS )
193 // cout << "Better CC: " << cc << " (" << angle*180.0/M_PI << ")" << endl;
194 }
195 delete pt;
196 if ( verbose )
197 cout << shift << tab << angle*180.0/M_PI << tab << cc << endl;
198 }
199 dang *= 0.75;
200 angmin = best_angle - 2*dang;
201 angmax = best_angle + 2.1*dang;
202 hires = dang*pref->real_size()[0];
203 }
204
205 return mat;
206 }
207 */
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
use
const char * use[]
Definition: bscale.cpp:18
main
int main(int argc, char **argv)
Definition: bscale.cpp:45
Bimage::symmetry_equivalent
Matrix3 symmetry_equivalent(Bimage *ptemp, Bimage *pmask, Bsymmetry *sym)
Finds the symmetry equivalent orientation of a particle with respect to a template.
Definition: Bimage_symmetry.cpp:654
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::scale_to_same_size
Bimage * scale_to_same_size(Bimage *pref)
Scales and image to the given reference.
Definition: Bimage_transform.cpp:740
Matrix3
Definition: Matrix3.h:17
Bstring::length
long length() const
Definition: Bstring.cpp:263
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
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
verbose
int verbose
Bsymmetry::point
int point
Definition: symmetry.h:39
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:208
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
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:252
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
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bsymmetry
Definition: symmetry.h:38
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::scale_to_reference
Bimage * scale_to_reference(Bimage *pref, Bimage *pmask, double scalemin, double scalemax, double step)
Scales and image to the given reference, searching for the correct scale.
Definition: Bimage_transform.cpp:764
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
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:253
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bimage::bin_around_origin
Bimage * bin_around_origin(int bin)
Bins by an integer size making sure the origin falls on a binned voxel, and returns a new image.
Definition: Bimage_bin.cpp:327
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Vector3::volume
double volume()
Definition: Vector3.h:235
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
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563