Bsoft  2.1.0
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->scale();
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  write_img(argv[optind], p, 0);
163  }
164 
165  delete p;
166 
167  if ( verbose & VERB_TIME )
168  timer_report(ti);
169 
170  bexit(0);
171 }
172 /*
173 Matrix3 img_align(Bimage* p, Bimage* pref, Bimage* pmask)
174 {
175  Matrix3 mat(1);
176  double accuracy(2/pref->sizeX());
177 
178  while ( dang >= accuracy ) {
179  ccbest = -1;
180  cout << "∆ang = " << dang*180.0/M_PI << tab << "res = " << hires << endl;
181  cout << "Shift\t\t\tAngle\tCC" << endl;
182  for ( angle = angmin; angle <= angmax; angle += dang ) {
183  mat = Matrix3(axis, angle);
184  pt = transform(pref->size(), vscale, ori, translate, mat, FILL_BACKGROUND);
185 // cc = pt->correlate(pref, 0, pref->sizeX()/2, pmask);
186  shift = pt->find_shift(pref, NULL, hires, 0, 10, 0, 1, cc);
187  if ( ccbest < cc ) {
188  ccbest = cc;
189  best_angle = angle;
190  best_shift = shift;
191 // if ( verbose & VERB_PROCESS )
192 // cout << "Better CC: " << cc << " (" << angle*180.0/M_PI << ")" << endl;
193  }
194  delete pt;
195  if ( verbose )
196  cout << shift << tab << angle*180.0/M_PI << tab << cc << endl;
197  }
198  dang *= 0.75;
199  angmin = best_angle - 2*dang;
200  angmax = best_angle + 2.1*dang;
201  hires = dang*pref->real_size()[0];
202  }
203 
204  return mat;
205 }
206 */
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: bscale.cpp:18
Bimage::scale_to_same_size
Bimage * scale_to_same_size(Bimage *pref)
Scales and image to the given reference.
Definition: Bimage_transform.cpp:735
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bsymmetry::point
int point
Definition: symmetry.h:39
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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:759
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::size
Vector3< long > size() const
Definition: Bimage.h:386
options.h
Header file for option handlers.
verbose
int verbose
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
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:331
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
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
Bstring::length
long length() const
Definition: Bstring.cpp:263
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
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
main
int main(int argc, char **argv)
Definition: bscale.cpp:45
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