Bsoft  2.1.0
Bernard's software package
mg_tomography.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_tomography.h"
11 #include "mg_select.h"
12 #include "mg_img_proc.h"
13 #include "rwimg.h"
14 #include "matrix_linear.h"
15 #include "simplex.h"
16 #include "linked_list.h"
17 #include "utilities.h"
18 #include "timer.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
23 
31 {
32  if ( !model )
33  return error_show("No markers found!", __FILE__, __LINE__);
34 
35  Bmarker* mark = NULL;
36  Bmarker* mark2 = NULL;
37 
38  for ( mark = model; mark; mark=mark->next ) {
39  mark2 = (Bmarker *) add_item((char **) &mark2, sizeof(Bmarker));
40  if ( !mg->mark ) mg->mark = mark2;
41  mark2->id = mark->id;
42  mark2->fom = 1;
43  mark2->sel = mark->sel;
44  }
45 
46  return 0;
47 }
48 
60 int mg_marker_update(Bmicrograph* mg, Bmarker* model, Vector3<double> oriref, int update_location)
61 {
62  if ( !model )
63  return error_show("No model markers found!", __FILE__, __LINE__);
64 
65  int upd;
66  Bmarker* mark = NULL;
67  Bmarker* modmark = NULL;
68 
69 // if ( !mg->mark ) mg_marker_init(mg, model);
70 
71  if ( verbose & VERB_DEBUG )
72  cout << "DEBUG mg_marker_update: mg=" << mg->id << endl;
73 
74  for ( modmark = model; modmark; modmark = modmark->next ) {
75  upd = update_location;
76  for ( mark = mg->mark; mark && mark->id != modmark->id; mark = mark->next ) ;
77  if ( !mark ) {
78  mark = (Bmarker *) add_item((char **) &mg->mark, sizeof(Bmarker));
79  mark->id = modmark->id;
80  mark->fom = 1;
81  mark->sel = modmark->sel;
82  upd = 1;
83  }
84  if ( upd )
85  mark->loc = mg_location_from_3D_model(modmark->loc, oriref, mg->matrix, mg->origin);
86  }
87 
88  return 0;
89 }
90 
92 {
93  Vector3<double> loc3D = loc - ori;
94  loc3D = mat * loc3D;
95  loc3D += ori3D;
96 // loc3D[2] = 0;
97 
98  return loc3D;
99 }
100 
112 {
113  if ( !model )
114  return error_show("No model markers found!", __FILE__, __LINE__);
115 
116  Bmarker* mark = NULL;
117  Bmarker* modmark = NULL;
118 
119  for ( mark = mg->mark; mark; mark = mark->next ) {
120  for ( modmark = model; modmark && mark->id != modmark->id; modmark = modmark->next ) ;
121  if ( !modmark ) {
122  modmark = (Bmarker *) add_item((char **) &model, sizeof(Bmarker));
123  modmark->id = mark->id;
124  modmark->fom = 1;
125  modmark->sel = mark->sel;
126  modmark->loc = mg_3D_location_from_marker(mark->loc, oriref, mg->matrix, mg->origin);
127  }
128  }
129 
130  return 0;
131 }
132 
141  Vector3<double> ori)
142 {
143  Matrix3 tmat = mat.transpose();
144  Vector3<double> mloc = tmat * loc3D;
145  mloc += ori;
146  mloc[2] = 0;
147 
148  return mloc;
149 }
150 
152  Vector3<double> ori, Vector3<double> scale)
153 {
154  Matrix3 tmat = mat.transpose();
155  Vector3<double> mloc = tmat * loc3D;
156  mloc /= scale;
157  mloc += ori;
158  mloc[2] = 0;
159 
160  return mloc;
161 }
162 
164  Matrix3 mat, Vector3<double> ori)
165 {
166  Vector3<double> mloc = loc3D - ori3D;
167  return mg_location_from_3D_model(mloc, mat, ori);
168 }
169 
171  Matrix3 mat, Vector3<double> ori, Vector3<double> scale)
172 {
173  Vector3<double> mloc = loc3D - ori3D;
174 // cout << loc3D << endl << ori3D << endl << ori << endl << scale << endl;
175  return mg_location_from_3D_model(mloc, mat, ori, scale);
176 }
177 
189 {
190  Bimage* p = new Bimage(Float, TSimple, size, 1);
191  Vector3<double> origin(p->size()/2);
192  double outrad = sqrt(2.0)*radius;
193 
194  p->sphere(origin, outrad, 2, FILL_USER, 1);
195  p->sphere(origin, radius, 2, FILL_USER, -1);
196 
197  p->origin(origin);
198 
199  if ( verbose & VERB_DEBUG ) {
200  cout << "DEBUG img_gold_particle: rad=" << radius << endl;
201  write_img("gold.map", p, 0);
202  }
203 
204  p->statistics();
205 
206  return p;
207 }
208 
228 Bmarker* img_find_gold_particles(Bimage* p, int img_select, double radius, long edge, double cutoff)
229 {
230  if ( radius < 1 ) return NULL;
231 
232  int img_start(0);
233  int img_end(p->images());
234  if ( img_select > -1 ) {
235  if ( img_select >= p->images() ) img_select = p->images() - 1;
236  img_start = img_select;
237  img_end = img_start + 1;
238  }
239 
240  Bimage* pgold = img_gold_particle(p->size(), radius);
241 
242  long i, j, x, y, z, n, nn, np(0);
243  long xx, yy, zz;
244  double fom_avg(0), fom_std(0), fom_max(0);
245  Vector3<long> ve(p->size()/2);
246  ve = ve.min(edge);
247 
248  if ( verbose & VERB_LABEL ) {
249  cout << "Finding particles:" << endl;
250  cout << "Particle radius: " << radius*p->sampling(0)[0]
251  << " A " << radius << " pixels" << endl;
252  cout << "Edge: " << edge << " pixels" << endl;
253  }
254 
255  if ( verbose & VERB_DEBUG )
256  cout << "DEBUG img_find_gold_particles: radius=" << radius << endl;
257 
258  Bimage* pone = NULL;
259  Bimage* pcc = NULL;
260 
261  long imgsize(p->sizeX()*p->sizeY()*p->sizeZ());
262  long fomsize(imgsize);
263  if ( img_select < 0 ) fomsize *= p->images();
264  float* fom = new float[fomsize];
265  for ( i=0; i<fomsize; i++ ) fom[i] = 0;
266 
267 // if ( verbose & VERB_PROCESS )
268 // cout << "Image\tx\ty\tz" << endl;
269  for ( i=0, n=img_start; n<img_end; n++ ) { // i is the external counter
270  pone = p->extract(n);
271  pcc = pone->cross_correlate(pgold, 2*p->sampling(0)[0], 8*radius*p->sampling(0)[0]);
272  delete pone;
273  pone = pcc->find_peaks(3);
274  delete pcc;
275  for ( j=0; j<imgsize; j++, i++ ) if ( (*pone)[j] ) {
276  fom[i] = (*pone)[j];
277  fom_avg += fom[i];
278  fom_std += fom[i]*fom[i];
279  if ( fom_max < fom[i] ) fom_max = fom[i];
280  np++;
281  }
282  delete pone;
283  if ( verbose & VERB_PROCESS )
284  cout << "Image " << n << ": " << np << " gold fiducials" << endl;
285  }
286 
287  fom_avg /= np;
288  fom_std = fom_std/np - fom_avg*fom_avg;
289  if ( fom_std > 0 ) fom_std = sqrt(fom_std);
290  else fom_std = 0;
291  if ( cutoff == 0 ) cutoff = fom_avg + fom_std;
292  else if ( cutoff < 0 ) cutoff = fom_max/2;
293  if ( verbose )
294  cout << "Cutoff = " << cutoff << endl;
295 
296  Bmarker* mark = NULL;
297  Bmarker* m = NULL;
298 
299  if ( np ) {
300  if ( verbose )
301  cout << "Marker\tx\ty\tz\tn\tFOM" << endl;
302  for ( j=nn=0, n=img_start; n<img_end; n++, nn++ ) {
303  for ( z=0; z<p->sizeZ(); z++ ) {
304  zz = (long)z - pgold->image->origin()[2];
305  if ( zz < 0 ) zz += p->sizeZ();
306  if ( zz >= ve[2] && zz < p->sizeZ()-ve[2] )
307  for ( y=0; y<p->sizeY(); y++ ) {
308  yy = (long)y - pgold->image->origin()[1];
309  if ( yy < 0 ) yy += p->sizeY();
310  if ( yy >= ve[1] && yy < p->sizeY()-ve[1] )
311  for ( x=0; x<p->sizeX(); x++ ) {
312  xx = (long)x - pgold->image->origin()[0];
313  if ( xx < 0 ) xx += p->sizeX();
314  if ( xx >= ve[0] && xx < p->sizeX()-ve[0] ) {
315  i = p->index(x, y, z, nn);
316  if ( fom[i] > cutoff ) {
317  m = (Bmarker *) add_item((char **) &m, sizeof(Bmarker));
318  if ( !mark ) mark = m;
319  j++;
320  m->id = j;
321  m->loc[0] = x - pgold->image->origin()[0];
322  m->loc[1] = y - pgold->image->origin()[1];
323  m->loc[2] = z - pgold->image->origin()[2];
324  m->img_num = n;
325  m->sel = 1;
326  m->fom = fom[i];
327  if ( m->loc[0] < 0 ) m->loc[0] += p->sizeX();
328  if ( m->loc[1] < 0 ) m->loc[1] += p->sizeY();
329  if ( m->loc[2] < 0 ) m->loc[2] += p->sizeZ();
330  if ( verbose )
331  cout << m->id << tab << m->loc[0] << tab <<
332  m->loc[1] << tab << m->loc[2] << tab <<
333  m->img_num << tab << m->fom << endl;
334  }
335  }
336  }
337  }
338  }
339  }
340  }
341 
342  delete pgold;
343 
344  delete[] fom;
345 
346  if ( verbose & VERB_LABEL )
347  cout << "Number of particles found: " << j << endl << endl;
348 
349  return mark;
350 }
351 
379 int project_mass_normalize(Bproject* project, double avg, double std, int norm_type,
380  DataType datatype, int setinputZslices, int setoutputZslices,
381  double cutmin, double cutmax, double replace_threshold)
382 {
383  if ( !project->field ) return -1;
384 
385  int nimg(0);
386  Bfield* field;
387  Bmicrograph* mg;
388  Bimage* p;
389  Bstring oldname;
390  Bstring normname;
391  DataType nudatatype;
392 
393  for ( field = project->field; field; field = field->next ) {
394  for ( mg = field->mg; mg; mg = mg->next ) {
395  if ( oldname != mg->fmg ) {
396  nimg++;
397  p = read_img(mg->fmg, 1, -1);
398  if ( !p ) {
399  error_show("project_mass_normalize", __FILE__, __LINE__);
400  return -1;
401  }
402  nudatatype = datatype;
403  if ( nudatatype == Unknown_Type )
404  nudatatype = p->data_type();
405  else if ( nudatatype > p->data_type() )
406  p->change_type(nudatatype);
407 
408  if ( setinputZslices )
409  if ( p->slices_to_images() < 0 ) {
410  error_show("project_mass_normalize", __FILE__, __LINE__);
411  return -1;
412  }
413 
414  if ( cutmin < cutmax ) p->truncate_to_min_max(cutmin, cutmax);
415 
416  if ( replace_threshold != 0 )
417  p->replace_maxima(replace_threshold);
418 
419  if ( norm_type >= 0 )
420  p->normalize(avg, std, norm_type);
421 
422  if ( setoutputZslices )
423  if ( p->images_to_slices() < 0 ) {
424  error_show("project_mass_normalize", __FILE__, __LINE__);
425  return -1;
426  }
427 
428  if ( p->data_type() != nudatatype ) {
429  if ( nudatatype == UCharacter ) p->truncate_to_min_max(0, 255);
430  p->change_type(nudatatype);
431  }
432 
433  normname = mg->fmg.pre_rev('.') + "_norm." + mg->fmg.post_rev('.');
434  write_img(normname, p, 0);
435  delete p;
436  }
437  oldname = mg->fmg;
438  mg->fmg = normname;
439  }
440  }
441 
442  return 0;
443 }
444 
445 double lambda_R(Bsimplex& simp)
446 {
447  long i;
448  double R(0), df, fs(0), fs2(0);
449  vector<double>& f = simp.dependent_values();
450  vector<double>& x = simp.independent_values();
451 
452  for ( i=0; i<simp.points(); i++ ) {
453  df = cos(x[i] + simp.parameter(2));
454  if ( df )
455  df = f[i] - simp.parameter(0)*exp(-simp.parameter(1)/df);
456  else
457  df = f[i] - simp.parameter(0);
458  fs += f[i];
459  fs2 += f[i] * f[i];
460  R += df*df;
461  }
462 
463  df = i*fs2-fs*fs;
464  if ( df > 0 ) R = sqrt(R/df);
465  else R = sqrt(R/i);
466 
467  return R;
468 }
469 
470 vector<double> simplex_fit_tilt_intensities(vector<double>& a, vector<double>& I)
471 {
472  long i;
473  double Imax(0);
474 
475  for ( i=0; i<I.size(); ++i ) if ( Imax < I[i] ) Imax = I[i];
476 
477  Bsimplex simp(1, 3, 0, a.size(), a, I);
478 
479  simp.parameter(0, 2*Imax);
480  simp.parameter(1, 0.6);
481  simp.parameter(2, -0.01);
482  simp.limits(0, 0.9*Imax, 5*Imax);
483  simp.limits(1, 0.01, 10);
484  simp.limits(2, -0.5, 0.5);
485 
486 // simp.show();
487 
488  long maxiter(10);
489  double R = simp.run(10000, 0.0001, lambda_R);
490 // simp.show();
491  for ( i=0; i<maxiter && ( !isfinite(R) || R > 1 ); ++i ) {
492  simp.parameter(0, 1.5*Imax);
493  simp.parameter(1, 0.7);
494  simp.parameter(2, 0.01);
495  R = simp.run(10000, 0.0001, lambda_R);
496 // simp.show();
497  }
498 
499  vector<double> result;
500  for ( i=0; i<3; ++i )
501  result.push_back(simp.parameter(i));
502  result.push_back(R);
503 
504  return result;
505 }
506 
516 {
517  if ( !project->field ) return NULL;
518 
519  long i, j, k;
520  Bfield* field = project->field;
521  Bmicrograph* mg = NULL;
522  Bimage* p = NULL;
523 
524  long ncol(3);
525  long nmg = field_count_mg_selected(field);
526  double amin(0), amax(0), Imin(1e30), Imax(-1e30);
527  Bstring title("Intensity-tilt curve: " + project->filename);
528 // cout << "Plot title: " << title << endl;
529  Bplot* plot = new Bplot(1, nmg, ncol);
530  plot->title(title);
531  plot->page(0).title(title);
532  plot->page(0).columns(ncol);
533  for ( i=0; i<ncol; ++i ) plot->page(0).column(i).number(i);
534  plot->page(0).column(0).label("Tilt angle");
535  plot->page(0).column(0).axis(1);
536  plot->page(0).column(1).type(0);
537  plot->page(0).column(1).element_size(5);
538  plot->page(0).column(1).label("Intensity");
539  plot->page(0).column(1).axis(3);
540  plot->page(0).column(2).type(2);
541  plot->page(0).column(2).label("Int(calc)");
542  plot->page(0).column(2).axis(3);
543  plot->page(0).column(1).color(1,0,0);
544  plot->page(0).column(2).color(0,0,1);
545 
546 // cout << "nmg=" << nmg << endl;
547 
548 // for ( field = project->field; field; field = field->next ) {
549  for ( mg = field->mg; mg; mg = mg->next ) {
550  if ( mg->intensity < 1e-10 ) {
551  if ( mg->fmg.length() ) {
552  p = read_img(mg->fmg, 0, mg->img_num);
553 // cout << mg->img_num << tab << p->image->average() << endl;
554  if ( fabs(p->image->average()) < 1e-20 ) {
555  delete p;
556  p = read_img(mg->fmg, 1, mg->img_num);
557  p->statistics();
558 // cout << mg->img_num << tab << p->image->average() << endl;
559  }
560  } else if ( mg->fframe.length() ) {
561  p = read_img(mg->fframe, 0, mg->img_num);
562  if ( fabs(p->image->average()) < 1e-20 ) {
563  delete p;
564  p = read_img(mg->fframe, 1, mg->img_num);
565  p->statistics();
566  }
567  }
568  if ( !p ) {
569  error_show("project_thickness_to_lambda_ratio", __FILE__, __LINE__);
570  return NULL;
571  } else {
572  mg->intensity = p->image->average();
573  delete p;
574  }
575  }
576  if ( mg->intensity < 0.1 ) mg->select = 0;
577  }
578 
579  for ( i=0, j=plot->rows(), k=2*j, mg = field->mg; mg; mg = mg->next )
580  if ( mg->select ) {
581  (*plot)[i] = mg->tilt_angle;
582  (*plot)[j] = mg->intensity;
583  (*plot)[k] = 0;
584  if ( amin > mg->tilt_angle ) amin = mg->tilt_angle;
585  if ( amax < mg->tilt_angle ) amax = mg->tilt_angle;
586  if ( Imin > mg->intensity ) Imin = mg->intensity;
587  if ( Imax < mg->intensity ) Imax = mg->intensity;
588  i++; j++; k++;
589  }
590 // }
591 
592  double f(Imax-Imin);
593  if ( -amin < amax ) amin = -amax;
594  else amax = -amin;
595  Imin = f*long(Imin/f);
596  Imax = f*long(Imax/f+1);
597  plot->page(0).column(0).min(amin*180.0/M_PI);
598  plot->page(0).column(0).max(amax*180.0/M_PI);
599  plot->page(0).column(1).min(Imin);
600  plot->page(0).column(1).max(Imax);
601  plot->page(0).column(2).min(Imin);
602  plot->page(0).column(2).max(Imax);
603 
604 // cout << "setup done " << endl;
605 
606  return plot;
607 }
608 
620 {
621  long i, j, k;
622  Bfield* field = project->field;
623  Bmicrograph* mg = NULL;
624  long nmg = field_count_mg_selected(field);
625 
626 // cout << "nmg = " << nmg << endl;
627 
628  if ( nmg < 3 ) {
629  if ( verbose )
630  cerr << "Too few micrographs to determine the thickness!" << endl;
631  return 0;
632  }
633 
634  if ( !plot )
636 
637  vector<double> a(plot->rows()), I(plot->rows());
638  for ( i=0, j=plot->rows(); i<plot->rows(); ++i, ++j ) {
639  a[i] = (*plot)[i];
640  I[i] = (*plot)[j];
641  }
642 
643 // cout << "starting fit" << endl;
644  vector<double> result = simplex_fit_tilt_intensities(a, I);
645 
646  double I0 = result[0];
647  double tlr = result[1];
648  double da = result[2];
649  double R = result[3];
650 
651  double Ip;
652  if ( verbose )
653  cout << endl << "Tilt\tInt\tInt(calc)" << endl;
654  for ( i=0, j=plot->rows(), k=2*j; i<plot->rows(); ++i, ++j, ++k ) {
655  Ip = I0*exp(-tlr/cos(a[i]+da));
656  (*plot)[i] = a[i]*180.0/M_PI;
657  (*plot)[k] = Ip;
658  if ( verbose )
659  cout << a[i]*180.0/M_PI << tab << I[i] << tab << Ip << endl;
660  }
661 
662  Bstring txt;
663  txt = Bstring(I0, "Direct beam intensity: %lg");
664  plot->page(0).add_text(txt);
665  txt = Bstring(tlr, "Tilt-lambda ratio: %lg");
666  plot->page(0).add_text(txt);
667  txt = Bstring(da*180.0/M_PI, "Specimen tilt angle: %lg");
668  plot->page(0).add_text(txt);
669  txt = Bstring(R, "Rfit: %lg");
670  plot->page(0).add_text(txt);
671 
672  if ( flag )
673  for ( mg = field->mg; mg; mg = mg->next )
674  mg->tilt_angle += da;
675 
676  if ( verbose ) {
677  cout << endl << "Calculating tilt intensity parameters:" << endl;
678  cout << "Residual: " << R << endl;
679  cout << "Direct beam intensity: " << I0 << endl;
680  cout << "Specimen tilt: " << da*180.0/M_PI << " degrees" << endl;
681  cout << "Thickness to lambda ratio: " << tlr << endl << endl;
682  }
683 
684  return tlr;
685 }
686 
687 
697 {
698  if ( !project->field ) return -1;
699 
700  long i, j, k;
701  Bfield* field = project->field;
702  Bmicrograph* mg = NULL;
703  Bimage* p = NULL;
704 
705  long nmg = field_count_mg_selected(field);
706  vector<double> a(nmg), I(nmg);
707 
708 // cout << "nmg=" << nmg << endl;
709 
710 // for ( field = project->field; field; field = field->next ) {
711  for ( mg = field->mg; mg; mg = mg->next ) {
712  if ( mg->intensity < 1e-10 ) {
713  if ( mg->fmg.length() ) {
714  p = read_img(mg->fmg, 0, mg->img_num);
715 // cout << mg->img_num << tab << p->image->average() << endl;
716  if ( fabs(p->image->average()) < 1e-20 ) {
717  delete p;
718  p = read_img(mg->fmg, 1, mg->img_num);
719  p->statistics();
720 // cout << mg->img_num << tab << p->image->average() << endl;
721  }
722  } else if ( mg->fframe.length() ) {
723  p = read_img(mg->fframe, 0, mg->img_num);
724  if ( fabs(p->image->average()) < 1e-20 ) {
725  delete p;
726  p = read_img(mg->fframe, 1, mg->img_num);
727  p->statistics();
728  }
729  }
730  if ( !p ) {
731  error_show("project_thickness_to_lambda_ratio", __FILE__, __LINE__);
732  return -1;
733  } else {
734  mg->intensity = p->image->average();
735  delete p;
736  }
737  }
738  if ( mg->intensity < 0.1 ) mg->select = 0;
739  }
740 
741  for ( i=0, mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
742  a[i] = mg->tilt_angle;
743  I[i] = mg->intensity;
744  i++;
745  }
746 // }
747 
748  nmg = i;
749 
750  if ( nmg < 3 ) {
751  if ( verbose )
752  cerr << "Too few micrographs to determine the thickness!" << endl;
753  return 0;
754  }
755 
756  vector<double> result = simplex_fit_tilt_intensities(a, I);
757 
758  double I0 = result[0];
759  double tlr = result[1];
760  double da = result[2];
761  double R = result[3];
762 
763  double Ip;
764  if ( verbose )
765  cout << endl << "Tilt\tInt\tInt(calc)" << endl;
766  for ( i=0, j=a.size(), k=2*j; i<a.size(); ++i, ++j, ++k ) {
767  Ip = I0*exp(-tlr/cos(a[i]+da));
768  if ( verbose )
769  cout << a[i]*180.0/M_PI << tab << I[i] << tab << Ip << endl;
770  }
771 
772  for ( mg = field->mg; mg; mg = mg->next )
773  mg->tilt_angle += da;
774 
775  if ( verbose ) {
776  cout << endl << "Calculating tilt intensity parameters:" << endl;
777  cout << "Residual: " << R << endl;
778  cout << "Direct beam intensity: " << I0 << endl;
779  cout << "Specimen tilt: " << da*180.0/M_PI << " degrees" << endl;
780  cout << "Thickness to lambda ratio: " << tlr << endl << endl;
781  }
782 
783  return tlr;
784 }
785 
795 double project_lambda(Bproject* project, double thickness)
796 {
797  if ( !project->field ) return -1;
798 
800 
801  double lambda = thickness/tlr;
802 
803  if ( verbose ) {
804  cout << "Thickness: " << thickness << " A" << endl;
805  cout << "Proportionality parameter: " << lambda << " A" << endl << endl;
806  }
807 
808  return lambda;
809 }
810 
820 double project_thickness(Bproject* project, double lambda)
821 {
822  if ( !project->field ) return -1;
823 
825 
826  double thickness = lambda*tlr;
827 
828  if ( verbose ) {
829  cout << "Thickness: " << thickness << " A" << endl << endl;
830  cout << "Proportionality parameter: " << lambda << " A" << endl << endl;
831  }
832 
833  return lambda;
834 }
835 
845 {
846  Bfield* field;
847  Bmicrograph* mg;
848 
849  if ( verbose )
850  cout << "Sorting markers" << endl << endl;
851 
852  for ( field = project->field; field; field = field->next )
853  for ( mg = field->mg; mg; mg = mg->next )
854  markers_sort_by_id(&mg->mark);
855 
856  return 0;
857 }
858 
869 {
870  if ( project_count_markers(project) < 1 ) return 0;
871 
872  Bmarker* m, *mr;
873  Bfield* field = NULL;
874  Bmicrograph* mg;
875  Bmicrograph* mg_ref = NULL;
876  Breconstruction* rec = project->rec;
877  Vector3<double> loc, shift, tilt_axis;
878  Quaternion q;
879  Transform t;
880  Matrix3 mat, mat2;
881  double d(0), dsum(0), dmgsum, w(0), wmg, mgfom, avg_axis(0), wa, wax(0);
882 
883  for ( field = project->field; field && !mg_ref; field = field->next )
884  if ( field->select ) mg_ref = field_find_zero_tilt_mg(field);
885 
886 // long nmg = count_list((char *) field->mg);
887 
888  if ( mg_ref->origin[0] < 1 || mg_ref->origin[1] < 1 )
890 
891  if ( mg_ref->matrix.determinant() < 0.5 )
893 
894  Bstring id("1");
895  if ( !project->rec ) {
896  rec = reconstruction_add(&project->rec, id);
897  rec->origin = mg_ref->origin;
898  }
899 
900  if ( !rec->mark )
902 
903  for ( mr = rec->mark; mr; mr = mr->next ) {
904  mr->err = 0;
905  mr->res = 0;
906  mr->sel = 0;
907  }
908 
909  if ( verbose && show ) {
910  cout << "Tomography alignment results:" << endl;
911  cout << "Mg\tShiftX\tShiftY\tScaleX\tScaleY\tAxis\tTilt\tLevel\tn\tResid\tFOM" << endl;
912  }
913 
914  for ( field = project->field; field; field = field->next ) if ( field->select ) {
915 /* if ( field != project->field ) {
916  mg_ref2 = field_find_zero_tilt_mg(field);
917  if ( mg_ref2->mark ) {
918  t = marker_find_transform(mg_ref2->mark, mg_ref->mark, rec->origin);
919 // cout << "axis=" << t.axis*180/M_PI << tab << "angle=" << t.angle*180/M_PI << endl;
920  field->matrix = Matrix3(t.axis, t.angle);
921  }
922  } else field->matrix = Matrix3(1);*/
923  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
924  shift = mg->origin - mg_ref->origin;
925  dmgsum = wmg = mgfom = 0;
926 // mat = mg->matrix.transpose(); // Handedness
927 // mat = mg->matrix * field->matrix;
928  for ( mr = rec->mark; mr; mr = mr->next ) {
929  for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
930  if ( m ) {
931 // loc = mg_location_from_3D_model(mr->loc, rec->origin, mat, mg->origin, mg->scale*rec->scale);
932  loc = mg_location_from_3D_model(mr->loc, rec->origin, mg->matrix, mg->origin, mg->scale*rec->scale);
933  m->err = loc - m->loc;
934  d = m->err.length2();
935  if ( m->sel > 0 ) {
936  dmgsum += d;
937  mgfom += m->fom;
938  mr->err += m->err;
939  mr->res += d;
940  mr->sel += 1;
941  wmg += 1;
942  }
943  d = sqrt(d);
944  m->res = d;
945  if ( ( verbose & VERB_FULL ) && show )
946  cout << mg->id << tab << m->id << tab << loc[0] << tab << loc[1] << tab << loc[2] << tab << d << endl;
947  }
948  }
949  dsum += dmgsum;
950  w += wmg;
951  if ( wmg ) {
952  mg->fom = sqrt(dmgsum/wmg);
953  mgfom /= wmg;
954  }
955  wa = 1 - exp(-mg->tilt_angle*mg->tilt_angle/0.5);
956  if ( fabs(mg->tilt_angle) > M_PI/45.0 ) {
957  avg_axis += wa*mg->tilt_axis;
958  wax += wa;
959  }
960  if ( verbose && show )
961  cout << mg->id << tab << fixed << setprecision(2) << shift[0] << tab << shift[1] << tab
962  << setprecision(4) << mg->scale[0] << tab << mg->scale[1] << tab
963  << mg->tilt_axis*180.0/M_PI << tab
964  << mg->tilt_angle*180.0/M_PI << tab
965  << mg->level_angle*180.0/M_PI << tab
966  << wmg << tab << mg->fom << tab << mgfom << endl;
967  }
968  }
969 
970  if ( wax ) avg_axis /= wax;
971  if ( w ) d = sqrt(dsum/w);
972 
973  // Fix tilt axis angles - particularly for the zero-tilt micrograph
974 // for ( field = project->field; field; field = field->next ) if ( field->select )
975 // for ( mg = field->mg; mg; mg = mg->next )
976 // if ( fabs(mg->tilt_axis - avg_axis) > M_PI/90.0 ) mg->tilt_axis = avg_axis;
977 
978  for ( mr = rec->mark; mr; mr = mr->next ) {
979  if ( mr->sel ) mr->err /= mr->sel;
980  mr->res = sqrt(mr->res/mr->sel);
981  }
982 
983  if ( verbose && show ) {
984  cout << "Average tilt axis angle: " << avg_axis*180.0/M_PI << endl << endl;
985  cout << "Marker errors:\nMarker\tn\tx\ty\tResid\tFOM" << endl;
986  for ( mr = rec->mark; mr; mr = mr->next )
987  cout << mr->id << tab << mr->sel << tab <<
988  setprecision(3) << mr->err[0] << tab << mr->err[1] << tab <<
989  mr->res << tab << setprecision(4) << mr->fom << endl;
990  cout << "Average residual = " << d << " pixels" << endl << endl;
991  }
992 
993  return d;
994 }
995 
1007 {
1008  Bmarker* m, *mr;
1009  Vector3<double> loc;
1010  double d(0), dsum(0), w(0);
1011 
1012  for ( mr = model; mr; mr = mr->next ) {
1013  for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
1014  if ( m ) {
1015  loc = mg_location_from_3D_model(mr->loc, oriref, mg->matrix, mg->origin, mg->scale);
1016  m->err = loc - m->loc;
1017  d = m->err.length2();
1018  if ( m->sel > 0 ) {
1019  dsum += d;
1020  w += 1;
1021  }
1022  d = sqrt(d);
1023  m->res = d;
1024  }
1025  }
1026 
1027  if ( w ) d = sqrt(dsum/w);
1028 
1029  return d;
1030 }
1031 
1041 {
1042  Bfield* field = project->field;
1043  Bmicrograph* mg;
1044  Bmarker *m1, *m2;
1045 
1046  int i, n(0);
1047  double scale = 10;
1048  int h[1000];
1049  for ( i=0; i<1000; i++ ) h[i] = 0;
1050 
1051  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1052  for ( mg = field->mg; mg->next; mg = mg->next ) {
1053  for ( m1=mg->mark, m2=mg->next->mark; m1 && m2; m1=m1->next, m2=m2->next ) {
1054  i = (int) (scale*(m1->err - m2->err).length() + 0.5);
1055  if ( i < 1000 ) {
1056  h[i]++;
1057  if ( n < i ) n = i;
1058  }
1059  }
1060  }
1061  }
1062 
1063  if ( verbose ) {
1064  cout << "Histogram of marker refinement shifts:" << endl;
1065  cout << "Shift\tCount" << endl;
1066  for ( i=0; i<=n; i++ )
1067  cout << setprecision(2) << i/scale << tab << h[i] << endl;
1068  cout << endl;
1069  }
1070 
1071  return 0;
1072 }
1073 
1084 {
1085  if ( project_count_markers(project) < 1 ) return 0;
1086 
1087  int nm;
1088  Bfield* field;
1089  Bmicrograph* mg;
1090  Bmarker *m, *mr;
1091  Vector3<double> t;
1092  Bmicrograph* mg_ref = NULL;
1093  Breconstruction* rec = project->rec;
1094 
1095  for ( field = project->field; field && !mg_ref; field = field->next )
1096  if ( field->select ) mg_ref = field_find_zero_tilt_mg(field);
1097 
1098  if ( !mg_ref->mark )
1099  for ( mg_ref = project->field->mg; mg_ref && !mg_ref->mark; mg_ref = mg_ref->next ) ;
1100 
1101  if ( !mg_ref )
1102  return error_show("project_calculate_model: No markers defined!", __FILE__, __LINE__);
1103 
1104  Bstring id("1");
1105  if ( !project->rec ) {
1106  rec = reconstruction_add(&project->rec, id);
1107  rec->origin = mg_ref->origin;
1108  rec->mark_radius = mg_ref->mark_radius;
1109  }
1110 
1111  if ( verbose & VERB_FULL )
1112  cout << "Calculating a 3D marker model" << endl << endl;
1113 
1114  // Set 3D model if not already present
1115  if ( !rec->mark )
1116  rec->mark = markers_copy(mg_ref->mark);
1117 
1118  // Fill in extra markers
1119  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1120  for ( mg = field->mg; mg; mg = mg->next ) {
1121  for ( m = mg->mark; m; m = m->next ) {
1122  for ( mr = rec->mark; mr && mr->id != m->id; mr = mr->next ) ;
1123  if ( !mr ) {
1124  mr = (Bmarker *) add_item((char **) &rec->mark, sizeof(Bmarker));
1125  if ( !rec->mark ) rec->mark = mr;
1126  mr->id = m->id;
1127  mr->loc = m->loc;
1128  mr->res = m->res;
1129  mr->fom = m->fom;
1130  mr->sel = m->sel;
1131  }
1132  }
1133  }
1134  }
1135 
1136  // Calculate the micrograph origins
1137  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1138  for ( mg = field->mg; mg; mg = mg->next ) {
1139  t = 0;
1140  nm = 0;
1141  for ( mr = rec->mark; mr; mr = mr->next ) {
1142  for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
1143  if ( m ) {
1144  t += m->loc - mg_location_from_3D_model(mr->loc, rec->origin, mg->matrix, mg->origin);
1145  nm++;
1146  }
1147  }
1148  if ( nm ) {
1149  t /= nm;
1150  mg->origin += t;
1151  }
1152  }
1153  }
1154 
1155  // Calculate the z-coordinates
1156  for ( mr = rec->mark; mr; mr = mr->next ) {
1157  mr->loc[2] = 0;
1158  mr->sel = 0;
1159  }
1160  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1161  for ( mg = field->mg; mg; mg = mg->next ) {
1162  for ( mr = rec->mark; mr; mr = mr->next ) {
1163  for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
1164  if ( m ) {
1165  t = mr->loc - rec->origin;
1166 /* if ( mg->matrix[6] ) {
1167  mr->loc[2] += (m->loc[0] - mg->origin[0] - mg->matrix[0]*t[0] - mg->matrix[3]*t[1])/mg->matrix[6];
1168  mr->sel++;
1169  }
1170  if ( mg->matrix[7] ) {
1171  mr->loc[2] += (m->loc[1] - mg->origin[1] - mg->matrix[1]*t[0] - mg->matrix[4]*t[1])/mg->matrix[7];
1172  mr->sel++;
1173  }*/
1174  if ( mg->matrix[2][0] ) {
1175  mr->loc[2] += (m->loc[0] - mg->origin[0] - mg->matrix[0][0]*t[0] - mg->matrix[1][0]*t[1])/mg->matrix[2][0];
1176  mr->sel++;
1177  }
1178  if ( mg->matrix[2][1] ) {
1179  mr->loc[2] += (m->loc[1] - mg->origin[1] - mg->matrix[0][1]*t[0] - mg->matrix[1][1]*t[1])/mg->matrix[2][1];
1180  mr->sel++;
1181  }
1182  }
1183  }
1184  }
1185  }
1186  for ( mr = rec->mark; mr; mr = mr->next ) {
1187  if ( mr->sel ) mr->loc[2] /= mr->sel;
1188  mr->sel = 1;
1189  }
1190 
1191  return 0;
1192 }
1193 
1205 {
1206  if ( !project->rec ) return -1;
1207 
1208  Bfield* field = project->field;
1209  Bmicrograph* mg;
1210 
1211  if ( verbose )
1212  cout << "Generating missing markers" << endl << endl;
1213 
1214  for ( field = project->field; field; field = field->next ) if ( field->select )
1215  for ( mg = field->mg; mg; mg = mg->next )
1217 
1218  for ( field = project->field; field; field = field->next ) if ( field->select )
1219  for ( mg = field->mg; mg; mg = mg->next )
1221 
1222  return 0;
1223 }
1224 
1236 long img_erase_markers(Bimage* p, Bmarker* mark, double marker_radius)
1237 {
1238  if ( marker_radius < 1 ) return 0;
1239 
1240  long nm(0);
1241  double fill, fstd;
1242  for ( ; mark; mark = mark->next, nm++ ) {
1243  p->stats_within_radii(0, mark->loc, marker_radius, 2*marker_radius, fill, fstd);
1244  p->sphere(mark->loc, marker_radius, 2, FILL_USER, fill);
1245  }
1246 
1247  return nm;
1248 }
1249 
1260 Bimage* mg_erase_markers(Bmicrograph* mg, double marker_radius)
1261 {
1262  if ( verbose & VERB_FULL )
1263  cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
1264 
1265  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1266  if ( !p ) {
1267  error_show("mg_erase_markers", __FILE__, __LINE__);
1268  return NULL;
1269  }
1270 
1271  p->change_type(Float);
1272 
1273  p->sampling(mg->pixel_size);
1274 
1275  p->origin(mg->origin[0], mg->origin[1], 0);
1276 
1277  if ( marker_radius < 1 ) return p;
1278 
1279  img_erase_markers(p, mg->mark, marker_radius);
1280 
1281  return p;
1282 }
1283 
1294 int project_erase_markers(Bproject* project, double marker_radius)
1295 {
1296  long n, nmg = project_count_micrographs(project);
1297 
1298  if ( nmg < 1 ) return -1;
1299 
1300  Bstring filename;
1301  Bfield* field = project->field;
1302  Bmicrograph* mg = field->mg;
1303  Bimage* p;
1304  Bimage* pall = NULL;
1305 
1306  if ( verbose )
1307  cout << "Removing markers with radius " << marker_radius << endl << endl;
1308 
1309  for ( field = project->field; field; field = field->next ) {
1310  nmg = field_count_micrographs(field);
1311  p = read_img(mg->fmg, 0, 0);
1312  if ( !p ) {
1313  error_show("project_erase_markers", __FILE__, __LINE__);
1314  return -1;
1315  }
1316  pall = new Bimage(p->data_type(), p->compound_type(), p->size(), nmg);
1317  delete p;
1318  if ( field->mg->pixel_size[0] ) pall->sampling(mg->pixel_size);
1319  filename = field->mg->fmg.pre_rev('.') + "_me." + field->mg->fmg.post_rev('.');
1320  for ( n=0, mg = field->mg; mg; mg = mg->next, n++ ) {
1321  if ( verbose )
1322  cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
1323  p = mg_erase_markers(mg, marker_radius);
1324  if ( !p ) {
1325  error_show("project_remove_markers", __FILE__, __LINE__);
1326  return -1;
1327  }
1328  pall->replace(n, p);
1329  pall->image[n] = p->image[0];
1330  delete p;
1331  mg->fmg = filename;
1332  }
1333  write_img(filename, pall, 0);
1334  delete pall;
1335  }
1336 
1337  return 0;
1338 }
1339 
1350 {
1351  if ( !mg->mark ) return 0;
1352 
1353  int n(0), ns(0);
1354  Bmarker *m, *mr;
1355 
1356  if ( verbose ) {
1357  cout << "Resetting 3D model x and y coordinates:" << endl;
1358  cout << "Micrograph: " << mg->id << endl << endl;
1359  }
1360 
1361  for ( mr = model; mr; mr = mr->next ) mr->sel = 0;
1362 
1363  for ( m = mg->mark; m; m = m->next ) {
1364  for ( mr = model; mr && m->id != mr->id; mr = mr->next ) ;
1365  if ( !mr ) {
1366  mr = (Bmarker *) add_item((char **) &model, sizeof(Bmarker));
1367  mr->id = m->id;
1368  }
1369  mr->loc[0] = m->loc[0];
1370  mr->loc[1] = m->loc[1];
1371  mr->sel = 1;
1372  }
1373 
1374  for ( mr = model; mr; mr = mr->next, n++ ) ns += mr->sel;
1375 
1376  if ( verbose )
1377  cout << "Markers in model: " << n << " (" << ns << ")" << endl << endl;
1378 
1379 // markers_delete_non_selected(&model);
1380 
1381  return ns;
1382 }
1383 
1394 long project_find_markers(Bproject* project, long edge, int add)
1395 {
1396  Bfield* field = NULL;
1397  Bmicrograph* mg = NULL;
1398 
1399  for ( field = project->field; field; field = field->next )
1400  for ( mg = field->mg; mg; mg = mg->next )
1401  mg_find_markers(mg, edge, add);
1402 
1404 }
1405 
1419 long mg_find_markers(Bmicrograph* mg, long edge, int add)
1420 {
1421  if ( mg->mark_radius < 1 ) {
1422  cerr << "Error: The marker radius is too low! (" << mg->mark_radius << ")" << endl;
1423  bexit(-1);
1424  }
1425 
1426  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1427  if ( !p )
1428  return error_show("mg_find_markers", __FILE__, __LINE__);
1429 
1430  Bmarker* mark = img_find_gold_particles(p, 0, mg->mark_radius, edge, -1);
1431 
1432  delete p;
1433 
1434  long nmark = count_list((char *) mark);
1435 
1436  if ( nmark ) {
1437  if ( add ) {
1438  markers_add(&mg->mark, mark, 2*mg->mark_radius, 2);
1439  } else {
1440  kill_list((char *) mg->mark, sizeof(Bmarker));
1441  mg->mark = mark;
1442  }
1443  }
1444 
1445  return nmark;
1446 }
1447 
1455 {
1456  if ( !mg->mark ) {
1457  error_show("No markers found!", __FILE__, __LINE__);
1458  return NULL;
1459  }
1460 
1461  int n;
1462 
1463  Bmarker* mark;
1464  Bimage* pmark;
1465 
1466  if ( verbose & VERB_DEBUG )
1467  cout << "DEBUG mg_composite_particle: size = " << size << endl;
1468 
1469  Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1470  if ( !p ) {
1471  error_show("mg_composite_particle", __FILE__, __LINE__);
1472  return NULL;
1473  }
1474 
1475  Bimage* pavg = new Bimage(Float, TSimple, size, 1);
1476  pavg->origin(pavg->size()/2);
1477 
1478  Vector3<double> ghalf(pavg->image->origin());
1479  Vector3<double> gorigin;
1480 
1481  for ( n=0, mark = mg->mark; mark; mark = mark->next, n++ ) if ( mark->sel > 0 ) {
1482  gorigin = mark->loc - ghalf;
1483  pmark = p->extract(0, gorigin, size);
1484  pmark->change_type(Float);
1485  pavg->add(pmark);
1486  delete pmark;
1487  }
1488 
1489  delete p;
1490 
1491  pavg->calculate_background();
1492 
1493  pavg->symmetrize_cylinder();
1494 
1495  pavg->statistics();
1496 
1497  pavg->calculate_background();
1498 
1499  return pavg;
1500 }
1501 
1508 {
1509  if ( !project ) return 0;
1510  if ( !project->field ) return 0;
1511  if ( !project->field->mg ) return 0;
1512 
1513  long nmark(0);
1514  Bfield* field;
1515  Bmicrograph* mg;
1516  Breconstruction* rec;
1517 
1518  if ( project->select < 1 ) {
1519  for ( field = project->field; field; field = field->next )
1520  for ( mg = field->mg; mg; mg = mg->next )
1521  nmark += count_list((char *) mg->mark);
1522  } else {
1523  for ( rec = project->rec; rec; rec = rec->next )
1524  nmark += count_list((char *) rec->mark);
1525  }
1526 
1527  return nmark;
1528 }
1529 
1536 {
1537  if ( !project ) return 0;
1538  if ( !project->field ) return 0;
1539  if ( !project->field->mg ) return 0;
1540 
1541  long i, nmark(0);
1542  Bfield* field;
1543  Bmicrograph* mg;
1544 
1545  cout << "Project markers:" << endl;
1546  for ( field = project->field; field; field = field->next ) {
1547  cout << "Field: " << field->id << endl;
1548  for ( mg = field->mg; mg; mg = mg->next ) {
1549  i = count_list((char *) mg->mark);
1550  nmark += i;
1551  cout << "\tMicrograph: " << mg->id << tab << i << " markers\tselection = " << mg->select << endl;
1552  }
1553  }
1554  cout << "Total:\t" << nmark << " markers" << endl << endl;
1555 
1556  return nmark;
1557 }
1558 
1565 long project_show_errors(Bproject* project, double error_cutoff)
1566 {
1567  if ( !verbose ) return 0;
1568  if ( !project ) return 0;
1569 
1570  long nmark(0), nerr(0);
1571  Bfield* field = project->field;
1572  Bmicrograph* mg;
1573  Bmarker* m;
1574 
1575  cout << "Project marker errors:" << endl;
1576  cout << "Micrograph\tMarker\tx\ty\tex\tey\ted" << endl;
1577  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1578  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1579  for ( m = mg->mark; m; m = m->next ) if ( m->sel > 0 ) {
1580  if ( m->res > error_cutoff ) {
1581  cout << mg->id << tab << m->id << tab << m->loc[0] << tab <<
1582  m->loc[1] << tab << m->err[0] << tab << m->err[1] << tab << m->res << endl;
1583  nerr++;
1584  }
1585  nmark++;
1586  }
1587  }
1588  }
1589  cout << "Total above " << error_cutoff << ":\t" << nerr << " markers (" << nerr*100.0/nmark << "%)" << endl << endl;
1590 
1591  return nmark;
1592 }
1593 
1604 {
1605  if ( !project ) return 0;
1606 
1607  long maxid;
1608  Bfield* field;
1609  Bmicrograph* mg;
1610  Breconstruction* rec;
1611  Bmarker* mark;
1612 
1613  for ( maxid=0, field = project->field; field; field = field->next )
1614  for ( mg = field->mg; mg; mg = mg->next )
1615  for ( mark = mg->mark; mark; mark = mark->next )
1616  if ( maxid < mark->id ) maxid = mark->id;
1617 
1618  for ( rec = project->rec; rec; rec = rec->next )
1619  for ( mark = rec->mark; mark; mark = mark->next )
1620  if ( maxid < mark->id ) maxid = mark->id;
1621 
1622  maxid++;
1623 
1624 // int* sel = new int[maxid];
1625 
1626 // select_numbers(deselect_list, maxid, sel);
1627  vector<int> sel = select_numbers(deselect_list, maxid);
1628 
1629  if ( verbose )
1630  cout << "Deselecting markers" << endl << endl;
1631  for ( field = project->field; field; field = field->next ) {
1632  for ( mg = field->mg; mg; mg = mg->next ) {
1633  for ( mark = mg->mark; mark; mark = mark->next )
1634  if ( sel[mark->id] ) mark->sel = 0;
1635  }
1636  }
1637 
1638  for ( rec = project->rec; rec; rec = rec->next ) {
1639  for ( mark = rec->mark; mark; mark = mark->next ) {
1640  if ( sel[mark->id] ) mark->sel = 0;
1641  }
1642  }
1643 
1644 // delete[] sel;
1645 
1646  long n = count_list((char *) project->field->mg->mark);
1647 
1648  return n;
1649 }
1650 
1661 {
1662  if ( !project ) return 0;
1663 
1664  long maxid;
1665  Bfield* field;
1666  Bmicrograph* mg;
1667  Breconstruction* rec;
1668  Bmarker* mark;
1669 
1670  for ( maxid=0, field = project->field; field; field = field->next )
1671  for ( mg = field->mg; mg; mg = mg->next )
1672  for ( mark = mg->mark; mark; mark = mark->next )
1673  if ( maxid < mark->id ) maxid = mark->id;
1674 
1675  maxid++;
1676 
1677 // int* sel = new int[maxid];
1678 
1679 // select_numbers(delete_list, maxid, sel);
1680  vector<int> sel = select_numbers(delete_list, maxid);
1681 
1682  if ( verbose )
1683  cout << "Deleting markers:" << endl;
1684  for ( field = project->field; field; field = field->next ) {
1685  if ( verbose )
1686  cout << "Field: " << field->id << ":" << endl;
1687  for ( mg = field->mg; mg; mg = mg->next ) {
1688  if ( verbose )
1689  cout << "\tMicrograph: " << mg->id << endl;
1690  for ( mark = mg->mark; mark; ) {
1691  if ( sel[mark->id] ) {
1692  if ( verbose )
1693  cout << "\t\tDeleting marker: " << mark->id << endl;
1694  mark = (Bmarker *) remove_item((char **)&mg->mark, (char *)mark, sizeof(Bmarker));
1695  } else {
1696  mark = mark->next;
1697  }
1698  }
1699  }
1700  }
1701 
1702  for ( rec = project->rec; rec; rec = rec->next ) {
1703  if ( verbose )
1704  cout << "Reconstruction: " << rec->id << endl;
1705  for ( mark = rec->mark; mark; ) {
1706  if ( sel[mark->id] ) {
1707  if ( verbose )
1708  cout << "\t\tDeleting marker: " << mark->id << endl;
1709  mark = (Bmarker *) remove_item((char **)&rec->mark, (char *)mark, sizeof(Bmarker));
1710  } else {
1711  mark = mark->next;
1712  }
1713  }
1714  }
1715 
1716 // delete[] sel;
1717 
1718  long n = count_list((char *) project->field->mg->mark);
1719 
1720  return n;
1721 }
1722 
1733 {
1734  if ( !project ) return 0;
1735 
1736  long maxid, i, j;
1737  Bfield* field;
1738  Bmicrograph* mg;
1739  Breconstruction* rec;
1740  Bmarker* mark;
1741 
1742  // Find maximum marker id
1743  for ( maxid=0, field = project->field; field; field = field->next )
1744  for ( mg = field->mg; mg; mg = mg->next )
1745  for ( mark = mg->mark; mark; mark = mark->next )
1746  if ( maxid < mark->id ) maxid = mark->id;
1747 
1748  for ( rec = project->rec; rec; rec = rec->next )
1749  for ( mark = rec->mark; mark; mark = mark->next );
1750  if ( maxid < mark->id ) maxid = mark->id;
1751 
1752  if ( verbose & VERB_DEBUG )
1753  cout << "DEBUG project_renumber_markers: maxid=" << maxid << endl;
1754 
1755  maxid++;
1756 
1757  // Establish all existing marker ids
1758  int* id = new int[maxid];
1759  for ( i=0; i<maxid; i++ ) id[i] = 0;
1760 
1761  for ( field = project->field; field; field = field->next )
1762  for ( mg = field->mg; mg; mg = mg->next )
1763  for ( mark = mg->mark; mark; mark = mark->next )
1764  id[mark->id]++;
1765 
1766  for ( rec = project->rec; rec; rec = rec->next )
1767  for ( mark = rec->mark; mark; mark = mark->next );
1768  id[mark->id]++;
1769 
1770  // Create a mapping from old to new ids
1771  for ( i=j=0; i<maxid; i++ )
1772  if ( id[i] ) id[i] = ++j;
1773 
1774  if ( verbose & VERB_DEBUG )
1775  cout << "DEBUG project_renumber_markers: new maxid=" << j << endl;
1776 
1777  for ( field = project->field; field; field = field->next )
1778  for ( mg = field->mg; mg; mg = mg->next )
1779  for ( mark = mg->mark; mark; mark = mark->next )
1780  mark->id = id[mark->id];
1781 
1782  for ( rec = project->rec; rec; rec = rec->next )
1783  for ( mark = rec->mark; mark; mark = mark->next );
1784  mark->id = id[mark->id];
1785 
1786  delete[] id;
1787 
1788  return j;
1789 }
1790 
1802 {
1803  if ( !project ) return 0;
1804  if ( mg_select.length() < 1 ) return 0;
1805 
1806  long n(0);
1807  Bfield* field = project->field;
1808  Bmicrograph* mg;
1809 
1810  vector<long> en = mg_select.split_into_integers(",");
1811 
1812  if ( verbose & VERB_PROCESS )
1813  cout << "Selecting micrographs:" << endl;
1814  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1815  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1816  mg->select = 0;
1817  for ( size_t i=0; i<en.size(); i++ ) {
1818  if ( mg->img_num == en[i] ) {
1819  mg->select = 1;
1820  n++;
1821  if ( verbose & VERB_PROCESS )
1822  cout << "Micrograph " << mg->id << " (" << mg->img_num << ") selected" << endl;
1823  }
1824  }
1825  }
1826  }
1827  if ( verbose & VERB_PROCESS )
1828  cout << "Total Selected: " << n << endl << endl;
1829 
1830  return n;
1831 }
1832 
1840 {
1841  if ( !project ) return 0;
1842 
1843  long i, nmg(0), n(0);
1844  Bfield* field = project->field;
1845  Bmicrograph* mg;
1846 
1847  if ( mg_exclude.contains("none") ) {
1848  for ( field = project->field; field; field = field->next )
1849  for ( mg = field->mg; mg; mg = mg->next ) mg->select = 1;
1850  return 0;
1851  }
1852 
1853  for ( field = project->field; field; field = field->next )
1854  for ( nmg=0, mg = field->mg; mg; mg = mg->next )
1855  if ( nmg <= mg->img_num ) nmg++;
1856 
1857 // int* en = new int[nmg];
1858 
1859 // select_numbers(mg_exclude, nmg, en);
1860  vector<int> en = select_numbers(mg_exclude, nmg);
1861 
1862  if ( verbose )
1863  cout << "Deselecting micrographs:" << endl;
1864  for ( field = project->field; field; field = field->next ) {
1865  for ( i=0, mg = field->mg; mg; mg = mg->next, i++ ) if ( en[i] ) {
1866  mg->select = 0;
1867  n++;
1868  if ( verbose & VERB_PROCESS )
1869  cout << "Micrograph " << mg->id << " (" << mg->img_num << ") deselected" << endl;
1870  }
1871  }
1872  if ( verbose )
1873  cout << "Total deselected: " << n << endl << endl;
1874 
1875 // delete[] en;
1876 
1877  return n;
1878 }
1879 
1889 int project_set_tilt_axis(Bproject* project, double tilt_axis)
1890 {
1891  Bfield* field = project->field;
1892  Bmicrograph* mg;
1893 
1894  for ( field = project->field; field; field = field->next ) if ( field->select )
1895  for ( mg = field->mg; mg; mg = mg->next )
1896  mg->tilt_axis = tilt_axis;
1897 
1899 
1900  return 0;
1901 }
1902 
1912 {
1913  Bfield* field = project->field;
1914  Bmicrograph* mg;
1915 
1916  for ( field = project->field; field; field = field->next ) if ( field->select )
1917  for ( mg = field->mg; mg; mg = mg->next )
1919 
1921 
1922  return 0;
1923 }
1924 
1936 int project_set_tilt_angles(Bproject* project, double tilt_start, double tilt_step)
1937 {
1938  double tilt;
1939  Bfield* field = project->field;
1940  Bmicrograph* mg;
1941 
1942  if ( fabs(tilt_step) < 0.005 ) { // Must be greater than 0.5 degrees
1943  cerr << "Error: The tilt step size is too small (" << tilt_step << ")!" << endl;
1944  return -1;
1945  }
1946 
1947  for ( field = project->field; field; field = field->next ) if ( field->select )
1948  for ( tilt=tilt_start, mg = field->mg; mg; mg = mg->next, tilt+=tilt_step )
1949  mg->tilt_angle = tilt;
1950 
1952 
1953  return 0;
1954 }
1955 
1967 {
1968  double axis_angle, avg_axis(0), wa, wax(0);
1969  Vector3<double> tilt_axis;
1970  Quaternion q;
1971  Transform t;
1972  Matrix3 mat;
1973  Bfield* field = project->field;
1974  Bmicrograph* mg;
1975 
1976  Bmicrograph* mg_ref = field_find_zero_tilt_mg(field);
1977 
1978  if ( field->next ) {
1979  mg = field_find_zero_tilt_mg(field->next);
1980  t = marker_find_transform(mg->mark, mg_ref->mark, mg_ref->origin);
1981  field->next->matrix = Matrix3(t.axis, t.angle);
1982  }
1983 
1984  for ( field = project->field; field; field = field->next ) if ( field->select ) {
1985 // cout << "field matrix: " << field->matrix << endl;
1986  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1987  if ( mg->matrix.determinant() > 0.5 ) {
1988 // if ( mg != mg_ref && mg->mark ) {
1989 // mat = mg->matrix * field->matrix;
1990  mat = field->matrix.transpose() * mg->matrix;
1991  q = mat.quaternion();
1992 // q = mg->matrix.quaternion();
1993  tilt_axis = q.axis();
1994  axis_angle = atan2(tilt_axis[1], tilt_axis[0]);
1995  if ( !isfinite(axis_angle) ) axis_angle = 0;
1996  mg->tilt_angle = -2*acos(q[0]);
1997  if ( !isfinite(mg->tilt_angle) ) mg->tilt_angle = 0;
1998  if ( fabs(mg->tilt_axis - axis_angle) < M_PI_2 ) {
1999  mg->tilt_axis = axis_angle;
2000  } else {
2001  if ( mg->tilt_axis > axis_angle ) mg->tilt_axis = axis_angle + M_PI;
2002  else mg->tilt_axis = axis_angle - M_PI;
2003  mg->tilt_angle = -mg->tilt_angle;
2004  }
2005  // Calculate level angle
2006  mg->level_angle = asin(tilt_axis[2]);
2007  if ( !isfinite(mg->level_angle) ) mg->level_angle = 0;
2008  if ( mg->level_angle > 0.2 ) mg->level_angle = 0;
2009  }
2010  wa = 1 - exp(-mg->tilt_angle*mg->tilt_angle/0.5);
2011  if ( fabs(mg->tilt_angle) > M_PI/45.0 ) {
2012  avg_axis += wa*mg->tilt_axis;
2013  wax += wa;
2014  }
2015  }
2016  }
2017 
2018  if ( wax ) avg_axis /= wax;
2019 
2020  // Fix tilt axis angles - particularly for the zero-tilt micrograph
2021  for ( field = project->field; field; field = field->next ) if ( field->select )
2022  for ( mg = field->mg; mg; mg = mg->next )
2023  if ( fabs(mg->tilt_axis - avg_axis) > M_PI/90.0 ) mg->tilt_axis = avg_axis;
2024 
2025  return 0;
2026 }
2027 
2037 {
2038  Bfield* field = project->field;
2039  Bmicrograph* mg;
2040 
2041  for ( field = project->field; field; field = field->next ) if ( field->select )
2042  for ( mg = field->mg; mg; mg = mg->next )
2043  mg->matrix = mg->matrix.transpose();
2044 
2045  return 0;
2046 }
2047 
2048 
2059 {
2060  Bfield* field;
2061  Bmicrograph* mg;
2062  Bmarker* mark;
2063 
2064  for ( field = project->field; field; field = field->next )
2065  for ( mg = field->mg; mg; mg = mg->next )
2066  for ( mark = mg->mark; mark; mark = mark->next )
2067  if ( mark->fom >= fom ) mark->sel = 1;
2068  else mark->sel = 0;
2069 
2070  return 0;
2071 }
2072 
2079 int project_set_marker_radius(Bproject* project, double mark_radius)
2080 {
2081  Bfield* field;
2082  Bmicrograph* mg;
2083  Breconstruction* rec;
2084 
2085  for ( field = project->field; field; field = field->next )
2086  for ( mg = field->mg; mg; mg = mg->next )
2087  mg->mark_radius = mark_radius;
2088 
2089  for ( rec = project->rec; rec; rec = rec->next )
2090  rec->mark_radius = mark_radius;
2091 
2092  return 0;
2093 }
2094 
2109 {
2110  Bfield* field = project->field;
2112  Bmarker* m;
2113  Bimage* p = read_img(mg->fmg, 0, mg->img_num);
2114 
2115  if ( flags & 1 ) {
2116  cout << "Markers outside micrograph boundaries:" << endl;
2117  cout << "Micrograph\tMarker\tx\ty" << endl;
2118  }
2119 
2120  for ( field = project->field; field; field = field->next ) if ( field->select ) {
2121  for ( mg = field->mg; mg; mg = mg->next ) {
2122  for ( m = mg->mark; m; m = m->next ) {
2123  if ( m->loc[0] < 0 || m->loc[0] > p->sizeX() || m->loc[1] < 0 || m->loc[1] > p->sizeY() ) {
2124  if ( flags & 1 ) cout << mg->id << tab << m->id << tab << m->loc[0] << tab << m->loc[1] << endl;
2125  if ( flags & 2 ) m->sel = 0;
2126  if ( flags & 4 ) m->fom = 0;
2127  if ( flags & 8 ) m->err = m->res = 0;
2128  }
2129  }
2130  }
2131  }
2132 
2133  delete p;
2134 
2135  return 0;
2136 }
2137 
2144 {
2145  Bfield* field = project->field;
2147  Breconstruction* rec = project->rec;
2148  Bmarker* m, *mm;
2149  Bimage* p = read_img(mg->fmg, 0, mg->img_num);
2150 
2151  for ( field = project->field; field; field = field->next ) if ( field->select ) {
2152  for ( mg = field->mg; mg; mg = mg->next ) {
2153  for ( m = mg->mark, mm = rec->mark; m; m = m->next, mm = mm->next ) {
2154  if ( m->loc[0] < 0 || m->loc[0] > p->sizeX() || m->loc[1] < 0 || m->loc[1] > p->sizeY() ) {
2155  m->loc = mg_location_from_3D_model(mm->loc, rec->origin, mg->matrix, mg->origin);
2156  m->err = 0;
2157  m->sel = 0;
2158  m->fom = m->res = 0;
2159  }
2160  }
2161  }
2162  }
2163 
2164  delete p;
2165 
2166  return 0;
2167 }
2168 
2178 {
2179  Bfield* field;
2180  Bmicrograph* mg;
2181 
2182  for ( field = project->field; field; field = field->next )
2183  for ( mg = field->mg; mg; mg = mg->next ) {
2184  mg->matrix = Matrix3(mg->tilt_angle, mg->tilt_axis);
2185  mg->matrix = mg->matrix.transpose();
2186  }
2187 
2188  return 0;
2189 }
2190 
2191 int img_clear_extraneous_areas(Bimage* p, Vector3<double> vec, double dist, double width)
2192 {
2193  long i, xx, yy, cc;
2194  double de(0), dr(0), edge, a(GOLDEN/width);
2195  Vector3<double> d, r, hr(p->size()/2-3*width);
2196 
2197  p->calculate_background();
2198 
2199  double fill(p->background(long(0)));
2200 
2201  for ( i=yy=0; yy<p->sizeY(); yy++ ) {
2202  d[1] = (double)yy - p->image->origin()[1];
2203  for ( xx=0; xx<p->sizeX(); xx++ ) {
2204  d[0] = (double)xx - p->image->origin()[0];
2205  r = d.abs() - hr;
2206  if ( r[0] < r[1] ) dr = a*r[1];
2207  else dr = a*r[0];
2208  de = a*(fabs(vec[0]*d[0] + vec[1]*d[1]) - dist);
2209  if ( dr > de ) de = dr;
2210  if ( de > 50 ) edge = 1e30;
2211  else edge = exp(de);
2212  if ( !isfinite(edge) ) {
2213  cerr << xx << " " << yy << ": Value too large or not finite!: " << edge <<endl;
2214  edge = 1e30;
2215  }
2216  for ( cc=0; cc<p->channels(); cc++, i++ )
2217  p->set(i, ((*p)[i] + fill*edge)/(1+edge));
2218  }
2219  }
2220 
2221  return 0;
2222 }
2223 
2232 int micrograph_clear_extraneous_areas(Bmicrograph* mg, Bimage* p, long thickness, double width)
2233 {
2234  if ( !p )
2235  return error_show("micrograph_clear_extraneous_areas", __FILE__, __LINE__);
2236 
2237  View view = View(mg->matrix);
2238  Vector3<double> v = Vector3<double>(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2239  double d(1e30);
2240  if ( v[0] && d > fabs(p->sizeX()*0.5/v[0]) ) d = fabs(p->sizeX()*0.5/v[0]);
2241  if ( v[1] && d > fabs(p->sizeY()*0.5/v[1]) ) d = fabs(p->sizeY()*0.5/v[1]);
2242  double a = d*cos(mg->tilt_angle) + fabs(0.5*thickness*sin(mg->tilt_angle));
2243 
2244  p->calculate_background();
2245  p->origin(mg->origin);
2246  p->image->view(view);
2247 
2248  img_clear_extraneous_areas(p, v, a, width);
2249 
2250  return 0;
2251 }
2252 
2260 int project_clear_extraneous_areas(Bproject* project, long thickness, double width)
2261 {
2262  long nmg(0);
2263  Bimage* p = NULL;
2264  Bimage* pnu = NULL;
2265  Bfield* field = project->field;
2266  Bmicrograph* mg = field->mg;
2267 
2268  Bstring newfile = mg->fmg;
2269  newfile = newfile.insert(newfile.rfind("."), "_clr");
2270 
2271  for ( field = project->field; field; field = field->next ) if ( field->select ) {
2272  for ( nmg=0, mg = field->mg; mg; mg = mg->next, nmg++ ) {
2273  if ( verbose )
2274  cout << "Micrograph " << mg->img_num << ": " << mg->fmg << endl;
2275  p = read_img(mg->fmg, 0, mg->img_num);
2276  if ( !p )
2277  return error_show("project_clear_extraneous_areas", __FILE__, __LINE__);
2278  if ( !pnu ) {
2279  pnu = p;
2280  } else {
2281  delete p;
2282  }
2283  }
2284  }
2285 
2286  if ( verbose ) {
2287  cout << "Clearing extraneous areas from micrographs:" << endl;
2288  cout << "Thickness: " << thickness << endl;
2289  cout << "Edge width: " << width << endl;
2290  cout << "New micrograph file name: " << newfile << endl << endl;
2291  }
2292 
2293  pnu->images(nmg);
2294  pnu->data_alloc();
2295 
2296 // mg = project->field->mg;
2297 
2298 /* View view;
2299  Vector3<double> v(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2300  double a, d(1e30);
2301  if ( v[0] && d > fabs(pnu->sizeX()*0.5/v[0]) ) d = fabs(pnu->sizeX()*0.5/v[0]);
2302  if ( v[1] && d > fabs(pnu->sizeY()*0.5/v[1]) ) d = fabs(pnu->sizeY()*0.5/v[1]);*/
2303 
2304  for ( field = project->field; field; field = field->next ) if ( field->select ) {
2305  for ( nmg=0, mg = field->mg; mg; mg = mg->next, nmg++ ) {
2306  p = read_img(mg->fmg, 1, mg->img_num);
2307  mg->fmg = newfile;
2308  micrograph_clear_extraneous_areas(mg, p, thickness, width);
2309 /* view = View(mg->matrix);
2310  if ( verbose )
2311  cout << "Micrograph " << nmg+1 << ": " << mg->fmg << endl;
2312  if ( !p )
2313  return error_show("project_clear_extraneous_areas", __FILE__, __LINE__);
2314  p->calculate_background();
2315  p->origin(mg->origin);
2316  v = Vector3<double>(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2317  a = d*cos(mg->tilt_angle) + fabs(0.5*thickness*sin(mg->tilt_angle));
2318  img_clear_extraneous_areas(p, v, a, width);*/
2319  pnu->replace(nmg, p);
2320  pnu->image[nmg] = p->image[0];
2321  pnu->image[nmg].origin(mg->origin);
2322  pnu->image[nmg].view(View(mg->matrix));
2323  delete p;
2324  }
2325  write_img(field->mg->fmg, pnu, 0);
2326  }
2327 
2328  delete pnu;
2329 
2330  return 0;
2331 }
2332 
2342 {
2343  long n;
2344  double avg_z_offset(0), zs(0), d;
2345  Bfield* field = project->field;
2346  Bmicrograph* mg = field->mg;
2348 
2349  for ( n=0, mg = field->mg; mg; mg = mg->next ) {
2350  if ( mg->tilt_angle > M_PI/180.0 ) {
2351  zs += fabs(mg->origin.distance(mg_ref->origin)/sin(mg->tilt_angle));
2352  n++;
2353  }
2354  }
2355 
2356  if ( n ) avg_z_offset = zs/n;
2357 
2358  cout << "Average z offset: " << avg_z_offset << endl << endl;
2359 
2360  cout << "Micrograph\tOffset\tAdjusted" << endl;
2361  for ( mg = field->mg; mg; mg = mg->next ) {
2362  d = mg->origin.distance(mg_ref->origin);
2363  cout << mg->id << tab << d << tab << d - fabs(avg_z_offset*sin(mg->tilt_angle)) << endl;
2364  }
2365  cout << endl;
2366 
2367  return avg_z_offset;
2368 }
2369 
2381 {
2382  Breconstruction* rec1 = project->rec;
2383  Breconstruction* rec2 = project->rec->next;
2384  Bmarker* mark1 = rec1->mark;
2385  Bmarker* mark2 = rec2->mark;
2386 
2387  long n;
2388  double R(0);
2389 
2390  if ( verbose & VERB_FULL )
2391  cout << "Marker\tLocation\tResidual" << endl;
2392  for ( n=0, mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
2393  for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
2394  if ( mark2 && mark2->sel ) {
2395  mark1->err = mark1->loc - mark2->loc;
2396  mark1->res = mark1->err.length();
2397  R += mark1->res*mark1->res;
2398  n++;
2399  mark1->loc = (mark1->loc + mark2->loc)/2; // Set the marker model to the average
2400  if ( verbose & VERB_FULL )
2401  cout << mark1->id << tab << mark1->loc << tab << mark1->res << endl;
2402  }
2403  }
2404 
2405  if ( n ) R = sqrt(R/n);
2406 
2407  if ( verbose & VERB_FULL ) {
2408  cout << "Markers selected: " << n << endl;
2409  cout << "Residual: " << R << endl << endl;
2410  }
2411 
2412  reconstruction_kill(rec2);
2413  rec1->next = NULL;
2414 
2415  return n;
2416 }
2417 
2418 
field_find_low_tilt_mg_with_markers
Bmicrograph * field_find_low_tilt_mg_with_markers(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series with markers.
Definition: mg_select.cpp:312
Bmicrograph::scale
Vector3< double > scale
Definition: mg_processing.h:224
Bsub_image::average
double average()
Definition: Bimage.h:119
mg_marker_init
int mg_marker_init(Bmicrograph *mg, Bmarker *model)
Initializes markers from the reference.
Definition: mg_tomography.cpp:30
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
Bmarker::fom
float fom
Definition: marker.h:28
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_select.cpp:671
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
project_thickness_to_lambda_ratio
double project_thickness_to_lambda_ratio(Bproject *project)
Determines the thickness to mean free path ratio from a tilt series.
Definition: mg_tomography.cpp:696
matrix_linear.h
Header file for matrix functions.
project_mg_select
long project_mg_select(Bproject *project, Bstring &mg_select)
Selects micrographs in a project hierarchy.
Definition: mg_tomography.cpp:1801
markers_add
long markers_add(Bmarker **mark, Bmarker *mark2, double mindist, int sel)
Adds a set of markers to another.
Definition: marker.cpp:170
img_find_gold_particles
Bmarker * img_find_gold_particles(Bimage *p, int img_select, double radius, long edge, double cutoff)
Finds gold particles in micrograph images.
Definition: mg_tomography.cpp:228
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
project_show_errors
long project_show_errors(Bproject *project, double error_cutoff)
Shows the errors in marker coordinates in a project hierarchy.
Definition: mg_tomography.cpp:1565
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::find_peaks
Bimage * find_peaks(long kernelsize)
Finds peaks in a map with periodic boundaries.
Definition: Bimage_correlate.cpp:1739
Bimage::symmetrize_cylinder
Bimage * symmetrize_cylinder(int flag)
Calculates a cylindrically symmetrized map.
Definition: Bimage_helix.cpp:524
Bstring::insert
Bstring insert(long pos, const Bstring &s)
Definition: Bstring.cpp:598
Bproject::select
int select
Definition: mg_processing.h:342
UCharacter
@ UCharacter
Definition: utilities.h:153
Quaternion
Definition: Quaternion.h:25
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
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
project_thickness
double project_thickness(Bproject *project, double lambda)
Determines the thickness from a tilt series and mean free path.
Definition: mg_tomography.cpp:820
Bmarker::img_num
int img_num
Definition: marker.h:24
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
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
mg_tomo_residuals
double mg_tomo_residuals(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref)
Calculates the residuals for an alignment solution of one micrograph.
Definition: mg_tomography.cpp:1006
Bproject::field
Bfield * field
Definition: mg_processing.h:346
project_sort_markers_by_id
int project_sort_markers_by_id(Bproject *project)
Sorts markers for each micrograph by ID number.
Definition: mg_tomography.cpp:844
markers_copy
Bmarker * markers_copy(Bmarker *mark)
Copies a set of markers.
Definition: marker.cpp:142
marker_find_transform
Transform marker_find_transform(Bmarker *markers1, Bmarker *markers2, Vector3< double > origin)
Determines the rotation matrix between two sets of markers.
Definition: marker.cpp:396
project_deselect_markers
long project_deselect_markers(Bproject *project, Bstring &deselect_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1603
Bcolumn::color
double * color()
Definition: Bplot.h:76
project_set_nominal_mg_origins
int project_set_nominal_mg_origins(Bproject *project)
Sets micrograph origins to the centers of the micrographs.
Definition: mg_img_proc.cpp:770
mg_update_model
int mg_update_model(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref)
Fills in missing markers in the 3D model from micrograph markers.
Definition: mg_tomography.cpp:111
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bcolumn::element_size
double element_size()
Definition: Bplot.h:74
project_fix_markers
int project_fix_markers(Bproject *project)
Checks if the markers in a project parameter structure fall within micrographs.
Definition: mg_tomography.cpp:2143
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
mg_marker_update
int mg_marker_update(Bmicrograph *mg, Bmarker *model, Vector3< double > oriref, int update_location)
Calculates the predicted positions of markers from the reference.
Definition: mg_tomography.cpp:60
project_show_markers
long project_show_markers(Bproject *project)
Shows the markers in a project hierarchy.
Definition: mg_tomography.cpp:1535
GOLDEN
#define GOLDEN
Definition: utilities.h:85
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
mg_select.h
Header file for micrograph processing.
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bproject::filename
Bstring filename
Definition: mg_processing.h:338
Bimage::normalize
int normalize(long imgnum, double average, double stdev, int norm_type, long bins)
Normalizes a sub-image to a desired average and standard deviation.
Definition: Bimage_rescale.cpp:481
Bimage::background
double background(long nn)
Definition: Bimage.h:549
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
simplex.h
Nelder and Mead downhill simplex method for generalized parameter fitting.
project_erase_markers
int project_erase_markers(Bproject *project, double marker_radius)
Erases or paints markers in micrographs.
Definition: mg_tomography.cpp:1294
Bplot
Definition: Bplot.h:146
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bfield::id
Bstring id
Definition: mg_processing.h:262
View
Definition: View.h:26
mg_erase_markers
Bimage * mg_erase_markers(Bmicrograph *mg, double marker_radius)
Erases or paints markers in micrographs.
Definition: mg_tomography.cpp:1260
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bcolumn::type
long type()
Definition: Bplot.h:66
Bsimplex::dependent_values
vector< double > & dependent_values()
Definition: simplex.h:85
Breconstruction::mark_radius
double mark_radius
Definition: mg_processing.h:304
Bfield
Definition: mg_processing.h:260
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bsimplex
Structure used in the downhill simplex method.
Definition: simplex.h:44
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bcolumn::axis
long axis()
Definition: Bplot.h:68
M_PI_2
#define M_PI_2
Definition: utilities.h:66
Bmarker::res
float res
Definition: marker.h:27
intensity
double intensity(JSvalue &el, double d, double thickness, Batomtype *atompar, CTFparam &ctf)
Calculates the expected intensity given a thickness of vitreous ice.
Definition: scatter.cpp:391
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:207
project_fit_intensities
double project_fit_intensities(Bproject *project, Bplot *plot, int flag)
Determines the thickness to mean free path ratio from a tilt series.
Definition: mg_tomography.cpp:619
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
project_delete_markers
long project_delete_markers(Bproject *project, Bstring &delete_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1660
mg_composite_particle
Bimage * mg_composite_particle(Bmicrograph *mg, Vector3< long > size)
Generates an average particle image from a micrograph.
Definition: mg_tomography.cpp:1454
img_gold_particle
Bimage * img_gold_particle(Vector3< long > size, double radius)
Generates an image representing a gold particle.
Definition: mg_tomography.cpp:188
Bmarker
Definition: marker.h:21
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
Bsimplex::independent_values
vector< double > & independent_values()
Definition: simplex.h:84
project_mg_exclude
long project_mg_exclude(Bproject *project, Bstring &mg_exclude)
Deselects micrographs in a project hierarchy.
Definition: mg_tomography.cpp:1839
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
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
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
project_set_marker_radius
int project_set_marker_radius(Bproject *project, double mark_radius)
Sets the marker radius in a project parameter structure.
Definition: mg_tomography.cpp:2079
Bsimplex::points
long points()
Definition: simplex.h:66
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
project_marker_rotation_axis
double project_marker_rotation_axis(Bproject *project)
Determines the most likely rotation axis location for a tilt series.
Definition: mg_tomography.cpp:2341
Bstring::rfind
long rfind(const Bstring &s)
Definition: Bstring.h:106
project_renumber_markers
long project_renumber_markers(Bproject *project)
Renumbers the markers in a project hierarchy.
Definition: mg_tomography.cpp:1732
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Bcolumn::max
double max()
Definition: Bplot.h:72
Bmarker::id
int id
Definition: marker.h:23
Bmicrograph::level_angle
double level_angle
Definition: mg_processing.h:221
project_tomo_errors
double project_tomo_errors(Bproject *project)
Analyzes marker errors.
Definition: mg_tomography.cpp:1040
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
project_check_markers
int project_check_markers(Bproject *project, int flags)
Checks if the markers in a project parameter structure fall within micrographs.
Definition: mg_tomography.cpp:2108
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
project_intensity_plot
Bplot * project_intensity_plot(Bproject *project)
Transfers the micrograph intensities for a field into a plot.
Definition: mg_tomography.cpp:515
project_merge_rec_markers
int project_merge_rec_markers(Bproject *project)
Merges the markers for two reconstructions.
Definition: mg_tomography.cpp:2380
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
timer.h
Utilities for timing functions.
lambda_R
double lambda_R(Bsimplex &simp)
Definition: mg_tomography.cpp:445
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bsimplex::run
double run(long maxcycles, double tolerance, double(funk)(Bsimplex &))
Definition: simplex.cpp:76
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bmarker::err
Vector3< float > err
Definition: marker.h:26
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
project_mass_normalize
int project_mass_normalize(Bproject *project, double avg, double std, int norm_type, DataType datatype, int setinputZslices, int setoutputZslices, double cutmin, double cutmax, double replace_threshold)
Normalizes a set of images in a project to a desired average and standard deviation.
Definition: mg_tomography.cpp:379
project_lambda
double project_lambda(Bproject *project, double thickness)
Determines the mean free path from a tilt series and thickness.
Definition: mg_tomography.cpp:795
show
int show(0)
field_count_mg_selected
long field_count_mg_selected(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_select.cpp:686
project_set_tilt_axis
int project_set_tilt_axis(Bproject *project, double tilt_axis)
Sets the tilt axis angle in a project parameter structure.
Definition: mg_tomography.cpp:1889
remove_item
char * remove_item(char **list, char *item, unsigned long size)
Finds the given item and deletes it from the linked list.
Definition: linked_list.cpp:114
micrograph_clear_extraneous_areas
int micrograph_clear_extraneous_areas(Bmicrograph *mg, Bimage *p, long thickness, double width)
Clears extraneous areas from a micrograph.
Definition: mg_tomography.cpp:2232
project_calculate_model
int project_calculate_model(Bproject *project)
Calculates the z-coordinates of the marker model.
Definition: mg_tomography.cpp:1083
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bplot::title
Bstring & title()
Definition: Bplot.h:194
mg_img_proc.h
Header file for image processing from micrograph structures.
Bsimplex::parameter
void parameter(long i, double p)
Definition: simplex.h:71
Bimage::truncate_to_min_max
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:331
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
mg_3D_location_from_marker
Vector3< double > mg_3D_location_from_marker(Vector3< double > loc, Vector3< double > ori3D, Matrix3 mat, Vector3< double > ori)
Definition: mg_tomography.cpp:91
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
project_mg_marker_select
int project_mg_marker_select(Bproject *project, double fom)
Selects markers.
Definition: mg_tomography.cpp:2058
img_erase_markers
long img_erase_markers(Bimage *p, Bmarker *mark, double marker_radius)
Erases or paints markers in an image.
Definition: mg_tomography.cpp:1236
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.
field_find_micrograph_by_tiltang
Bmicrograph * field_find_micrograph_by_tiltang(Bfield *field, double tiltang)
Returns a pointer to the micrograph in a field-of-view structure with the specified tilt angle.
Definition: mg_select.cpp:243
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
project_set_tilt_angles
int project_set_tilt_angles(Bproject *project, double tilt_start, double tilt_step)
Sets the tilt and tilt axis angles in a project parameter structure.
Definition: mg_tomography.cpp:1936
Bfield::matrix
Matrix3 matrix
Definition: mg_processing.h:264
verbose
int verbose
Breconstruction
Definition: mg_processing.h:283
project_tomo_residuals
double project_tomo_residuals(Bproject *project, int show)
Calculates the residuals for an alignment solution.
Definition: mg_tomography.cpp:868
Bstring::length
long length() const
Definition: Bstring.cpp:263
isfinite
bool isfinite(complex< T > &value)
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:225
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
reconstruction_kill
int reconstruction_kill(Breconstruction *rec)
Frees a whole reconstruction parameter structure.
Definition: mg_processing.cpp:582
field_find_zero_tilt_mg
Bmicrograph * field_find_zero_tilt_mg(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series.
Definition: mg_select.cpp:281
Bfield::select
long select
Definition: mg_processing.h:265
project_count_markers
long project_count_markers(Bproject *project)
Counts the markers in a project hierarchy.
Definition: mg_tomography.cpp:1507
project_invert_tilt_axis
int project_invert_tilt_axis(Bproject *project)
Inverts the tilt axis in a project parameter structure.
Definition: mg_tomography.cpp:1911
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
model
Bmodel * model
Definition: bshow.cpp:34
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Bmicrograph::fom
double fom
Definition: mg_processing.h:239
rwimg.h
Header file for 2D and 3D image I/O.
Quaternion::axis
Vector3< double > axis()
Definition: Quaternion.h:144
Bfield::next
Bfield * next
Definition: mg_processing.h:261
simplex_fit_tilt_intensities
vector< double > simplex_fit_tilt_intensities(vector< double > &a, vector< double > &I)
Definition: mg_tomography.cpp:470
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
mg_processing.h
Header file for micrograph processing.
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bmicrograph::intensity
double intensity
Definition: mg_processing.h:217
Bmarker::sel
int sel
Definition: marker.h:29
project_find_markers
long project_find_markers(Bproject *project, long edge, int add)
Finds all markers in a project.
Definition: mg_tomography.cpp:1394
linked_list.h
Header file for generalized linked list functions.
Bmicrograph::select
long select
Definition: mg_processing.h:204
Breconstruction::scale
Vector3< double > scale
Definition: mg_processing.h:297
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
mg_reset_model
int mg_reset_model(Bmicrograph *mg, Bmarker *model)
Resets the model x and y coordinates from the zero-tilt micrograph.
Definition: mg_tomography.cpp:1349
Vector3::min
Type min()
Definition: Vector3.h:169
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
img_clear_extraneous_areas
int img_clear_extraneous_areas(Bimage *p, Vector3< double > vec, double dist, double width)
Definition: mg_tomography.cpp:2191
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Transform::angle
double angle
Definition: Transform.h:29
project_generate_markers
int project_generate_markers(Bproject *project)
Generates missing micrograph and model markers.
Definition: mg_tomography.cpp:1204
markers_sort_by_id
Bmarker * markers_sort_by_id(Bmarker **mark)
Sorts a set of markers by ID number.
Definition: marker.cpp:1154
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Transform
Definition: Transform.h:24
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Vector3::length
double length()
Definition: Vector3.h:222
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
project_clear_extraneous_areas
int project_clear_extraneous_areas(Bproject *project, long thickness, double width)
Clears areas from a tilt series not used in a reconstruction.
Definition: mg_tomography.cpp:2260
Bcolumn::number
long number()
Definition: Bplot.h:61
Bsimplex::limits
void limits(long i, double vlo, double vhi)
Definition: simplex.h:77
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
mg_location_from_3D_model
Vector3< double > mg_location_from_3D_model(Vector3< double > loc3D, Matrix3 mat, Vector3< double > ori)
Calculates the 2D location of a marker from a 3D model.
Definition: mg_tomography.cpp:140
project_invert_matrices
int project_invert_matrices(Bproject *project)
Inverts the micrograph matrices in a project parameter structure.
Definition: mg_tomography.cpp:2036
Bimage::stats_within_radii
long stats_within_radii(long nn, Vector3< double > loc, double rad_min, double rad_max, double &vavg, double &vstd)
Calculates the statistics for an image within given radii from a location.
Definition: Bimage_stats.cpp:205
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:335
Bcolumn::min
double min()
Definition: Bplot.h:70
mg_find_markers
long mg_find_markers(Bmicrograph *mg, long edge, int add)
Finds markers in a micrograph.
Definition: mg_tomography.cpp:1419
project_calculate_angles
int project_calculate_angles(Bproject *project)
Calculates tilt and tilt axis angles from matrices in a project parameter structure.
Definition: mg_tomography.cpp:1966
Matrix3::quaternion
Quaternion quaternion() const
Definition: Matrix3.h:129
Vector3::length2
double length2()
Definition: Vector3.h:221
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_select.cpp:350
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bstring::split_into_integers
vector< long > split_into_integers(const Bstring &delim)
Definition: Bstring.cpp:657
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:440
mg_tomography.h
Header file for single particle image processing in Bsoft.
Vector3::abs
Vector3 abs()
Definition: Vector3.h:201
Bimage::replace_maxima
long replace_maxima(double threshold)
Replaces maxima above a threshold using local averages.
Definition: Bimage_segment.cpp:678
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284