Bsoft  2.1.0
Bernard's software package
bmatch.cpp
Go to the documentation of this file.
1 
11 #include "rwimg.h"
12 #include "rwmodel.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 Bmodel* img_match(Bimage* p, Bimage* pref, double res_hi, double res_lo,
21  double angle_inc, double threshold, long exclusion, double mask_rad);
22 Bmodel* img_match_one(Bimage* p, long img_num, Bimage* pref, double res_hi, double res_lo,
23  double angle_inc, double threshold, long exclusion, double mask_rad, fft_plan planf, fft_plan planb);
24 
25 /* Usage assistance */
26 const char* use[] = {
27 " ",
28 "Usage: bmatch [options] img.mrc cc.map",
29 "--------------------------------------",
30 "Searching an image with a set of 2D templates/references",
31 "Orientations are taken from the input projections.",
32 " ",
33 //"Actions:",
34 //"-variance Calculate shell variances of input projections.",
35 " ",
36 "Parameters:",
37 "-verbose 7 Verbosity of output.",
38 "-datatype u Force writing of a new data type.",
39 "-sampling 2,3.5,1 Sampling before rescaling (angstrom/voxel, a single value sets all three).",
40 "-resolution 25,200 Resolution limits (angstrom).",
41 "-angle 2.5 Angular increment (default 3 degrees).",
42 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
43 "-threshold 0.24 Threshold to find correlation peaks.",
44 "-exclusion 150 Minimum distance between correlation peaks.",
45 "-radius 50 Mask radius (slightly bigger than particle).",
46 " ",
47 "Input:",
48 "-reference file.pif Input reference projections file.",
49 " ",
50 "Output:",
51 "-output newmod.star New model file.",
52 " ",
53 NULL
54 };
55 
56 int main(int argc, char **argv)
57 {
58  // Initialize variables
59  DataType nudatatype(Unknown_Type); // Conversion to new type
60 
61  Vector3<double> origin; // Origin
62  int set_origin(0); // Flag for setting the origin
63  Vector3<double> sampling; // Sampling before rescaling
64  double res_hi(0); // High resolution limit
65  double res_lo(0); // Low resolution limit
66  double angle_inc(M_PI/60); // Angular increment
67  double threshold(0.1); // Threshold for peak detection
68  long exclusion(10); // Peak detection exclusion size
69  double mask_rad(70); // Particle masking radius
70  Bstring ref_filename; // Input projections file name
71  Bstring outfile; // Output parameter file name
72 
73  int optind;
74  Boption* option = get_option_list(use, argc, argv, optind);
75  Boption* curropt;
76  for ( curropt = option; curropt; curropt = curropt->next ) {
77  if ( curropt->tag == "datatype" )
78  nudatatype = curropt->datatype();
79  if ( curropt->tag == "origin" ) {
80  if ( curropt->value[0] == 'c' ) {
81  set_origin = 2;
82  } else {
83  origin = curropt->origin();
84  set_origin = 1;
85  }
86  }
87  if ( curropt->tag == "resolution" )
88  if ( curropt->values(res_hi, res_lo) < 1 )
89  cerr << "-resolution: A resolution limit must be specified!" << endl;
90  if ( curropt->tag == "angle" )
91  if ( ( angle_inc = curropt->value.real() ) < 0.1 )
92  cerr << "-angle: An angle must be specified!" << endl;
93  if ( curropt->tag == "sampling" )
94  sampling = curropt->scale();
95  if ( curropt->tag == "threshold" )
96  if ( ( threshold = curropt->value.real() ) < 0.001 )
97  cerr << "-threshold: A resolution limit must be specified!" << endl;
98  if ( curropt->tag == "exclusion" )
99  if ( ( exclusion = curropt->value.integer() ) < 2 )
100  cerr << "-exclusion: A minimum distance between peaks must be specified!" << endl;
101  if ( curropt->tag == "radius" )
102  if ( ( mask_rad = curropt->value.real() ) < 0.001 )
103  cerr << "-radius: A mask radius in pixels must be specified!" << endl;
104  if ( curropt->tag == "reference" )
105  ref_filename = curropt->filename();
106  if ( curropt->tag == "output" )
107  outfile = curropt->filename();
108  }
109  option_kill(option);
110 
111  double ti = timer_start();
112 
113  Bimage* p = NULL;
114  Bimage* pref = NULL;
115 
116  if ( ( p = read_img(argv[optind++], 1, -1) ) == NULL ) {
117  cerr << "Error: File not read!" << endl;
118  bexit(-1);
119  }
120 
121  if ( set_origin ) {
122  if ( set_origin == 2 ) p->origin(p->size()/2);
123  else p->origin(origin);
124  }
125 
126  if ( ref_filename.length() ) {
127  if ( ( pref = read_img(ref_filename, 1, -1) ) == NULL ) {
128  cerr << "Error: File not read!" << endl;
129  bexit(-1);
130  }
131  }
132 
133  if ( set_origin ) {
134  if ( set_origin == 2 ) pref->origin(p->size()/2);
135  else pref->origin(origin);
136  }
137 
138  if ( nudatatype == Unknown_Type )
139  nudatatype = p->data_type();
140 
141  if ( sampling.volume() > 0 ) {
142  p->sampling(sampling);
143  if ( pref ) pref->sampling(sampling);
144  }
145 
146  Bmodel* model = NULL;
147  if ( pref )
148  model = img_match(p, pref, res_hi, res_lo, angle_inc, threshold, exclusion, mask_rad);
149 
150  // Write an output parameter format file if a name is given
151  if ( outfile.length() && model ) {
152  write_model(outfile, model);
153  }
154 
155  model_kill(model);
156 
157  delete p;
158  delete pref;
159 
160  if ( verbose & VERB_TIME )
161  timer_report(ti);
162 
163  return 0;
164 }
165 
166 Bmodel* img_match(Bimage* p, Bimage* pref, double res_hi, double res_lo,
167  double angle_inc, double threshold, long exclusion, double mask_rad)
168 {
169  Bmodel* modlist = NULL;
170  Bmodel* model;
171 
172  fft_plan planf = fft_setup_plan(p->size(), FFTW_FORWARD, 1);
173  fft_plan planb = fft_setup_plan(p->size(), FFTW_BACKWARD, 1);
174 
175  for ( long n = 0; n < p->images(); ++n ) {
176  model = img_match_one(p, n, pref, res_hi, res_lo,
177  angle_inc, threshold, exclusion, mask_rad, planf, planb);
178  model_add(&modlist, model);
179  }
180 
183 
184  return modlist;
185 }
186 
188 {
189  Bimage* pref1 = pref->extract(nn);
190 
191  if ( pref1->size() != size ) {
192  pref1->calculate_background();
193  pref1->pad(size, FILL_BACKGROUND);
194  }
195 
196 // write_img("t.pif", pref1, 0);
197 
198  return pref1;
199 }
200 
201 
202 /*
203 */
204 Bmodel* img_match_one(Bimage* p, long img_num, Bimage* pref, double res_hi, double res_lo,
205  double angle_inc, double threshold, long exclusion, double mask_rad, fft_plan planf, fft_plan planb)
206 {
207  if ( angle_inc < M_PI/180 ) angle_inc = M_PI/180;
208 
209  long i, j, k, nn;
210  double angle, amin(0), amax(TWOPI), ccmax(0), ccmax1;
211  Vector3<long> coor, rect(2*mask_rad, 2*mask_rad, 1);
212  Vector3<double> start, nucoor;
213  View view;
214 
215  Bstring id(img_num, "%d");
216  Bmodel* model = model_init(id);
217  Bcomponent* comp = NULL;
218  Bcomptype* ct = NULL;
219 
220  model->fmap = p->file_name();
221  model->img_num = img_num;
222 
223  Bimage* p1 = p->extract(img_num);
224 
225  p1->calculate_background();
226 
227  Bimage* pcc = new Bimage(Float, TSimple, p1->size(), 1);
228  Bimage* pang = pcc->copy();
229  Bimage* pnn = new Bimage(Integer, TSimple, p1->size(), 1);
230  pnn->fill(-1);
231 
232  if ( verbose ) {
233  cout << "Global search for image " << img_num << ":" << endl;
234  cout << "Proj\tvx\tvy\tvz\tva\tccmax" << setprecision(4) << endl;
235  }
236  for ( nn=0; nn<pref->images(); ++nn ) {
237  id = Bstring(nn+1, "%d");
239  Bimage* pref1 = img_reference_prepare(pref, nn, p1->size());
240  ccmax1 = 0;
241  for ( angle = amin; angle <= amax; angle += angle_inc ) {
242  Bimage* prot = pref1->rotate(pref1->size(), angle);
243  Bimage* pcc1 = p1->cross_correlate(prot, res_hi, res_lo, planf, planb);
244  if ( !pcc1 ) bexit(-1);
245  for ( j=0; j<pcc1->image_size(); ++j ) if ( (*pcc)[j] < (*pcc1)[j] ) {
246  pcc->set(j, (*pcc1)[j]);
247  pang->set(j, angle);
248  pnn->set(j, nn);
249  }
250  if ( ccmax1 < pcc1->maximum() ) ccmax1 = pcc1->maximum();
251  delete pcc1;
252  delete prot;
253  }
254  if ( verbose )
255  cout << nn << tab << pref->image[nn].view() << tab << ccmax1 << endl;
256  if ( ccmax < ccmax1 ) ccmax = ccmax1;
257  delete pref1;
258  }
259  if ( verbose )
260  cout << endl;
261 
262  if ( threshold >= ccmax ) {
263  threshold = 0.95*ccmax;
264  if ( verbose )
265  cout << "Adjusting threshold to " << threshold << endl;
266  }
267 
268  Bimage* ppeak = pcc->region_peaks(exclusion, threshold, 0, 1);
269  write_img("cc.pif", pcc, 0);
270  write_img("nn.pif", pnn, 0);
271 
272  pcc->clear();
273 
274  for ( i=j=0; i<ppeak->image_size(); ++i ) if ( (*ppeak)[i] ) j++;
275 
276  if ( verbose )
277  cout << "Number of peaks: " << j << endl << endl;
278 
279  threshold *= p1->sizeX()*1.0/pref->sizeX();
280 
281  if ( verbose ) {
282  cout << "Adjusting threshold to " << threshold << endl;
283  cout << "Local search for image " << img_num << ":" << endl;
284  cout << "x\ty\tz\tvx\tvy\tvz\tva\tccmax" << setprecision(4) << endl;
285  }
286  rect = Vector3<long>(2*mask_rad, 2*mask_rad, 1);
287  angle_inc /= 10;
288  for ( i=j=0; i<ppeak->image_size(); ++i ) if ( (*ppeak)[i] ) {
289  nn = (*pnn)[i];
290  for ( k=0, ct = model->type; k<nn; ++k ) ct = ct->next;
291  Bimage* pref1 = pref->extract(nn);
292  pref1->calculate_background();
293  coor = vector3_set_PBC(pref1->image->origin() + ppeak->coordinates(i), p->size());
294  start[0] = coor[0] - mask_rad;
295  start[1] = coor[1] - mask_rad;
296  Bimage* pe = p1->copy();
297  pe->calculate_background();
298  pe->origin(pref1->image->origin());
299  pe->edge(0, 1, rect, start, 5, FILL_BACKGROUND);
300 // Bstring fn(j+1, "t%d.pif");
301 // write_img(fn, pe, 0);
302  ccmax1 = 0;
303  amin = (*pang)[i] - 10*angle_inc;
304  amax = (*pang)[i] + 10*angle_inc;
305  coor = vector3_set_PBC(ppeak->coordinates(i), p->size());
306  nucoor = coor;
307  for ( angle = amin; angle <= amax; angle += angle_inc ) {
308  Bimage* prot = pref1->rotate(pref1->size(), angle);
309  Bimage* pcc1 = pe->cross_correlate(prot, res_hi, res_lo);
310  if ( !pcc1 ) bexit(-1);
311  pcc1->origin(coor);
312  pcc1->find_peak(5);
313  if ( (*pcc)[i] < pcc1->image->FOM() ) {
314  pcc->set(i, pcc1->image->FOM());
315  pang->set(i, angle);
316  nucoor = pcc1->image->origin();
317  }
318  if ( ccmax1 < pcc1->image->FOM() ) ccmax1 = pcc1->image->FOM();
319  delete pcc1;
320  delete prot;
321  }
322  delete pe;
323  delete pref1;
324  view = pref->image[nn].view();
325  view.angle(view.angle() - (*pang)[i]);
326  if ( p->sizeX() > 1 && nucoor[0] >= p->sizeX()/2 ) nucoor[0] -= p->sizeX();
327  if ( p->sizeY() > 1 && nucoor[1] >= p->sizeY()/2 ) nucoor[1] -= p->sizeY();
328  if ( p->sizeZ() > 1 && nucoor[2] >= p->sizeZ()/2 ) nucoor[2] -= p->sizeZ();
329  if ( verbose )
330  cout << nucoor << tab << view << tab << ccmax1 << endl;
331  if ( ccmax1 >= threshold ) {
332  comp = component_add(&model->comp, ++j);
333  comp->type = ct;
334  comp->loc = nucoor*p->sampling(0);
335  comp->view = view.backward();
336  comp->fom = ccmax1;
337  comp->radius = 10*p->sampling(0)[0];
338  }
339  }
340  if ( verbose )
341  cout << "Number of peaks: " << j << endl << endl;
342 
343  delete ppeak;
344  delete pcc;
345  delete pang;
346  delete pnn;
347  delete p1;
348 
349  return model;
350 }
351 
352 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bcomponent
Definition: rwmodel.h:51
Boption::scale
Vector3< double > scale()
Definition: options.h:190
rwmodel.h
Header file for reading and writing atomic model files.
Bimage::cross_correlate
Bimage * cross_correlate(Bimage *p, double hires, double lores, fft_plan planf, fft_plan planb)
Calculates a cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:363
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Boption::datatype
DataType datatype()
Definition: options.h:93
Bcomptype
Definition: rwmodel.h:25
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::find_peak
int find_peak(double radius=1e30, double sigma=0)
Finds the peak in an image to the nearest voxel.
Definition: Bimage_correlate.cpp:1340
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
Bstring::real
double real()
Definition: Bstring.cpp:553
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
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
planf
fft_plan planf
Definition: bico.cpp:24
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:147
use
const char * use[]
Definition: bmatch.cpp:26
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
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
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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
View::backward
View backward()
Definition: View.h:118
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::image_size
long image_size()
Definition: Bimage.h:290
img_match_one
Bmodel * img_match_one(Bimage *p, long img_num, Bimage *pref, double res_hi, double res_lo, double angle_inc, double threshold, long exclusion, double mask_rad, fft_plan planf, fft_plan planb)
Definition: bmatch.cpp:204
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
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
img_match
Bmodel * img_match(Bimage *p, Bimage *pref, double res_hi, double res_lo, double angle_inc, double threshold, long exclusion, double mask_rad)
Definition: bmatch.cpp:166
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
Bimage::clear
void clear()
Definition: Bimage.h:705
Bmodel
Definition: rwmodel.h:119
vector3_set_PBC
Vector3< T1 > vector3_set_PBC(Vector3< T1 > coord, Vector3< T2 > box)
Definition: Vector3.h:684
Bimage::region_peaks
Bimage * region_peaks(long kernel_size, double threshold, int flood=0, int wrap=0)
Generates a segmented image from peaks above a threshold value.
Definition: Bimage_segment.cpp:457
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
verbose
int verbose
Integer
@ Integer
Definition: utilities.h:158
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
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
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
Boption::filename
Bstring filename()
Definition: options.h:85
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:181
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
main
int main(int argc, char **argv)
Definition: bmatch.cpp:56
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:414
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::pad
int pad(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:347
img_reference_prepare
Bimage * img_reference_prepare(Bimage *pref, long nn, Vector3< long > size)
Definition: bmatch.cpp:187
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
Bcomptype::next
Bcomptype * next
Definition: rwmodel.h:26
Bcomponent::radius
float radius
Definition: rwmodel.h:62
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::edge
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Definition: Bimage_edit.cpp:321
Float
@ Float
Definition: utilities.h:161
View::angle
double angle()
Definition: View.h:98
TWOPI
#define TWOPI
Definition: utilities.h:72