| Bsoft 2.0.5 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_img_proc.h"
12 #include "rwimg.h"
13 #include "matrix_linear.h"
14 #include "simplex.h"
15 #include "linked_list.h"
16 #include "utilities.h"
17 #include "timer.h"
18
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21
22
29 int mg_marker_init(Bmicrograph* mg, Bmarker* model)
30 {
31 if ( !model )
32 return error_show("No markers found!", __FILE__, __LINE__);
33
34 Bmarker* mark = NULL;
35 Bmarker* mark2 = NULL;
36
37 for ( mark = model; mark; mark=mark->next ) {
38 mark2 = (Bmarker *) add_item((char **) &mark2, sizeof(Bmarker));
39 if ( !mg->mark ) mg->mark = mark2;
40 mark2->id = mark->id;
41 mark2->fom = 1;
42 mark2->sel = mark->sel;
43 }
44
45 return 0;
46 }
47
59 int mg_marker_update(Bmicrograph* mg, Bmarker* model, Vector3<double> oriref, int update_location)
60 {
61 if ( !model )
62 return error_show("No model markers found!", __FILE__, __LINE__);
63
64 int upd;
65 Bmarker* mark = NULL;
66 Bmarker* modmark = NULL;
67
68 // if ( !mg->mark ) mg_marker_init(mg, model);
69
70 if ( verbose & VERB_DEBUG )
71 cout << "DEBUG mg_marker_update: mg=" << mg->id << endl;
72
73 for ( modmark = model; modmark; modmark = modmark->next ) {
74 upd = update_location;
75 for ( mark = mg->mark; mark && mark->id != modmark->id; mark = mark->next ) ;
76 if ( !mark ) {
77 mark = (Bmarker *) add_item((char **) &mg->mark, sizeof(Bmarker));
78 mark->id = modmark->id;
79 mark->fom = 1;
80 mark->sel = modmark->sel;
81 upd = 1;
82 }
83 if ( upd )
84 mark->loc = mg_location_from_3D_model(modmark->loc, oriref, mg->matrix, mg->origin);
85 }
86
87 return 0;
88 }
89
90 Vector3<double> mg_3D_location_from_marker(Vector3<double> loc, Vector3<double> ori3D, Matrix3 mat, Vector3<double> ori)
91 {
92 Vector3<double> loc3D = loc - ori;
93 loc3D = mat * loc3D;
94 loc3D += ori3D;
95 // loc3D[2] = 0;
96
97 return loc3D;
98 }
99
110 int mg_update_model(Bmicrograph* mg, Bmarker* model, Vector3<double> oriref)
111 {
112 if ( !model )
113 return error_show("No model markers found!", __FILE__, __LINE__);
114
115 Bmarker* mark = NULL;
116 Bmarker* modmark = NULL;
117
118 for ( mark = mg->mark; mark; mark = mark->next ) {
119 for ( modmark = model; modmark && mark->id != modmark->id; modmark = modmark->next ) ;
120 if ( !modmark ) {
121 modmark = (Bmarker *) add_item((char **) &model, sizeof(Bmarker));
122 modmark->id = mark->id;
123 modmark->fom = 1;
124 modmark->sel = mark->sel;
125 modmark->loc = mg_3D_location_from_marker(mark->loc, oriref, mg->matrix, mg->origin);
126 }
127 }
128
129 return 0;
130 }
131
139 Vector3<double> mg_location_from_3D_model(Vector3<double> loc3D, Matrix3 mat,
140 Vector3<double> ori)
141 {
142 Matrix3 tmat = mat.transpose();
143 Vector3<double> mloc = tmat * loc3D;
144 mloc += ori;
145 mloc[2] = 0;
146
147 return mloc;
148 }
149
150 Vector3<double> mg_location_from_3D_model(Vector3<double> loc3D, Matrix3 mat,
151 Vector3<double> ori, Vector3<double> scale)
152 {
153 Matrix3 tmat = mat.transpose();
154 Vector3<double> mloc = tmat * loc3D;
155 mloc /= scale;
156 mloc += ori;
157 mloc[2] = 0;
158
159 return mloc;
160 }
161
162 Vector3<double> mg_location_from_3D_model(Vector3<double> loc3D, Vector3<double> ori3D,
163 Matrix3 mat, Vector3<double> ori)
164 {
165 Vector3<double> mloc = loc3D - ori3D;
166 return mg_location_from_3D_model(mloc, mat, ori);
167 }
168
169 Vector3<double> mg_location_from_3D_model(Vector3<double> loc3D, Vector3<double> ori3D,
170 Matrix3 mat, Vector3<double> ori, Vector3<double> scale)
171 {
172 Vector3<double> mloc = loc3D - ori3D;
173 // cout << loc3D << endl << ori3D << endl << ori << endl << scale << endl;
174 return mg_location_from_3D_model(mloc, mat, ori, scale);
175 }
176
187 Bimage* img_gold_particle(Vector3<long> size, double radius)
188 {
189 Bimage* p = new Bimage(Float, TSimple, size, 1);
190 Vector3<double> origin(p->size()/2);
191 double outrad = sqrt(2.0)*radius;
192
193 p->sphere(origin, outrad, 2, FILL_USER, 1);
194 p->sphere(origin, radius, 2, FILL_USER, -1);
195
196 p->origin(origin);
197
198 if ( verbose & VERB_DEBUG ) {
199 cout << "DEBUG img_gold_particle: rad=" << radius << endl;
200 write_img("gold.map", p, 0);
201 }
202
203 p->statistics();
204
205 return p;
206 }
207
227 Bmarker* img_find_gold_particles(Bimage* p, int img_select, double radius, long edge, double cutoff)
228 {
229 if ( radius < 1 ) return NULL;
230
231 int img_start(0);
232 int img_end(p->images());
233 if ( img_select > -1 ) {
234 if ( img_select >= p->images() ) img_select = p->images() - 1;
235 img_start = img_select;
236 img_end = img_start + 1;
237 }
238
239 Bimage* pgold = img_gold_particle(p->size(), radius);
240
241 long i, j, x, y, z, n, nn, np(0);
242 long xx, yy, zz;
243 double fom_avg(0), fom_std(0), fom_max(0);
244 Vector3<long> ve(p->size()/2);
245 ve = ve.min(edge);
246
247 if ( verbose & VERB_LABEL ) {
248 cout << "Finding particles:" << endl;
249 cout << "Particle radius: " << radius*p->sampling()[0]
250 << " A " << radius << " pixels" << endl;
251 cout << "Edge: " << edge << " pixels" << endl;
252 }
253
254 if ( verbose & VERB_DEBUG )
255 cout << "DEBUG img_find_gold_particles: radius=" << radius << endl;
256
257 Bimage* pone = NULL;
258 Bimage* pcc = NULL;
259
260 long imgsize(p->sizeX()*p->sizeY()*p->sizeZ());
261 long fomsize(imgsize);
262 if ( img_select < 0 ) fomsize *= p->images();
263 float* fom = new float[fomsize];
264 for ( i=0; i<fomsize; i++ ) fom[i] = 0;
265
266 // if ( verbose & VERB_PROCESS )
267 // cout << "Image\tx\ty\tz" << endl;
268 for ( i=0, n=img_start; n<img_end; n++ ) { // i is the external counter
269 pone = p->extract(n);
270 pcc = pone->cross_correlate(pgold, 2*p->sampling()[0], 8*radius*p->sampling()[0]);
271 delete pone;
272 pone = pcc->find_peaks(3);
273 delete pcc;
274 for ( j=0; j<imgsize; j++, i++ ) if ( (*pone)[j] ) {
275 fom[i] = (*pone)[j];
276 fom_avg += fom[i];
277 fom_std += fom[i]*fom[i];
278 if ( fom_max < fom[i] ) fom_max = fom[i];
279 np++;
280 }
281 delete pone;
282 if ( verbose & VERB_PROCESS )
283 cout << "Image " << n << ": " << np << " gold fiducials" << endl;
284 }
285
286 fom_avg /= np;
287 fom_std = fom_std/np - fom_avg*fom_avg;
288 if ( fom_std > 0 ) fom_std = sqrt(fom_std);
289 else fom_std = 0;
290 if ( cutoff == 0 ) cutoff = fom_avg + fom_std;
291 else if ( cutoff < 0 ) cutoff = fom_max/2;
292 if ( verbose )
293 cout << "Cutoff = " << cutoff << endl;
294
295 Bmarker* mark = NULL;
296 Bmarker* m = NULL;
297
298 if ( np ) {
299 if ( verbose )
300 cout << "Marker\tx\ty\tz\tn\tFOM" << endl;
301 for ( j=nn=0, n=img_start; n<img_end; n++, nn++ ) {
302 for ( z=0; z<p->sizeZ(); z++ ) {
303 zz = (long)z - pgold->image->origin()[2];
304 if ( zz < 0 ) zz += p->sizeZ();
305 if ( zz >= ve[2] && zz < p->sizeZ()-ve[2] )
306 for ( y=0; y<p->sizeY(); y++ ) {
307 yy = (long)y - pgold->image->origin()[1];
308 if ( yy < 0 ) yy += p->sizeY();
309 if ( yy >= ve[1] && yy < p->sizeY()-ve[1] )
310 for ( x=0; x<p->sizeX(); x++ ) {
311 xx = (long)x - pgold->image->origin()[0];
312 if ( xx < 0 ) xx += p->sizeX();
313 if ( xx >= ve[0] && xx < p->sizeX()-ve[0] ) {
314 i = p->index(x, y, z, nn);
315 if ( fom[i] > cutoff ) {
316 m = (Bmarker *) add_item((char **) &m, sizeof(Bmarker));
317 if ( !mark ) mark = m;
318 j++;
319 m->id = j;
320 m->loc[0] = x - pgold->image->origin()[0];
321 m->loc[1] = y - pgold->image->origin()[1];
322 m->loc[2] = z - pgold->image->origin()[2];
323 m->img_num = n;
324 m->sel = 1;
325 m->fom = fom[i];
326 if ( m->loc[0] < 0 ) m->loc[0] += p->sizeX();
327 if ( m->loc[1] < 0 ) m->loc[1] += p->sizeY();
328 if ( m->loc[2] < 0 ) m->loc[2] += p->sizeZ();
329 if ( verbose )
330 cout << m->id << tab << m->loc[0] << tab <<
331 m->loc[1] << tab << m->loc[2] << tab <<
332 m->img_num << tab << m->fom << endl;
333 }
334 }
335 }
336 }
337 }
338 }
339 }
340
341 delete pgold;
342
343 delete[] fom;
344
345 if ( verbose & VERB_LABEL )
346 cout << "Number of particles found: " << j << endl << endl;
347
348 return mark;
349 }
350
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, string& label)
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 p->label(label);
435 write_img(normname, p, 0);
436 delete p;
437 }
438 oldname = mg->fmg;
439 mg->fmg = normname;
440 }
441 }
442
443 return 0;
444 }
445
446 double lambda_R(Bsimplex& simp)
447 {
448 long i;
449 double R(0), df;
450 vector<double>& f = simp.dependent_values();
451 vector<double>& x = simp.independent_values();
452
453 for ( i=0; i<simp.points(); i++ ) {
454 df = f[i] - simp.parameter(0)*exp(-simp.parameter(1)/cos(x[i] + simp.parameter(2)));
455 R += df*df;
456 }
457
458 R = sqrt(R/i);
459
460 return R;
461 }
462
463 vector<double> simplex_fit_tilt_intensities(long n, vector<double>& a, vector<double>& I)
464 {
465 long i;
466 double Imax(0);
467
468 for ( i=0; i<n; ++i ) if ( Imax < I[i] ) Imax = I[i];
469
470 Bsimplex simp(1, 3, 0, n, a, I);
471
472 simp.parameter(0, 2*Imax);
473 simp.parameter(1, 0.6);
474 simp.parameter(2, -0.01);
475 simp.limits(0, 0.9*Imax, 5*Imax);
476 simp.limits(1, 0.01, 10);
477 simp.limits(2, -0.5, 0.5);
478
479 long maxiter(10);
480 double R = simp.run(10000, 0.0001, lambda_R);
481 for ( i=0; i<maxiter && ( !isfinite(R) || R > 1 ); ++i ) {
482 simp.parameter(0, 1.5*Imax);
483 simp.parameter(1, 0.7);
484 simp.parameter(2, 0.01);
485 R = simp.run(10000, 0.0001, lambda_R);
486 }
487
488 vector<double> result;
489 for ( i=0; i<3; ++i )
490 result.push_back(simp.parameter(i));
491 result.push_back(R);
492
493 return result;
494 }
495
504 double project_thickness_to_lambda_ratio(Bproject* project)
505 {
506 if ( !project->field ) return -1;
507
508 long i;
509 Bfield* field = project->field;
510 Bmicrograph* mg = NULL;
511 Bimage* p = NULL;
512
513 long nmg = field_count_mg_selected(field);
514 vector<double> a(nmg), I(nmg);
515
516 // cout << "nmg=" << nmg << endl;
517
518 if ( verbose )
519 cout << endl << "Tilt\tIntensity" << endl;
520 // for ( field = project->field; field; field = field->next ) {
521 for ( mg = field->mg; mg; mg = mg->next ) {
522 if ( mg->dose < 1e-10 ) {
523 if ( mg->fmg.length() ) {
524 p = read_img(mg->fmg, 0, mg->img_num);
525 // cout << mg->img_num << tab << p->image->average() << endl;
526 if ( fabs(p->image->average()) < 1e-20 ) {
527 delete p;
528 p = read_img(mg->fmg, 1, mg->img_num);
529 p->statistics();
530 // cout << mg->img_num << tab << p->image->average() << endl;
531 }
532 } else if ( mg->fframe.length() ) {
533 p = read_img(mg->fframe, 0, mg->img_num);
534 if ( fabs(p->image->average()) < 1e-20 ) {
535 delete p;
536 p = read_img(mg->fframe, 1, mg->img_num);
537 p->statistics();
538 }
539 }
540 if ( !p ) {
541 error_show("project_thickness_to_lambda_ratio", __FILE__, __LINE__);
542 return -1;
543 } else {
544 mg->dose = p->image->average();
545 delete p;
546 }
547 }
548 if ( mg->dose < 0.1 ) mg->select = 0;
549 }
550
551 for ( i=0, mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
552 a[i] = mg->tilt_angle;
553 I[i] = mg->dose;
554 if ( verbose )
555 cout << a[i]*180.0/M_PI << tab << I[i] << endl;
556 i++;
557 }
558 // }
559
560 nmg = i;
561
562 if ( nmg < 3 ) {
563 if ( verbose )
564 cerr << "Too few micrographs to determine the thickness!" << endl;
565 return 0;
566 }
567
568 vector<double> result = simplex_fit_tilt_intensities(nmg, a, I);
569
570 double I0 = result[0];
571 double tlr = result[1];
572 double da = result[2];
573 double R = result[3];
574
575 for ( mg = field->mg; mg; mg = mg->next )
576 mg->tilt_angle += da;
577
578 if ( verbose ) {
579 cout << endl << "Calculating tilt intensity parameters:" << endl;
580 cout << "Residual: " << R << endl;
581 cout << "Direct beam intensity: " << I0 << endl;
582 cout << "Specimen tilt: " << da*180.0/M_PI << " degrees" << endl;
583 cout << "Thickness to lambda ratio: " << tlr << endl << endl;
584 }
585
586 return tlr;
587 }
588
598 double project_lambda(Bproject* project, double thickness)
599 {
600 if ( !project->field ) return -1;
601
602 double tlr = project_thickness_to_lambda_ratio(project);
603
604 double lambda = thickness/tlr;
605
606 if ( verbose ) {
607 cout << "Thickness: " << thickness << " A" << endl;
608 cout << "Proportionality parameter: " << lambda << " A" << endl << endl;
609 }
610
611 return lambda;
612 }
613
623 double project_thickness(Bproject* project, double lambda)
624 {
625 if ( !project->field ) return -1;
626
627 double tlr = project_thickness_to_lambda_ratio(project);
628
629 double thickness = lambda*tlr;
630
631 if ( verbose ) {
632 cout << "Thickness: " << thickness << " A" << endl << endl;
633 cout << "Proportionality parameter: " << lambda << " A" << endl << endl;
634 }
635
636 return lambda;
637 }
638
647 int project_sort_markers_by_id(Bproject* project)
648 {
649 Bfield* field;
650 Bmicrograph* mg;
651
652 if ( verbose )
653 cout << "Sorting markers" << endl << endl;
654
655 for ( field = project->field; field; field = field->next )
656 for ( mg = field->mg; mg; mg = mg->next )
657 markers_sort_by_id(&mg->mark);
658
659 return 0;
660 }
661
671 double project_tomo_residuals(Bproject* project, int show)
672 {
673 if ( project_count_markers(project) < 1 ) return 0;
674
675 Bmarker* m, *mr;
676 Bfield* field = NULL;
677 Bmicrograph* mg;
678 Bmicrograph* mg_ref = NULL;
679 Breconstruction* rec = project->rec;
680 Vector3<double> loc, shift, tilt_axis;
681 Quaternion q;
682 Transform t;
683 Matrix3 mat, mat2;
684 double d(0), dsum(0), dmgsum, w(0), wmg, mgfom, avg_axis(0), wa, wax(0);
685
686 for ( field = project->field; field && !mg_ref; field = field->next )
687 if ( field->select ) mg_ref = field_find_zero_tilt_mg(field);
688
689 // long nmg = count_list((char *) field->mg);
690
691 if ( mg_ref->origin[0] < 1 || mg_ref->origin[1] < 1 )
692 project_set_nominal_mg_origins(project);
693
694 if ( mg_ref->matrix.determinant() < 0.5 )
695 project_mg_tilt_to_matrix(project);
696
697 Bstring id("1");
698 if ( !project->rec ) {
699 rec = reconstruction_add(&project->rec, id);
700 rec->origin = mg_ref->origin;
701 }
702
703 if ( !rec->mark )
704 project_calculate_model(project);
705
706 for ( mr = rec->mark; mr; mr = mr->next ) {
707 mr->err = 0;
708 mr->res = 0;
709 mr->sel = 0;
710 }
711
712 if ( verbose && show ) {
713 cout << "Tomography alignment results:" << endl;
714 cout << "Mg\tShiftX\tShiftY\tScaleX\tScaleY\tAxis\tTilt\tLevel\tn\tResid\tFOM" << endl;
715 }
716
717 for ( field = project->field; field; field = field->next ) if ( field->select ) {
718 /* if ( field != project->field ) {
719 mg_ref2 = field_find_zero_tilt_mg(field);
720 if ( mg_ref2->mark ) {
721 t = marker_find_transform(mg_ref2->mark, mg_ref->mark, rec->origin);
722 // cout << "axis=" << t.axis*180/M_PI << tab << "angle=" << t.angle*180/M_PI << endl;
723 field->matrix = Matrix3(t.axis, t.angle);
724 }
725 } else field->matrix = Matrix3(1);*/
726 for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
727 shift = mg->origin - mg_ref->origin;
728 dmgsum = wmg = mgfom = 0;
729 // mat = mg->matrix.transpose(); // Handedness
730 // mat = mg->matrix * field->matrix;
731 for ( mr = rec->mark; mr; mr = mr->next ) {
732 for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
733 if ( m ) {
734 // loc = mg_location_from_3D_model(mr->loc, rec->origin, mat, mg->origin, mg->scale*rec->scale);
735 loc = mg_location_from_3D_model(mr->loc, rec->origin, mg->matrix, mg->origin, mg->scale*rec->scale);
736 m->err = loc - m->loc;
737 d = m->err.length2();
738 if ( m->sel > 0 ) {
739 dmgsum += d;
740 mgfom += m->fom;
741 mr->err += m->err;
742 mr->res += d;
743 mr->sel += 1;
744 wmg += 1;
745 }
746 d = sqrt(d);
747 m->res = d;
748 if ( ( verbose & VERB_FULL ) && show )
749 cout << mg->id << tab << m->id << tab << loc[0] << tab << loc[1] << tab << loc[2] << tab << d << endl;
750 }
751 }
752 dsum += dmgsum;
753 w += wmg;
754 if ( wmg ) {
755 mg->fom = sqrt(dmgsum/wmg);
756 mgfom /= wmg;
757 }
758 wa = 1 - exp(-mg->tilt_angle*mg->tilt_angle/0.5);
759 if ( fabs(mg->tilt_angle) > M_PI/45.0 ) {
760 avg_axis += wa*mg->tilt_axis;
761 wax += wa;
762 }
763 if ( verbose && show )
764 cout << mg->id << tab << fixed << setprecision(2) << shift[0] << tab << shift[1] << tab
765 << setprecision(4) << mg->scale[0] << tab << mg->scale[1] << tab
766 << mg->tilt_axis*180.0/M_PI << tab
767 << mg->tilt_angle*180.0/M_PI << tab
768 << mg->level_angle*180.0/M_PI << tab
769 << wmg << tab << mg->fom << tab << mgfom << endl;
770 }
771 }
772
773 if ( wax ) avg_axis /= wax;
774 if ( w ) d = sqrt(dsum/w);
775
776 // Fix tilt axis angles - particularly for the zero-tilt micrograph
777 // for ( field = project->field; field; field = field->next ) if ( field->select )
778 // for ( mg = field->mg; mg; mg = mg->next )
779 // if ( fabs(mg->tilt_axis - avg_axis) > M_PI/90.0 ) mg->tilt_axis = avg_axis;
780
781 for ( mr = rec->mark; mr; mr = mr->next ) {
782 if ( mr->sel ) mr->err /= mr->sel;
783 mr->res = sqrt(mr->res/mr->sel);
784 }
785
786 if ( verbose && show ) {
787 cout << "Average tilt axis angle: " << avg_axis*180.0/M_PI << endl << endl;
788 cout << "Marker errors:\nMarker\tn\tx\ty\tResid\tFOM" << endl;
789 for ( mr = rec->mark; mr; mr = mr->next )
790 cout << mr->id << tab << mr->sel << tab <<
791 setprecision(3) << mr->err[0] << tab << mr->err[1] << tab <<
792 mr->res << tab << setprecision(4) << mr->fom << endl;
793 cout << "Average residual = " << d << " pixels" << endl << endl;
794 }
795
796 return d;
797 }
798
809 double mg_tomo_residuals(Bmicrograph* mg, Bmarker* model, Vector3<double> oriref)
810 {
811 Bmarker* m, *mr;
812 Vector3<double> loc;
813 double d(0), dsum(0), w(0);
814
815 for ( mr = model; mr; mr = mr->next ) {
816 for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
817 if ( m ) {
818 loc = mg_location_from_3D_model(mr->loc, oriref, mg->matrix, mg->origin, mg->scale);
819 m->err = loc - m->loc;
820 d = m->err.length2();
821 if ( m->sel > 0 ) {
822 dsum += d;
823 w += 1;
824 }
825 d = sqrt(d);
826 m->res = d;
827 }
828 }
829
830 if ( w ) d = sqrt(dsum/w);
831
832 return d;
833 }
834
843 double project_tomo_errors(Bproject* project)
844 {
845 Bfield* field = project->field;
846 Bmicrograph* mg;
847 Bmarker *m1, *m2;
848
849 int i, n(0);
850 double scale = 10;
851 int h[1000];
852 for ( i=0; i<1000; i++ ) h[i] = 0;
853
854 for ( field = project->field; field; field = field->next ) if ( field->select ) {
855 for ( mg = field->mg; mg->next; mg = mg->next ) {
856 for ( m1=mg->mark, m2=mg->next->mark; m1 && m2; m1=m1->next, m2=m2->next ) {
857 i = (int) (scale*(m1->err - m2->err).length() + 0.5);
858 if ( i < 1000 ) {
859 h[i]++;
860 if ( n < i ) n = i;
861 }
862 }
863 }
864 }
865
866 if ( verbose ) {
867 cout << "Histogram of marker refinement shifts:" << endl;
868 cout << "Shift\tCount" << endl;
869 for ( i=0; i<=n; i++ )
870 cout << setprecision(2) << i/scale << tab << h[i] << endl;
871 cout << endl;
872 }
873
874 return 0;
875 }
876
886 int project_calculate_model(Bproject* project)
887 {
888 if ( project_count_markers(project) < 1 ) return 0;
889
890 int nm;
891 Bfield* field;
892 Bmicrograph* mg;
893 Bmarker *m, *mr;
894 Vector3<double> t;
895 Bmicrograph* mg_ref = NULL;
896 Breconstruction* rec = project->rec;
897
898 for ( field = project->field; field && !mg_ref; field = field->next )
899 if ( field->select ) mg_ref = field_find_zero_tilt_mg(field);
900
901 if ( !mg_ref->mark )
902 for ( mg_ref = project->field->mg; mg_ref && !mg_ref->mark; mg_ref = mg_ref->next ) ;
903
904 if ( !mg_ref )
905 return error_show("project_calculate_model: No markers defined!", __FILE__, __LINE__);
906
907 Bstring id("1");
908 if ( !project->rec ) {
909 rec = reconstruction_add(&project->rec, id);
910 rec->origin = mg_ref->origin;
911 rec->mark_radius = mg_ref->mark_radius;
912 }
913
914 if ( verbose & VERB_FULL )
915 cout << "Calculating a 3D marker model" << endl << endl;
916
917 // Set 3D model if not already present
918 if ( !rec->mark )
919 rec->mark = markers_copy(mg_ref->mark);
920
921 // Fill in extra markers
922 for ( field = project->field; field; field = field->next ) if ( field->select ) {
923 for ( mg = field->mg; mg; mg = mg->next ) {
924 for ( m = mg->mark; m; m = m->next ) {
925 for ( mr = rec->mark; mr && mr->id != m->id; mr = mr->next ) ;
926 if ( !mr ) {
927 mr = (Bmarker *) add_item((char **) &rec->mark, sizeof(Bmarker));
928 if ( !rec->mark ) rec->mark = mr;
929 mr->id = m->id;
930 mr->loc = m->loc;
931 mr->res = m->res;
932 mr->fom = m->fom;
933 mr->sel = m->sel;
934 }
935 }
936 }
937 }
938
939 // Calculate the micrograph origins
940 for ( field = project->field; field; field = field->next ) if ( field->select ) {
941 for ( mg = field->mg; mg; mg = mg->next ) {
942 t = 0;
943 nm = 0;
944 for ( mr = rec->mark; mr; mr = mr->next ) {
945 for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
946 if ( m ) {
947 t += m->loc - mg_location_from_3D_model(mr->loc, rec->origin, mg->matrix, mg->origin);
948 nm++;
949 }
950 }
951 if ( nm ) {
952 t /= nm;
953 mg->origin += t;
954 }
955 }
956 }
957
958 // Calculate the z-coordinates
959 for ( mr = rec->mark; mr; mr = mr->next ) {
960 mr->loc[2] = 0;
961 mr->sel = 0;
962 }
963 for ( field = project->field; field; field = field->next ) if ( field->select ) {
964 for ( mg = field->mg; mg; mg = mg->next ) {
965 for ( mr = rec->mark; mr; mr = mr->next ) {
966 for ( m = mg->mark; m && m->id != mr->id; m = m->next ) ;
967 if ( m ) {
968 t = mr->loc - rec->origin;
969 /* if ( mg->matrix[6] ) {
970 mr->loc[2] += (m->loc[0] - mg->origin[0] - mg->matrix[0]*t[0] - mg->matrix[3]*t[1])/mg->matrix[6];
971 mr->sel++;
972 }
973 if ( mg->matrix[7] ) {
974 mr->loc[2] += (m->loc[1] - mg->origin[1] - mg->matrix[1]*t[0] - mg->matrix[4]*t[1])/mg->matrix[7];
975 mr->sel++;
976 }*/
977 if ( mg->matrix[2][0] ) {
978 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];
979 mr->sel++;
980 }
981 if ( mg->matrix[2][1] ) {
982 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];
983 mr->sel++;
984 }
985 }
986 }
987 }
988 }
989 for ( mr = rec->mark; mr; mr = mr->next ) {
990 if ( mr->sel ) mr->loc[2] /= mr->sel;
991 mr->sel = 1;
992 }
993
994 return 0;
995 }
996
1007 int project_generate_markers(Bproject* project)
1008 {
1009 if ( !project->rec ) return -1;
1010
1011 Bfield* field = project->field;
1012 Bmicrograph* mg;
1013
1014 if ( verbose )
1015 cout << "Generating missing markers" << endl << endl;
1016
1017 for ( field = project->field; field; field = field->next ) if ( field->select )
1018 for ( mg = field->mg; mg; mg = mg->next )
1019 mg_update_model(mg, project->rec->mark, project->rec->origin);
1020
1021 for ( field = project->field; field; field = field->next ) if ( field->select )
1022 for ( mg = field->mg; mg; mg = mg->next )
1023 mg_marker_update(mg, project->rec->mark, project->rec->origin, 0);
1024
1025 return 0;
1026 }
1027
1038 Bimage* mg_erase_markers(Bmicrograph* mg, double marker_radius)
1039 {
1040 if ( verbose & VERB_FULL )
1041 cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
1042
1043 Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1044 if ( !p ) {
1045 error_show("mg_erase_markers", __FILE__, __LINE__);
1046 return NULL;
1047 }
1048
1049 p->change_type(Float);
1050
1051 p->sampling(mg->pixel_size, mg->pixel_size, 1);
1052
1053 p->origin(mg->origin[0], mg->origin[1], 0);
1054
1055 if ( marker_radius < 1 ) return p;
1056
1057 Bmarker* mark = NULL;
1058
1059 if ( marker_radius < mg->mark_radius ) marker_radius = 1.5*mg->mark_radius;
1060
1061 double fill, fstd;
1062 for ( mark = mg->mark; mark; mark = mark->next ) {
1063 p->stats_within_radii(0, mark->loc, marker_radius, 2*marker_radius, fill, fstd);
1064 p->sphere(mark->loc, marker_radius, 2, FILL_USER, fill);
1065 }
1066
1067 return p;
1068 }
1069
1080 int project_erase_markers(Bproject* project, double marker_radius)
1081 {
1082 long n, nmg = project_count_micrographs(project);
1083
1084 if ( nmg < 1 ) return -1;
1085
1086 Bstring filename;
1087 Bfield* field = project->field;
1088 Bmicrograph* mg = field->mg;
1089 Bimage* p;
1090 Bimage* pall = NULL;
1091
1092 if ( verbose )
1093 cout << "Removing markers with radius " << marker_radius << endl << endl;
1094
1095 for ( field = project->field; field; field = field->next ) {
1096 nmg = field_count_micrographs(field);
1097 p = read_img(mg->fmg, 0, 0);
1098 if ( !p ) {
1099 error_show("project_erase_markers", __FILE__, __LINE__);
1100 return -1;
1101 }
1102 pall = new Bimage(p->data_type(), p->compound_type(), p->size(), nmg);
1103 delete p;
1104 if ( field->mg->pixel_size ) pall->sampling(mg->pixel_size, mg->pixel_size, 1);
1105 filename = field->mg->fmg.pre_rev('.') + "_me." + field->mg->fmg.post_rev('.');
1106 for ( n=0, mg = field->mg; mg; mg = mg->next, n++ ) {
1107 if ( verbose )
1108 cout << "Reading image " << mg->img_num << " (micrograph " << mg->id << ")" << endl;
1109 p = mg_erase_markers(mg, marker_radius);
1110 if ( !p ) {
1111 error_show("project_remove_markers", __FILE__, __LINE__);
1112 return -1;
1113 }
1114 pall->replace(n, p);
1115 pall->image[n] = p->image[0];
1116 delete p;
1117 mg->fmg = filename;
1118 }
1119 write_img(filename, pall, 0);
1120 delete pall;
1121 }
1122
1123 return 0;
1124 }
1125
1135 int mg_reset_model(Bmicrograph* mg, Bmarker* model)
1136 {
1137 if ( !mg->mark ) return 0;
1138
1139 int n(0), ns(0);
1140 Bmarker *m, *mr;
1141
1142 if ( verbose ) {
1143 cout << "Resetting 3D model x and y coordinates:" << endl;
1144 cout << "Micrograph: " << mg->id << endl << endl;
1145 }
1146
1147 for ( mr = model; mr; mr = mr->next ) mr->sel = 0;
1148
1149 for ( m = mg->mark; m; m = m->next ) {
1150 for ( mr = model; mr && m->id != mr->id; mr = mr->next ) ;
1151 if ( !mr ) {
1152 mr = (Bmarker *) add_item((char **) &model, sizeof(Bmarker));
1153 mr->id = m->id;
1154 }
1155 mr->loc[0] = m->loc[0];
1156 mr->loc[1] = m->loc[1];
1157 mr->sel = 1;
1158 }
1159
1160 for ( mr = model; mr; mr = mr->next, n++ ) ns += mr->sel;
1161
1162 if ( verbose )
1163 cout << "Markers in model: " << n << " (" << ns << ")" << endl << endl;
1164
1165 // markers_delete_non_selected(&model);
1166
1167 return ns;
1168 }
1169
1180 long project_find_markers(Bproject* project, long edge, int add)
1181 {
1182 Bfield* field = NULL;
1183 Bmicrograph* mg = NULL;
1184
1185 for ( field = project->field; field; field = field->next )
1186 for ( mg = field->mg; mg; mg = mg->next )
1187 mg_find_markers(mg, edge, add);
1188
1189 return project_count_markers(project);
1190 }
1191
1205 long mg_find_markers(Bmicrograph* mg, long edge, int add)
1206 {
1207 if ( mg->mark_radius < 1 ) {
1208 cerr << "Error: The marker radius is too low! (" << mg->mark_radius << ")" << endl;
1209 bexit(-1);
1210 }
1211
1212 Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1213 if ( !p )
1214 return error_show("mg_find_markers", __FILE__, __LINE__);
1215
1216 Bmarker* mark = img_find_gold_particles(p, 0, mg->mark_radius, edge, -1);
1217
1218 delete p;
1219
1220 long nmark = count_list((char *) mark);
1221
1222 if ( nmark ) {
1223 if ( add ) {
1224 markers_add(&mg->mark, mark, 2*mg->mark_radius, 2);
1225 } else {
1226 kill_list((char *) mg->mark, sizeof(Bmarker));
1227 mg->mark = mark;
1228 }
1229 }
1230
1231 return nmark;
1232 }
1233
1240 Bimage* mg_composite_particle(Bmicrograph* mg, Vector3<long> size)
1241 {
1242 if ( !mg->mark ) {
1243 error_show("No markers found!", __FILE__, __LINE__);
1244 return NULL;
1245 }
1246
1247 int n;
1248
1249 Bmarker* mark;
1250 Bimage* pmark;
1251
1252 if ( verbose & VERB_DEBUG )
1253 cout << "DEBUG mg_composite_particle: size = " << size << endl;
1254
1255 Bimage* p = read_img(mg->fmg, 1, mg->img_num);
1256 if ( !p ) {
1257 error_show("mg_composite_particle", __FILE__, __LINE__);
1258 return NULL;
1259 }
1260
1261 Bimage* pavg = new Bimage(Float, TSimple, size, 1);
1262 pavg->origin(pavg->size()/2);
1263
1264 Vector3<double> ghalf(pavg->image->origin());
1265 Vector3<double> gorigin;
1266
1267 for ( n=0, mark = mg->mark; mark; mark = mark->next, n++ ) if ( mark->sel > 0 ) {
1268 gorigin = mark->loc - ghalf;
1269 pmark = p->extract(0, gorigin, size);
1270 pmark->change_type(Float);
1271 pavg->add(pmark);
1272 delete pmark;
1273 }
1274
1275 delete p;
1276
1277 pavg->calculate_background();
1278
1279 pavg->symmetrize_cylinder();
1280
1281 pavg->statistics();
1282
1283 pavg->calculate_background();
1284
1285 return pavg;
1286 }
1287
1293 long project_count_markers(Bproject* project)
1294 {
1295 if ( !project ) return 0;
1296 if ( !project->field ) return 0;
1297 if ( !project->field->mg ) return 0;
1298
1299 long nmark(0);
1300 Bfield* field;
1301 Bmicrograph* mg;
1302 Breconstruction* rec;
1303
1304 if ( project->select < 1 ) {
1305 for ( field = project->field; field; field = field->next )
1306 for ( mg = field->mg; mg; mg = mg->next )
1307 nmark += count_list((char *) mg->mark);
1308 } else {
1309 for ( rec = project->rec; rec; rec = rec->next )
1310 nmark += count_list((char *) rec->mark);
1311 }
1312
1313 return nmark;
1314 }
1315
1321 long project_show_markers(Bproject* project)
1322 {
1323 if ( !project ) return 0;
1324 if ( !project->field ) return 0;
1325 if ( !project->field->mg ) return 0;
1326
1327 long i, nmark(0);
1328 Bfield* field;
1329 Bmicrograph* mg;
1330
1331 cout << "Project markers:" << endl;
1332 for ( field = project->field; field; field = field->next ) {
1333 cout << "Field: " << field->id << endl;
1334 for ( mg = field->mg; mg; mg = mg->next ) {
1335 i = count_list((char *) mg->mark);
1336 nmark += i;
1337 cout << "\tMicrograph: " << mg->id << tab << i << " markers\tselection = " << mg->select << endl;
1338 }
1339 }
1340 cout << "Total:\t" << nmark << " markers" << endl << endl;
1341
1342 return nmark;
1343 }
1344
1351 long project_show_errors(Bproject* project, double error_cutoff)
1352 {
1353 if ( !verbose ) return 0;
1354 if ( !project ) return 0;
1355
1356 long nmark(0), nerr(0);
1357 Bfield* field = project->field;
1358 Bmicrograph* mg;
1359 Bmarker* m;
1360
1361 cout << "Project marker errors:" << endl;
1362 cout << "Micrograph\tMarker\tx\ty\tex\tey\ted" << endl;
1363 for ( field = project->field; field; field = field->next ) if ( field->select ) {
1364 for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1365 for ( m = mg->mark; m; m = m->next ) if ( m->sel > 0 ) {
1366 if ( m->res > error_cutoff ) {
1367 cout << mg->id << tab << m->id << tab << m->loc[0] << tab <<
1368 m->loc[1] << tab << m->err[0] << tab << m->err[1] << tab << m->res << endl;
1369 nerr++;
1370 }
1371 nmark++;
1372 }
1373 }
1374 }
1375 cout << "Total above " << error_cutoff << ":\t" << nerr << " markers (" << nerr*100.0/nmark << "%)" << endl << endl;
1376
1377 return nmark;
1378 }
1379
1389 long project_deselect_markers(Bproject* project, Bstring& deselect_list)
1390 {
1391 if ( !project ) return 0;
1392
1393 long maxid;
1394 Bfield* field;
1395 Bmicrograph* mg;
1396 Breconstruction* rec;
1397 Bmarker* mark;
1398
1399 for ( maxid=0, field = project->field; field; field = field->next )
1400 for ( mg = field->mg; mg; mg = mg->next )
1401 for ( mark = mg->mark; mark; mark = mark->next )
1402 if ( maxid < mark->id ) maxid = mark->id;
1403
1404 for ( rec = project->rec; rec; rec = rec->next )
1405 for ( mark = rec->mark; mark; mark = mark->next )
1406 if ( maxid < mark->id ) maxid = mark->id;
1407
1408 maxid++;
1409
1410 int* sel = new int[maxid];
1411
1412 select_numbers(deselect_list, maxid, sel);
1413
1414 if ( verbose )
1415 cout << "Deselecting markers" << endl << endl;
1416 for ( field = project->field; field; field = field->next ) {
1417 for ( mg = field->mg; mg; mg = mg->next ) {
1418 for ( mark = mg->mark; mark; mark = mark->next )
1419 if ( sel[mark->id] ) mark->sel = 0;
1420 }
1421 }
1422
1423 for ( rec = project->rec; rec; rec = rec->next ) {
1424 for ( mark = rec->mark; mark; mark = mark->next ) {
1425 if ( sel[mark->id] ) mark->sel = 0;
1426 }
1427 }
1428
1429 delete[] sel;
1430
1431 long n = count_list((char *) project->field->mg->mark);
1432
1433 return n;
1434 }
1435
1445 long project_delete_markers(Bproject* project, Bstring& delete_list)
1446 {
1447 if ( !project ) return 0;
1448
1449 long maxid;
1450 Bfield* field;
1451 Bmicrograph* mg;
1452 Breconstruction* rec;
1453 Bmarker* mark;
1454
1455 for ( maxid=0, field = project->field; field; field = field->next )
1456 for ( mg = field->mg; mg; mg = mg->next )
1457 for ( mark = mg->mark; mark; mark = mark->next )
1458 if ( maxid < mark->id ) maxid = mark->id;
1459
1460 maxid++;
1461
1462 int* sel = new int[maxid];
1463
1464 select_numbers(delete_list, maxid, sel);
1465
1466 if ( verbose )
1467 cout << "Deleting markers:" << endl;
1468 for ( field = project->field; field; field = field->next ) {
1469 if ( verbose )
1470 cout << "Field: " << field->id << ":" << endl;
1471 for ( mg = field->mg; mg; mg = mg->next ) {
1472 if ( verbose )
1473 cout << "\tMicrograph: " << mg->id << endl;
1474 for ( mark = mg->mark; mark; ) {
1475 if ( sel[mark->id] ) {
1476 if ( verbose )
1477 cout << "\t\tDeleting marker: " << mark->id << endl;
1478 mark = (Bmarker *) remove_item((char **)&mg->mark, (char *)mark, sizeof(Bmarker));
1479 } else {
1480 mark = mark->next;
1481 }
1482 }
1483 }
1484 }
1485
1486 for ( rec = project->rec; rec; rec = rec->next ) {
1487 if ( verbose )
1488 cout << "Reconstruction: " << rec->id << endl;
1489 for ( mark = rec->mark; mark; ) {
1490 if ( sel[mark->id] ) {
1491 if ( verbose )
1492 cout << "\t\tDeleting marker: " << mark->id << endl;
1493 mark = (Bmarker *) remove_item((char **)&rec->mark, (char *)mark, sizeof(Bmarker));
1494 } else {
1495 mark = mark->next;
1496 }
1497 }
1498 }
1499
1500 delete[] sel;
1501
1502 long n = count_list((char *) project->field->mg->mark);
1503
1504 return n;
1505 }
1506
1516 long project_renumber_markers(Bproject* project)
1517 {
1518 if ( !project ) return 0;
1519
1520 long maxid, i, j;
1521 Bfield* field;
1522 Bmicrograph* mg;
1523 Breconstruction* rec;
1524 Bmarker* mark;
1525
1526 // Find maximum marker id
1527 for ( maxid=0, field = project->field; field; field = field->next )
1528 for ( mg = field->mg; mg; mg = mg->next )
1529 for ( mark = mg->mark; mark; mark = mark->next )
1530 if ( maxid < mark->id ) maxid = mark->id;
1531
1532 for ( rec = project->rec; rec; rec = rec->next )
1533 for ( mark = rec->mark; mark; mark = mark->next );
1534 if ( maxid < mark->id ) maxid = mark->id;
1535
1536 if ( verbose & VERB_DEBUG )
1537 cout << "DEBUG project_renumber_markers: maxid=" << maxid << endl;
1538
1539 maxid++;
1540
1541 // Establish all existing marker ids
1542 int* id = new int[maxid];
1543 for ( i=0; i<maxid; i++ ) id[i] = 0;
1544
1545 for ( field = project->field; field; field = field->next )
1546 for ( mg = field->mg; mg; mg = mg->next )
1547 for ( mark = mg->mark; mark; mark = mark->next )
1548 id[mark->id]++;
1549
1550 for ( rec = project->rec; rec; rec = rec->next )
1551 for ( mark = rec->mark; mark; mark = mark->next );
1552 id[mark->id]++;
1553
1554 // Create a mapping from old to new ids
1555 for ( i=j=0; i<maxid; i++ )
1556 if ( id[i] ) id[i] = ++j;
1557
1558 if ( verbose & VERB_DEBUG )
1559 cout << "DEBUG project_renumber_markers: new maxid=" << j << endl;
1560
1561 for ( field = project->field; field; field = field->next )
1562 for ( mg = field->mg; mg; mg = mg->next )
1563 for ( mark = mg->mark; mark; mark = mark->next )
1564 mark->id = id[mark->id];
1565
1566 for ( rec = project->rec; rec; rec = rec->next )
1567 for ( mark = rec->mark; mark; mark = mark->next );
1568 mark->id = id[mark->id];
1569
1570 delete[] id;
1571
1572 return j;
1573 }
1574
1585 long project_mg_select(Bproject* project, Bstring& mg_select)
1586 {
1587 if ( !project ) return 0;
1588 if ( mg_select.length() < 1 ) return 0;
1589
1590 long n(0);
1591 Bfield* field = project->field;
1592 Bmicrograph* mg;
1593
1594 vector<long> en = mg_select.split_into_integers(",");
1595
1596 if ( verbose & VERB_PROCESS )
1597 cout << "Selecting micrographs:" << endl;
1598 for ( field = project->field; field; field = field->next ) if ( field->select ) {
1599 for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1600 mg->select = 0;
1601 for ( size_t i=0; i<en.size(); i++ ) {
1602 if ( mg->img_num == en[i] ) {
1603 mg->select = 1;
1604 n++;
1605 if ( verbose & VERB_PROCESS )
1606 cout << "Micrograph " << mg->id << " (" << mg->img_num << ") selected" << endl;
1607 }
1608 }
1609 }
1610 }
1611 if ( verbose & VERB_PROCESS )
1612 cout << "Total Selected: " << n << endl << endl;
1613
1614 return n;
1615 }
1616
1623 long project_mg_exclude(Bproject* project, Bstring& mg_exclude)
1624 {
1625 if ( !project ) return 0;
1626
1627 long i, nmg(0), n(0);
1628 Bfield* field = project->field;
1629 Bmicrograph* mg;
1630
1631 if ( mg_exclude.contains("none") ) {
1632 for ( field = project->field; field; field = field->next )
1633 for ( mg = field->mg; mg; mg = mg->next ) mg->select = 1;
1634 return 0;
1635 }
1636
1637 for ( field = project->field; field; field = field->next )
1638 for ( nmg=0, mg = field->mg; mg; mg = mg->next )
1639 if ( nmg <= mg->img_num ) nmg++;
1640
1641 int* en = new int[nmg];
1642
1643 select_numbers(mg_exclude, nmg, en);
1644
1645 if ( verbose )
1646 cout << "Deselecting micrographs:" << endl;
1647 for ( field = project->field; field; field = field->next ) {
1648 for ( i=0, mg = field->mg; mg; mg = mg->next, i++ ) if ( en[i] ) {
1649 mg->select = 0;
1650 n++;
1651 if ( verbose & VERB_PROCESS )
1652 cout << "Micrograph " << mg->id << " (" << mg->img_num << ") deselected" << endl;
1653 }
1654 }
1655 if ( verbose )
1656 cout << "Total deselected: " << n << endl << endl;
1657
1658 delete[] en;
1659
1660 return n;
1661 }
1662
1672 int project_set_tilt_axis(Bproject* project, double tilt_axis)
1673 {
1674 Bfield* field = project->field;
1675 Bmicrograph* mg;
1676
1677 for ( field = project->field; field; field = field->next ) if ( field->select )
1678 for ( mg = field->mg; mg; mg = mg->next )
1679 mg->tilt_axis = tilt_axis;
1680
1681 project_mg_tilt_to_matrix(project);
1682
1683 return 0;
1684 }
1685
1694 int project_invert_tilt_axis(Bproject* project)
1695 {
1696 Bfield* field = project->field;
1697 Bmicrograph* mg;
1698
1699 for ( field = project->field; field; field = field->next ) if ( field->select )
1700 for ( mg = field->mg; mg; mg = mg->next )
1701 mg->tilt_axis = angle_set_negPI_to_PI(mg->tilt_axis + M_PI);
1702
1703 project_mg_tilt_to_matrix(project);
1704
1705 return 0;
1706 }
1707
1719 int project_set_tilt_angles(Bproject* project, double tilt_start, double tilt_step)
1720 {
1721 double tilt;
1722 Bfield* field = project->field;
1723 Bmicrograph* mg;
1724
1725 if ( fabs(tilt_step) < 0.005 ) { // Must be greater than 0.5 degrees
1726 cerr << "Error: The tilt step size is too small (" << tilt_step << ")!" << endl;
1727 return -1;
1728 }
1729
1730 for ( field = project->field; field; field = field->next ) if ( field->select )
1731 for ( tilt=tilt_start, mg = field->mg; mg; mg = mg->next, tilt+=tilt_step )
1732 mg->tilt_angle = tilt;
1733
1734 project_mg_tilt_to_matrix(project);
1735
1736 return 0;
1737 }
1738
1749 int project_calculate_angles(Bproject* project)
1750 {
1751 double axis_angle, avg_axis(0), wa, wax(0);
1752 Vector3<double> tilt_axis;
1753 Quaternion q;
1754 Transform t;
1755 Matrix3 mat;
1756 Bfield* field = project->field;
1757 Bmicrograph* mg;
1758
1759 Bmicrograph* mg_ref = field_find_zero_tilt_mg(field);
1760
1761 if ( field->next ) {
1762 mg = field_find_zero_tilt_mg(field->next);
1763 t = marker_find_transform(mg->mark, mg_ref->mark, mg_ref->origin);
1764 field->next->matrix = Matrix3(t.axis, t.angle);
1765 }
1766
1767 for ( field = project->field; field; field = field->next ) if ( field->select ) {
1768 // cout << "field matrix: " << field->matrix << endl;
1769 for ( mg = field->mg; mg; mg = mg->next ) if ( mg->select ) {
1770 if ( mg->matrix.determinant() > 0.5 ) {
1771 // if ( mg != mg_ref && mg->mark ) {
1772 // mat = mg->matrix * field->matrix;
1773 mat = field->matrix.transpose() * mg->matrix;
1774 q = mat.quaternion();
1775 // q = mg->matrix.quaternion();
1776 tilt_axis = q.axis();
1777 axis_angle = atan2(tilt_axis[1], tilt_axis[0]);
1778 if ( !isfinite(axis_angle) ) axis_angle = 0;
1779 mg->tilt_angle = -2*acos(q[0]);
1780 if ( !isfinite(mg->tilt_angle) ) mg->tilt_angle = 0;
1781 if ( fabs(mg->tilt_axis - axis_angle) < M_PI_2 ) {
1782 mg->tilt_axis = axis_angle;
1783 } else {
1784 if ( mg->tilt_axis > axis_angle ) mg->tilt_axis = axis_angle + M_PI;
1785 else mg->tilt_axis = axis_angle - M_PI;
1786 mg->tilt_angle = -mg->tilt_angle;
1787 }
1788 // Calculate level angle
1789 mg->level_angle = asin(tilt_axis[2]);
1790 if ( !isfinite(mg->level_angle) ) mg->level_angle = 0;
1791 if ( mg->level_angle > 0.2 ) mg->level_angle = 0;
1792 }
1793 wa = 1 - exp(-mg->tilt_angle*mg->tilt_angle/0.5);
1794 if ( fabs(mg->tilt_angle) > M_PI/45.0 ) {
1795 avg_axis += wa*mg->tilt_axis;
1796 wax += wa;
1797 }
1798 }
1799 }
1800
1801 if ( wax ) avg_axis /= wax;
1802
1803 // Fix tilt axis angles - particularly for the zero-tilt micrograph
1804 for ( field = project->field; field; field = field->next ) if ( field->select )
1805 for ( mg = field->mg; mg; mg = mg->next )
1806 if ( fabs(mg->tilt_axis - avg_axis) > M_PI/90.0 ) mg->tilt_axis = avg_axis;
1807
1808 return 0;
1809 }
1810
1819 int project_invert_matrices(Bproject* project)
1820 {
1821 Bfield* field = project->field;
1822 Bmicrograph* mg;
1823
1824 for ( field = project->field; field; field = field->next ) if ( field->select )
1825 for ( mg = field->mg; mg; mg = mg->next )
1826 mg->matrix = mg->matrix.transpose();
1827
1828 return 0;
1829 }
1830
1831
1841 int project_mg_marker_select(Bproject* project, double fom)
1842 {
1843 Bfield* field;
1844 Bmicrograph* mg;
1845 Bmarker* mark;
1846
1847 for ( field = project->field; field; field = field->next )
1848 for ( mg = field->mg; mg; mg = mg->next )
1849 for ( mark = mg->mark; mark; mark = mark->next )
1850 if ( mark->fom >= fom ) mark->sel = 1;
1851 else mark->sel = 0;
1852
1853 return 0;
1854 }
1855
1862 int project_set_marker_radius(Bproject* project, double mark_radius)
1863 {
1864 Bfield* field;
1865 Bmicrograph* mg;
1866 Breconstruction* rec;
1867
1868 for ( field = project->field; field; field = field->next )
1869 for ( mg = field->mg; mg; mg = mg->next )
1870 mg->mark_radius = mark_radius;
1871
1872 for ( rec = project->rec; rec; rec = rec->next )
1873 rec->mark_radius = mark_radius;
1874
1875 return 0;
1876 }
1877
1891 int project_check_markers(Bproject* project, int flags)
1892 {
1893 Bfield* field = project->field;
1894 Bmicrograph* mg = field_find_micrograph_by_tiltang(field, 0);
1895 Bmarker* m;
1896 Bimage* p = read_img(mg->fmg, 0, mg->img_num);
1897
1898 if ( flags & 1 ) {
1899 cout << "Markers outside micrograph boundaries:" << endl;
1900 cout << "Micrograph\tMarker\tx\ty" << endl;
1901 }
1902
1903 for ( field = project->field; field; field = field->next ) if ( field->select ) {
1904 for ( mg = field->mg; mg; mg = mg->next ) {
1905 for ( m = mg->mark; m; m = m->next ) {
1906 if ( m->loc[0] < 0 || m->loc[0] > p->sizeX() || m->loc[1] < 0 || m->loc[1] > p->sizeY() ) {
1907 if ( flags & 1 ) cout << mg->id << tab << m->id << tab << m->loc[0] << tab << m->loc[1] << endl;
1908 if ( flags & 2 ) m->sel = 0;
1909 if ( flags & 4 ) m->fom = 0;
1910 if ( flags & 8 ) m->err = m->res = 0;
1911 }
1912 }
1913 }
1914 }
1915
1916 delete p;
1917
1918 return 0;
1919 }
1920
1926 int project_fix_markers(Bproject* project)
1927 {
1928 Bfield* field = project->field;
1929 Bmicrograph* mg = field_find_micrograph_by_tiltang(field, 0);
1930 Breconstruction* rec = project->rec;
1931 Bmarker* m, *mm;
1932 Bimage* p = read_img(mg->fmg, 0, mg->img_num);
1933
1934 for ( field = project->field; field; field = field->next ) if ( field->select ) {
1935 for ( mg = field->mg; mg; mg = mg->next ) {
1936 for ( m = mg->mark, mm = rec->mark; m; m = m->next, mm = mm->next ) {
1937 if ( m->loc[0] < 0 || m->loc[0] > p->sizeX() || m->loc[1] < 0 || m->loc[1] > p->sizeY() ) {
1938 m->loc = mg_location_from_3D_model(mm->loc, rec->origin, mg->matrix, mg->origin);
1939 m->err = 0;
1940 m->sel = 0;
1941 m->fom = m->res = 0;
1942 }
1943 }
1944 }
1945 }
1946
1947 delete p;
1948
1949 return 0;
1950 }
1951
1960 int project_mg_tilt_to_matrix(Bproject* project)
1961 {
1962 Bfield* field;
1963 Bmicrograph* mg;
1964
1965 for ( field = project->field; field; field = field->next )
1966 for ( mg = field->mg; mg; mg = mg->next ) {
1967 mg->matrix = Matrix3(mg->tilt_angle, mg->tilt_axis);
1968 mg->matrix = mg->matrix.transpose();
1969 }
1970
1971 return 0;
1972 }
1973
1974 int img_clear_extraneous_areas(Bimage* p, Vector3<double> vec, double dist, double width)
1975 {
1976 long i, xx, yy, cc;
1977 double de(0), dr(0), edge, a(GOLDEN/width);
1978 Vector3<double> d, r, hr(p->size()/2-3*width);
1979
1980 p->calculate_background();
1981
1982 double fill(p->background(long(0)));
1983
1984 for ( i=yy=0; yy<p->sizeY(); yy++ ) {
1985 d[1] = (double)yy - p->image->origin()[1];
1986 for ( xx=0; xx<p->sizeX(); xx++ ) {
1987 d[0] = (double)xx - p->image->origin()[0];
1988 r = d.abs() - hr;
1989 if ( r[0] < r[1] ) dr = a*r[1];
1990 else dr = a*r[0];
1991 de = a*(fabs(vec[0]*d[0] + vec[1]*d[1]) - dist);
1992 if ( dr > de ) de = dr;
1993 if ( de > 50 ) edge = 1e30;
1994 else edge = exp(de);
1995 if ( !isfinite(edge) ) {
1996 cerr << xx << " " << yy << ": Value too large or not finite!: " << edge <<endl;
1997 edge = 1e30;
1998 }
1999 for ( cc=0; cc<p->channels(); cc++, i++ )
2000 p->set(i, ((*p)[i] + fill*edge)/(1+edge));
2001 }
2002 }
2003
2004 return 0;
2005 }
2006
2015 int micrograph_clear_extraneous_areas(Bmicrograph* mg, Bimage* p, long thickness, double width)
2016 {
2017 if ( !p )
2018 return error_show("micrograph_clear_extraneous_areas", __FILE__, __LINE__);
2019
2020 View view = View(mg->matrix);
2021 Vector3<double> v = Vector3<double>(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2022 double d(1e30);
2023 if ( v[0] && d > fabs(p->sizeX()*0.5/v[0]) ) d = fabs(p->sizeX()*0.5/v[0]);
2024 if ( v[1] && d > fabs(p->sizeY()*0.5/v[1]) ) d = fabs(p->sizeY()*0.5/v[1]);
2025 double a = d*cos(mg->tilt_angle) + fabs(0.5*thickness*sin(mg->tilt_angle));
2026
2027 p->calculate_background();
2028 p->origin(mg->origin);
2029 p->image->view(view);
2030
2031 img_clear_extraneous_areas(p, v, a, width);
2032
2033 return 0;
2034 }
2035
2043 int project_clear_extraneous_areas(Bproject* project, long thickness, double width)
2044 {
2045 long nmg(0);
2046 Bimage* p = NULL;
2047 Bimage* pnu = NULL;
2048 Bfield* field = project->field;
2049 Bmicrograph* mg = field->mg;
2050
2051 Bstring newfile = mg->fmg;
2052 newfile = newfile.insert(newfile.rfind("."), "_clr");
2053
2054 for ( field = project->field; field; field = field->next ) if ( field->select ) {
2055 for ( nmg=0, mg = field->mg; mg; mg = mg->next, nmg++ ) {
2056 if ( verbose )
2057 cout << "Micrograph " << mg->img_num << ": " << mg->fmg << endl;
2058 p = read_img(mg->fmg, 0, mg->img_num);
2059 if ( !p )
2060 return error_show("project_clear_extraneous_areas", __FILE__, __LINE__);
2061 if ( !pnu ) {
2062 pnu = p;
2063 } else {
2064 delete p;
2065 }
2066 }
2067 }
2068
2069 if ( verbose ) {
2070 cout << "Clearing extraneous areas from micrographs:" << endl;
2071 cout << "Thickness: " << thickness << endl;
2072 cout << "Edge width: " << width << endl;
2073 cout << "New micrograph file name: " << newfile << endl << endl;
2074 }
2075
2076 pnu->images(nmg);
2077 pnu->data_alloc();
2078
2079 // mg = project->field->mg;
2080
2081 /* View view;
2082 Vector3<double> v(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2083 double a, d(1e30);
2084 if ( v[0] && d > fabs(pnu->sizeX()*0.5/v[0]) ) d = fabs(pnu->sizeX()*0.5/v[0]);
2085 if ( v[1] && d > fabs(pnu->sizeY()*0.5/v[1]) ) d = fabs(pnu->sizeY()*0.5/v[1]);*/
2086
2087 for ( field = project->field; field; field = field->next ) if ( field->select ) {
2088 for ( nmg=0, mg = field->mg; mg; mg = mg->next, nmg++ ) {
2089 p = read_img(mg->fmg, 1, mg->img_num);
2090 mg->fmg = newfile;
2091 micrograph_clear_extraneous_areas(mg, p, thickness, width);
2092 /* view = View(mg->matrix);
2093 if ( verbose )
2094 cout << "Micrograph " << nmg+1 << ": " << mg->fmg << endl;
2095 if ( !p )
2096 return error_show("project_clear_extraneous_areas", __FILE__, __LINE__);
2097 p->calculate_background();
2098 p->origin(mg->origin);
2099 v = Vector3<double>(-sin(mg->tilt_axis), cos(mg->tilt_axis), 0);
2100 a = d*cos(mg->tilt_angle) + fabs(0.5*thickness*sin(mg->tilt_angle));
2101 img_clear_extraneous_areas(p, v, a, width);*/
2102 pnu->replace(nmg, p);
2103 pnu->image[nmg] = p->image[0];
2104 pnu->image[nmg].origin(mg->origin);
2105 pnu->image[nmg].view(View(mg->matrix));
2106 delete p;
2107 }
2108 write_img(field->mg->fmg, pnu, 0);
2109 }
2110
2111 delete pnu;
2112
2113 return 0;
2114 }
2115
2124 double project_marker_rotation_axis(Bproject* project)
2125 {
2126 long n;
2127 double avg_z_offset(0), zs(0), d;
2128 Bfield* field = project->field;
2129 Bmicrograph* mg = field->mg;
2130 Bmicrograph* mg_ref = field_find_low_tilt_mg_with_markers(field);
2131
2132 for ( n=0, mg = field->mg; mg; mg = mg->next ) {
2133 if ( mg->tilt_angle > M_PI/180.0 ) {
2134 zs += fabs(mg->origin.distance(mg_ref->origin)/sin(mg->tilt_angle));
2135 n++;
2136 }
2137 }
2138
2139 if ( n ) avg_z_offset = zs/n;
2140
2141 cout << "Average z offset: " << avg_z_offset << endl << endl;
2142
2143 cout << "Micrograph\tOffset\tAdjusted" << endl;
2144 for ( mg = field->mg; mg; mg = mg->next ) {
2145 d = mg->origin.distance(mg_ref->origin);
2146 cout << mg->id << tab << d << tab << d - fabs(avg_z_offset*sin(mg->tilt_angle)) << endl;
2147 }
2148 cout << endl;
2149
2150 return avg_z_offset;
2151 }
2152
2163 int project_merge_rec_markers(Bproject* project)
2164 {
2165 Breconstruction* rec1 = project->rec;
2166 Breconstruction* rec2 = project->rec->next;
2167 Bmarker* mark1 = rec1->mark;
2168 Bmarker* mark2 = rec2->mark;
2169
2170 long n;
2171 double R(0);
2172
2173 if ( verbose & VERB_FULL )
2174 cout << "Marker\tLocation\tResidual" << endl;
2175 for ( n=0, mark1 = rec1->mark; mark1; mark1 = mark1->next ) if ( mark1->sel ) {
2176 for ( mark2 = rec2->mark; mark2 && mark2->id != mark1->id; mark2 = mark2->next ) ;
2177 if ( mark2 && mark2->sel ) {
2178 mark1->err = mark1->loc - mark2->loc;
2179 mark1->res = mark1->err.length();
2180 R += mark1->res*mark1->res;
2181 n++;
2182 mark1->loc = (mark1->loc + mark2->loc)/2; // Set the marker model to the average
2183 if ( verbose & VERB_FULL )
2184 cout << mark1->id << tab << mark1->loc << tab << mark1->res << endl;
2185 }
2186 }
2187
2188 if ( n ) R = sqrt(R/n);
2189
2190 if ( verbose & VERB_FULL ) {
2191 cout << "Markers selected: " << n << endl;
2192 cout << "Residual: " << R << endl << endl;
2193 }
2194
2195 reconstruction_kill(rec2);
2196 rec1->next = NULL;
2197
2198 return n;
2199 }
2200
2201
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
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:346
M_PI_2
#define M_PI_2
Definition: utilities.h:62
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2828
project_count_markers
long project_count_markers(Bproject *project)
Counts the markers in a project hierarchy.
Definition: mg_tomography.cpp:1293
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
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:201
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:143
Bfield::select
long select
Definition: mg_processing.h:262
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Vector3::length
double length()
Definition: Vector3.h:222
project_generate_markers
int project_generate_markers(Bproject *project)
Generates missing micrograph and model markers.
Definition: mg_tomography.cpp:1007
Bimage::add
void add(long j, double v)
Definition: Bimage.h:308
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:307
project_mg_marker_select
int project_mg_marker_select(Bproject *project, double fom)
Selects markers.
Definition: mg_tomography.cpp:1841
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, string &label)
Normalizes a set of images in a project to a desired average and standard deviation.
Definition: mg_tomography.cpp:379
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
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:660
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2052
markers_sort_by_id
Bmarker * markers_sort_by_id(Bmarker **mark)
Sorts a set of markers by ID number.
Definition: marker.cpp:1104
Bmicrograph::id
Bstring id
Definition: mg_processing.h:202
project_tomo_residuals
double project_tomo_residuals(Bproject *project, int show)
Calculates the residuals for an alignment solution.
Definition: mg_tomography.cpp:671
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:211
utilities.h
Header file for general utilities.
Bsimplex
Structure used in the downhill simplex method.
Definition: simplex.h:42
Vector3::abs
Vector3 abs()
Definition: Vector3.h:201
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Breconstruction
Definition: mg_processing.h:280
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
project_find_markers
long project_find_markers(Bproject *project, long edge, int add)
Finds all markers in a project.
Definition: mg_tomography.cpp:1180
Bstring::rfind
long rfind(const Bstring &s)
Definition: Bstring.h:105
mg_img_proc.h
Header file for image processing from micrograph structures.
Transform::angle
double angle
Definition: Transform.h:29
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:2015
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
project_renumber_markers
long project_renumber_markers(Bproject *project)
Renumbers the markers in a project hierarchy.
Definition: mg_tomography.cpp:1516
project_invert_matrices
int project_invert_matrices(Bproject *project)
Inverts the micrograph matrices in a project parameter structure.
Definition: mg_tomography.cpp:1819
Bsimplex::parameter
void parameter(long i, double p)
Definition: simplex.h:69
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:383
Bsimplex::dependent_values
vector< double > & dependent_values()
Definition: simplex.h:83
Bmarker
Definition: marker.h:21
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Matrix3
Definition: Matrix3.h:17
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::pixel_size
double pixel_size
Definition: mg_processing.h:214
project
Bproject * project
Definition: bshow.cpp:31
GOLDEN
#define GOLDEN
Definition: utilities.h:81
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Quaternion
Definition: Quaternion.h:25
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:1862
Bimage::symmetrize_cylinder
Bimage * symmetrize_cylinder(int flag)
Calculates a cylindrically symmetrized map.
Definition: Bimage_helix.cpp:524
Bmicrograph::dose
double dose
Definition: mg_processing.h:215
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
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
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_processing.cpp:746
simplex_fit_tilt_intensities
vector< double > simplex_fit_tilt_intensities(long n, vector< double > &a, vector< double > &I)
Definition: mg_tomography.cpp:463
project_invert_tilt_axis
int project_invert_tilt_axis(Bproject *project)
Inverts the tilt axis in a project parameter structure.
Definition: mg_tomography.cpp:1694
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:217
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
project_thickness
double project_thickness(Bproject *project, double lambda)
Determines the thickness from a tilt series and mean free path.
Definition: mg_tomography.cpp:623
Matrix3::quaternion
Quaternion quaternion() const
Definition: Matrix3.h:129
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:293
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:1719
Bfield::next
Bfield * next
Definition: mg_processing.h:258
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:1135
Bsimplex::run
double run(long maxcycles, double tolerance, double(funk)(Bsimplex &))
Definition: simplex.cpp:76
TSimple
Definition: Bimage.h:58
timer.h
Utilities for timing functions.
Breconstruction::mark_radius
double mark_radius
Definition: mg_processing.h:301
Float
Definition: utilities.h:157
project_deselect_markers
long project_deselect_markers(Bproject *project, Bstring &deselect_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1389
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:222
Bimage
General image parameter class.
Definition: Bimage.h:163
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:139
verbose
int verbose
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:216
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:704
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bsimplex::independent_values
vector< double > & independent_values()
Definition: simplex.h:82
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Transform
Definition: Transform.h:24
Bfield
Definition: mg_processing.h:257
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
Bsimplex::points
long points()
Definition: simplex.h:64
isfinite
bool isfinite(complex< T > &value)
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
reconstruction_kill
int reconstruction_kill(Breconstruction *rec)
Frees a whole reconstruction parameter structure.
Definition: mg_processing.cpp:1377
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:328
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:206
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
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bfield::id
Bstring id
Definition: mg_processing.h:259
img_clear_extraneous_areas
int img_clear_extraneous_areas(Bimage *p, Vector3< double > vec, double dist, double width)
Definition: mg_tomography.cpp:1974
mg_marker_init
int mg_marker_init(Bmicrograph *mg, Bmarker *model)
Initializes markers from the reference.
Definition: mg_tomography.cpp:29
Bmicrograph::select
long select
Definition: mg_processing.h:203
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:235
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:438
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bproject::field
Bfield * field
Definition: mg_processing.h:343
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:1926
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_processing.cpp:400
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:2043
Bmicrograph::level_angle
double level_angle
Definition: mg_processing.h:218
Bmicrograph
Definition: mg_processing.h:200
Bstring::insert
Bstring insert(long pos, const Bstring &s)
Definition: Bstring.cpp:590
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:110
mg_tomography.h
Header file for single particle image processing in Bsoft.
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:220
Bmarker::fom
float fom
Definition: marker.h:28
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
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:2124
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bmarker::id
int id
Definition: marker.h:23
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
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:59
FILL_USER
#define FILL_USER
Definition: utilities.h:172
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
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
Bimage::channels
long channels()
Definition: Bimage.h:371
Vector3::length2
double length2()
Definition: Vector3.h:221
Bimage::find_peaks
Bimage * find_peaks(long kernelsize)
Finds peaks in a map with periodic boundaries.
Definition: Bimage_correlate.cpp:1601
mg_composite_particle
Bimage * mg_composite_particle(Bmicrograph *mg, Vector3< long > size)
Generates an average particle image from a micrograph.
Definition: mg_tomography.cpp:1240
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:504
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:242
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:227
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
model
Bmodel * model
Definition: bshow.cpp:32
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_processing.cpp:362
Bfield::matrix
Matrix3 matrix
Definition: mg_processing.h:261
simplex.h
Nelder and Mead downhill simplex method for generalized parameter fitting.
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2093
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
mg_find_markers
long mg_find_markers(Bmicrograph *mg, long edge, int add)
Finds markers in a micrograph.
Definition: mg_tomography.cpp:1205
Bproject::select
int select
Definition: mg_processing.h:339
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
matrix_linear.h
Header file for matrix functions.
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:90
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:1749
Breconstruction::scale
Vector3< double > scale
Definition: mg_processing.h:294
Bmarker::img_num
int img_num
Definition: marker.h:24
markers_copy
Bmarker * markers_copy(Bmarker *mark)
Copies a set of markers.
Definition: marker.cpp:92
project_mg_select
long project_mg_select(Bproject *project, Bstring &mg_select)
Selects micrographs in a project hierarchy.
Definition: mg_tomography.cpp:1585
project_merge_rec_markers
int project_merge_rec_markers(Bproject *project)
Merges the markers for two reconstructions.
Definition: mg_tomography.cpp:2163
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
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:1960
Vector3< double >
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:205
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:1351
project_show_markers
long project_show_markers(Bproject *project)
Shows the markers in a project hierarchy.
Definition: mg_tomography.cpp:1321
Quaternion::axis
Vector3< double > axis()
Definition: Quaternion.h:144
lambda_R
double lambda_R(Bsimplex &simp)
Definition: mg_tomography.cpp:446
project_mg_exclude
long project_mg_exclude(Bproject *project, Bstring &mg_exclude)
Deselects micrographs in a project hierarchy.
Definition: mg_tomography.cpp:1623
View
Definition: View.h:26
Bmarker::err
Vector3< float > err
Definition: marker.h:26
show
int show(0)
Bmicrograph::fom
double fom
Definition: mg_processing.h:236
Vector3::min
Type min()
Definition: Vector3.h:169
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
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:1672
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_processing.cpp:431
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_processing.cpp:1067
field_count_mg_selected
long field_count_mg_selected(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_processing.cpp:1082
Bstring::split_into_integers
vector< long > split_into_integers(const Bstring &delim)
Definition: Bstring.cpp:649
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:647
mg_processing.h
Header file for micrograph processing.
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:809
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
project_calculate_model
int project_calculate_model(Bproject *project)
Calculates the z-coordinates of the marker model.
Definition: mg_tomography.cpp:886
mg_erase_markers
Bimage * mg_erase_markers(Bmicrograph *mg, double marker_radius)
Erases or paints markers in micrographs.
Definition: mg_tomography.cpp:1038
Bsub_image::average
double average()
Definition: Bimage.h:115
Bimage::replace_maxima
long replace_maxima(double threshold)
Replaces maxima above a threshold using local averages.
Definition: Bimage_segment.cpp:692
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:1891
Bimage::images
long images()
Definition: Bimage.h:491
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
markers_add
long markers_add(Bmarker **mark, Bmarker *mark2, double mindist, int sel)
Adds a set of markers to another.
Definition: marker.cpp:120
project_tomo_errors
double project_tomo_errors(Bproject *project)
Analyzes marker errors.
Definition: mg_tomography.cpp:843
project_erase_markers
int project_erase_markers(Bproject *project, double marker_radius)
Erases or paints markers in micrographs.
Definition: mg_tomography.cpp:1080
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
project_lambda
double project_lambda(Bproject *project, double thickness)
Determines the mean free path from a tilt series and thickness.
Definition: mg_tomography.cpp:598
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Bmicrograph::scale
Vector3< double > scale
Definition: mg_processing.h:221
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bsimplex::limits
void limits(long i, double vlo, double vhi)
Definition: simplex.h:75
Bmarker::res
float res
Definition: marker.h:27
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
Bmarker::sel
int sel
Definition: marker.h:29
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bproject
Definition: mg_processing.h:333
project_delete_markers
long project_delete_markers(Bproject *project, Bstring &delete_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1445
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
img_gold_particle
Bimage * img_gold_particle(Vector3< long > size, double radius)
Generates an image representing a gold particle.
Definition: mg_tomography.cpp:187
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201