Bsoft  2.1.0
Bernard's software package
mg_extract.cpp
Go to the documentation of this file.
1 
9 #include "mg_extract.h"
10 #include "mg_select.h"
11 #include "linked_list.h"
12 #include "spline.h"
13 #include "utilities.h"
14 
15 #include <sys/stat.h>
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 int mask_filament_particles(Bstring& filename, Bparticle* partlist, int mask_width);
21 int rotate_mask_filament_particles(Bstring& filename, Bparticle* partlist,
22  int rotation_axis, int back_flag, int mask_width);
23 
36 int particle_setup_filenames(Bparticle* part, Bstring filename, Bstring partpath)
37 {
38  long n(0);
39  char format[32] = "%03d";
40  Bstring path;
41 
42  if ( verbose & VERB_DEBUG )
43  cout << "DEBUG particle_setup_filenames: base=" << filename << endl;
44 
45  if ( partpath.length() ) {
46  if ( !partpath.empty() )
47  if ( partpath[-1] == '/' ) partpath = partpath.truncate(1);
48  n = partpath.count('?');
49  if ( n > 0 ) {
50  partpath = partpath.remove('?');
51  snprintf(format, 32, "%%0%ldd", n);
52  }
53  if ( verbose & VERB_DEBUG )
54  cout << "DEBUG particle_setup_filenames: path=" << partpath << endl;
55  }
56 
57  if ( partpath.length() > 1 ) if ( filename.contains("/") ) filename = filename.post_rev('/');
58  for ( ; part; part = part->next ) {
59  if ( partpath.length() > 1 ) {
60  if ( n < 1 ) path = partpath;
61  else path = partpath + Bstring(part->id, format);
62  part->fpart = path + "/" + filename;
63  } else part->fpart = filename;
64  mkdir(path.c_str(), (mode_t)0755);
65  part->fpart = part->fpart.pre_rev('.') +
66  Bstring(part->id, format) + "." + part->fpart.post_rev('.');
67  if ( verbose & VERB_DEBUG )
68  cout << "DEBUG particle_setup_filenames: id=" << part->id << " path=" << part->fpart << endl;
69  }
70 
71  return 0;
72 }
73 
86 int filament_setup_filenames(Bfilament* fil, Bstring filename, Bstring filpath)
87 {
88  long n(0);
89  char format[32] = "%03d";
90  Bstring path;
91 
92  if ( filpath.length() ) {
93  if ( !filpath.empty() )
94  if ( filpath[-1] == '/' ) filpath = filpath.truncate(1);
95  n = filpath.count('?');
96  if ( n > 0 ) {
97  filpath = filpath.remove('?');
98  snprintf(format, 32, "%%0%ldd", n);
99  }
100  if ( verbose & VERB_DEBUG )
101  cout << "DEBUG filament_setup_filenames: path=" << filpath << endl;
102  }
103 
104  if ( filpath.length() > 1 ) if ( filename.contains("/") ) filename = filename.post_rev('/');
105  for ( ; fil; fil = fil->next ) {
106  if ( filpath.length() > 1 ) {
107  if ( n < 1 ) path = filpath;
108  else path = filpath + Bstring(fil->id, format);
109  fil->ffil = path + "/" + filename;
110  } else fil->ffil = filename;
111  mkdir(path.c_str(), (mode_t)0755);
112  fil->ffil = fil->ffil.pre_rev('.') +
113  Bstring(fil->id, format) + "." + fil->ffil.post_rev('.');
114  }
115 
116  return 0;
117 }
118 
129 {
130  int n;
131  Bfilnode* fn;
132 
133  for ( n=0, fn = fnode; fn; fn=fn->next ) n++;
134 
135  Vector3<double>* coords = new Vector3<double>[n];
136 
137  for ( n=0, fn = fnode; fn; fn=fn->next, n++ ) coords[n] = fn->loc;
138 
139  Vector3<double>* spline = vector3_catmull_rom_spline(n, coords, nspline);
140 
141  delete[] coords;
142 
143  return spline;
144 }
145 
162  int back_flag, int norm_flag, int fill_type, double fill, int mask_width,
163  int split, Bstring& partbase, Bstring& partpath, Bstring& partext)
164 {
165  long i(1), npart(0);
166  Bstring filename;
167  Bfield* field = project->field;
168  Bmicrograph* mg;
169  Breconstruction* rec = project->rec;
170  Bimage* p = NULL;
171 
172  if ( !partpath.empty() ) if ( partpath[-1] != '/' ) partpath += "/";
173 
174  if ( partext.length() < 1 ) partext = "mrc";
175 
178 
179  long multi_img(0);
180 
181  Vector3<long> size;
182  if ( project->select && rec ) size = rec->box_size;
183  else if ( field && field->mg ) size = field->mg->box_size;
184  if ( size.volume() < 1 ) {
185  cerr << "Error in project_extract_particles: Box size = " << size << endl;
186  return -1;
187  }
188 
189  if ( verbose ) {
190  cout << "Extracting particles:" << endl;
191  cout << "Particle size: " << size << endl;
192  cout << "Particle scale: " << scale << endl;
193  if ( mask_width )
194  cout << "Filament mask width: " << mask_width << endl;
195  if ( back_flag ) cout << "With background correction" << endl;
196  if ( norm_flag ) cout << "With normalization" << endl;
197  if ( partbase.length() )
198  cout << "Particle base name: " << partbase << endl;
199  if ( partext.length() )
200  cout << "Particle extension: " << partext << endl;
201  if ( partpath.length() )
202  cout << "Particle path: " << partpath << endl;
203  if ( project->select > 1 )
204  cout << "Projecting" << endl;
205  cout << endl;
206  }
207 
208  if ( project->select < 1 ) {
209  for ( field = project->field; field; field = field->next ) {
210  for ( mg=field->mg; mg; mg=mg->next, i++ ) {
211  if ( mg->next ) {
212  if ( mg->fmg == mg->next->fmg ) multi_img = 1;
213  else multi_img = 0;
214  }
215 // cout << mg->fmg << endl;
216  npart = particle_count(mg->part);
217  if ( npart > 0 ) {
218  if ( partbase.length() ) {
219  if ( nmg == 1 )
220  filename = partbase;
221  else
222  filename = partbase + Bstring(i, "_%03d");
223  } else {
224  if ( mg->fpart.length() < 1 ) mg->fpart = mg->fmg;
225  if ( mg->fmg.length() < 1 ) mg->fpart = mg->ffil;
226  filename = mg->fpart.pre_rev('.');
227  if ( mg->fmg == mg->fpart ) {
228  if ( multi_img ) filename = filename + Bstring(mg->img_num+1, "_part%03d");
229  else filename = filename + "_part";
230  }
231  if ( mg->ffil == mg->fpart ) {
232  filename = filename + "_fil";
233  }
234  }
235  if ( filename.length() < 1 )
236  cerr << "Error in project_extract_particles: No particle file name!" << endl;
237  filename = filename + "." + partext;
238  if ( partpath.length() > 1 ) {
239  if ( filename.contains("/") ) filename = filename.post_rev('/');
240  filename = partpath + filename;
241  }
242  if ( split ) {
243  particle_setup_filenames(mg->part, filename, partpath);
244  mg->fpart = 0;
245  } else {
246  mg->fpart = filename;
247  if ( partpath.length() > 1 ) mkdir(partpath.c_str(), (mode_t)0755);
248 // cout << mg->fmg << " ---> " << mg->fpart << endl;
249  }
250  if ( mg->fmg.length() ) p = read_img(mg->fmg, 1, mg->img_num);
251  else if ( mg->ffil.length() ) p = read_img(mg->ffil, 1, 0);
252  if ( !p ) {
253  error_show(mg->fmg.c_str() , __FILE__, __LINE__);
254  return -1;
255  }
256  if ( fill_type == FILL_AVERAGE ) fill = p->average();
257  if ( fill_type == FILL_BACKGROUND ) {
258  if ( fabs(p->background(long(0))) < 1e-6 )
260  fill = p->background(long(0));
261  }
262  micrograph_extract_particles(mg, p, scale, back_flag, norm_flag, fill, mask_width);
263  delete p;
264  }
265  }
266  }
267  } else if ( project->select == 1 ) {
268  for ( rec=project->rec; rec; rec=rec->next, i++ ) {
269  npart = particle_count(rec->part);
270  if ( npart > 0 ) {
271  if ( partbase.length() ) {
272  if ( nrec == 1 )
273  filename = partbase;
274  else
275  filename = partbase + Bstring(i, "_%03d");
276  } else {
277  if ( rec->fpart.length() < 1 ) rec->fpart = rec->frec;
278  if ( rec->frec.length() < 1 ) rec->fpart = rec->ffil;
279  filename = rec->fpart.pre_rev('.');
280  if ( rec->frec == rec->fpart ) {
281 // if ( rec->img_num ) filename = filename + Bstring(rec->img_num, "_part%03d");
282 // else filename = filename + "_part";
283  filename = filename + "_part";
284  }
285  if ( rec->ffil == rec->fpart ) {
286  filename = filename + "_fil";
287  }
288  }
289  if ( filename.length() < 1 )
290  cerr << "Error in project_extract_particles: No particle file name!" << endl;
291  filename = filename + "." + partext;
292  if ( partpath.length() > 1 ) {
293  if ( filename.contains("/") ) filename = filename.post_rev('/');
294  filename = partpath + filename;
295  }
296  if ( split ) {
297  particle_setup_filenames(rec->part, filename, partpath);
298  rec->fpart = 0;
299  } else {
300  rec->fpart = filename;
301  if ( partpath.length() > 1 ) mkdir(partpath.c_str(), (mode_t)0755);
302  }
303  if ( rec->frec.length() ) p = read_img(rec->frec, 1, 0);
304  else if ( rec->ffil.length() ) p = read_img(rec->ffil, 1, 0);
305  if ( !p ) {
306  error_show(rec->frec.c_str() , __FILE__, __LINE__);
307  return -1;
308  }
309  if ( fill_type == FILL_AVERAGE ) fill = p->average();
310  if ( fill_type == FILL_BACKGROUND ) {
311  if ( fabs(p->background(long(0))) < 1e-6 )
313  fill = p->background(long(0));
314  }
315  reconstruction_extract_particles(rec, p, scale, back_flag, norm_flag, fill, mask_width);
316  delete p;
317  }
318  }
319  } else {
320  if ( rec->frec.length() ) p = read_img(rec->frec, 1, 0);
321  if ( !p ) {
322  error_show(rec->frec.c_str() , __FILE__, __LINE__);
323  return -1;
324  }
325  if ( fill_type == FILL_AVERAGE ) fill = p->average();
326  if ( fill_type == FILL_BACKGROUND ) {
327  if ( fabs(p->background(long(0))) < 1e-6 )
329  fill = p->background(long(0));
330  }
332  if ( mg->part ) particle_kill(mg->part);
333  mg->part = reconstruction_project_extract_particles(rec, p, scale, back_flag, norm_flag, fill, mask_width);
334  if ( p->file_name().length() ) mg->fpart = p->file_name();
335  delete p;
336  }
337 
338  return npart;
339 }
340 
353  int back_flag, int norm_flag, double fill, int mask_width)
354 {
355  long npart = particle_count(mg->part);
356 
357  if ( npart < 1 ) return 0;
358 
359  if ( verbose )
360  cout << mg->id << tab << npart << endl;
361 
362  if ( mg->pixel_size[0] > 0 ) p->sampling(mg->pixel_size);
363 
364  Bimage* ppart = particle_extract(mg->part, mg->bad, p, mg->box_size,
365  scale, mg->bad_radius, back_flag, norm_flag, fill, mask_width);
366 
367  if ( ppart ) {
368  write_img(mg->fpart, ppart, 0);
369  delete ppart;
370  }
371 
372  return npart;
373 }
374 
387  int back_flag, int norm_flag, double fill, int mask_width)
388 {
389  long npart = particle_count(rec->part);
390 
391  if ( npart < 1 ) return 0;
392 
393  if ( verbose )
394  cout << rec->id << tab << npart << endl;
395 
396  if ( rec->voxel_size[0] > 0 ) p->sampling(rec->voxel_size);
397 
398  Bimage* ppart = particle_extract(rec->part, rec->bad, p, rec->box_size,
399  scale, rec->bad_radius, back_flag, norm_flag, fill, mask_width);
400 
401  if ( ppart ) {
402  write_img(rec->fpart, ppart, 0);
403  delete ppart;
404  }
405 
406  return npart;
407 }
408 
424  int back_flag, int norm_flag, double fill, int mask_width)
425 {
426  long npart = particle_count(rec->part);
427 
428  if ( npart < 1 ) return 0;
429 
430  if ( verbose )
431  cout << rec->id << tab << npart << endl;
432 
433  if ( rec->voxel_size[0] > 0 ) p->sampling(rec->voxel_size);
434 
435  Bparticle* part = rec->part;
436 
437  Bstring filename = rec->frec.pre_rev('.') + "_part.mrc";
438  if ( part->fpart.length() && part->fpart.contains("/") )
439  filename = part->fpart.pre_rev('/') + "/" + filename;
440 
441  // Set up particle list for projections
442  Bparticle* part2D = particle_copy(rec->part);
443 
444  // Delete filenames to avoid splitting image file
445  for ( part = part2D; part; part = part->next )
446  part->fpart = 0;
447 
448  Bimage* ppart = particle_extract(part2D, rec->bad, p, rec->box_size,
449  scale, rec->bad_radius, back_flag, norm_flag, fill, mask_width);
450 
451  // Set the z-related parameters
452  for ( part = part2D; part; part = part->next ) {
453  part->pixel_size[2] = 1;
454  part->loc[2] = 0;
455  part->ori[2] = 0;
456  }
457 
458  if ( ppart ) {
459  Bimage* pproj = ppart->project('z');
460  write_img(filename, pproj, 0);
461  delete ppart;
462  delete pproj;
463  p->file_name(filename.c_str());
464  } else {
465  p->file_name("");
466  }
467 
468  return part2D;
469 }
470 
491 Bimage* particle_extract(Bparticle* particles, Bbadarea* bad_areas, Bimage* p,
492  Vector3<long> size, double scale, double bad_radius,
493  int back_flag, int norm_flag, double fill, int mask_width)
494 {
495  int split(0);
496  if ( particles->fpart.length() ) split = 1; // Flag to write separate particle images
497 
498  if ( scale < 0.1 ) scale = 1;
499 
500 // if ( mask_width < 1 ) mask_width = (p->sizeX() > p->sizeY())? p->sizeX(): p->sizeY();
501 
502  long id, i, j, k, c, n, x, y, z;
503  long oldx, oldy, oldz, oldn(0);
504  double d(0), dh(0), halfwidth(mask_width/2.0), ori_tol;
505  double iscale(1/scale);
506  Bparticle* part = particles;
507  Bbadarea* bad;
508  Bimage* pmask = NULL;
509  Bimage* ppart = NULL;
510  Bimage* partmask = NULL;
511  Vector3<long> box, radius;
512  Vector3<double> u, v;
513  Euler euler;
514 
515  long npart = particle_count(particles);
516 
517  if ( npart < 1 ) return NULL;
518 
519  Vector3<double> pixel_size;
520 
521  if ( part->mg ) pixel_size = part->mg->pixel_size;
522  else if ( part->rec ) pixel_size = part->rec->voxel_size;
523  if ( pixel_size[0] < 0.01 ) pixel_size = p->image->sampling();
524 
525  if ( pixel_size[0] < 0.01 ) {
526  cerr << "Error in particle_extract: Pixel size is too small! " << pixel_size << endl;
527  return NULL;
528  }
529 
530  pixel_size *= iscale;
531  box = size; // Box size
532  box = box.min(p->size()*scale);
533  box = box.max(1);
534  radius = box/2;
535  ori_tol = radius.length()/4;
536  if ( ori_tol > 2 ) ori_tol = 2;
537 
538  if ( box.volume() < 1 ) {
539  cerr << "Error: Particle box size is zero!" << endl;
540  return NULL;
541  }
542 
543  if ( box[2] == 1 ) pixel_size[2] = 1;
544 
545  if ( verbose )
546  cout << "Extracting " << npart << " particles from " << p->file_name() << endl;
547 
548  if ( verbose & VERB_PROCESS ) {
549  cout << "Number of particles: " << npart << endl;
550  cout << "Particle box size: " << box << endl;
551  cout << "Particle scale: " << scale << endl;
552  cout << "Particle radii: " << radius << endl;
553  cout << "Bad region radius: " << bad_radius << endl;
554  cout << "Mask width: " << mask_width << endl;
555  cout << "Fill value: " << fill << endl;
556  if ( back_flag ) cout << "Background corrected" << endl;
557  if ( norm_flag ) cout << "Normalized" << endl;
558  cout << endl;
559  }
560 
561  if ( bad_areas && bad_radius > 1 ) {
562  pmask = new Bimage(UCharacter, p->compound_type(), p->size(), p->images());
563  for ( bad = bad_areas; bad; bad = bad->next )
564  pmask->sphere(bad->loc, bad_radius, 0, FILL_USER, 1);
565  }
566 
567  if ( split ) {
568  partmask = new Bimage(UCharacter, TSimple, box, 1);
569  } else {
570  ppart = new Bimage(p->data_type(), p->compound_type(), box, npart);
571  ppart->fill(fill);
572  partmask = new Bimage(UCharacter, TSimple, box, npart);
573  }
574 
575  for ( id=1, i=n=0, part = particles; part; part = part->next, n++, id++ ) {
576  if ( split ) {
577  i = n = 0;
578  ppart = new Bimage(p->data_type(), p->compound_type(), box, 1);
579  ppart->fill(fill);
580  }
581 // cout << "extracting " << id << endl;
582  part->id = id;
583  if ( part->ori.distance(radius) > ori_tol ) part->ori = radius;
584  part->pixel_size = pixel_size;
585  ppart->sampling(pixel_size);
586  ppart->origin(n, part->ori);
587  ppart->image[n].view(part->view);
588  euler = Euler(part->view);
589  u[0] = cos(euler.psi());
590  u[1] = -sin(euler.psi());
591  for ( z=0; z<ppart->sizeZ(); z++ ) {
592  oldz = (z - radius[2])*iscale + (long) part->loc[2];
593  v[2] = z - ppart->image[n].origin()[2];
594  for ( y=0; y<ppart->sizeY(); y++ ) {
595  oldy = (y - radius[1])*iscale + (long) part->loc[1];
596  v[1] = y - ppart->image[n].origin()[1];
597  for ( x=0; x<ppart->sizeX(); x++, i++ ) {
598  oldx = (x - radius[0])*iscale + (long) part->loc[0];
599  v[0] = x - ppart->image[n].origin()[0];
600  if ( oldx >= 0 && oldx < p->sizeX() && oldy >= 0 && oldy < p->sizeY() && oldz >= 0 && oldz < p->sizeZ()) {
601  j = (((oldn*p->sizeZ() + oldz)*p->sizeY() + oldy)*p->sizeX() + oldx)*p->channels();
602  if ( halfwidth > 0 ) dh = (u.cross(v)).length() - halfwidth;
603  if ( back_flag ) d = (v/ppart->size()).length() - 0.5;
604 // cout << "d=" << d << endl;
605  if ( ( pmask && (*pmask)[j] > 0 ) || ( d > 0 ) || ( dh > 0 ) ) {
606  partmask->set(i, 0);
607  if ( back_flag ) for ( c=0, k=i*p->channels(); c<p->channels(); c++ )
608 // ppart->set(k++, (*p)[j++]);
609  ppart->set(k++, p->average(c, oldx, oldy, oldz, oldn, iscale));
610  } else {
611  partmask->set(i, 1);
612  for ( c=0, k=i*p->channels(); c<p->channels(); c++ )
613 // ppart->set(k++, (*p)[j++]);
614  ppart->set(k++, p->average(c, oldx, oldy, oldz, oldn, iscale));
615  }
616  }
617  }
618  }
619  }
620  if ( split ) {
621  if ( back_flag ) ppart->correct_background(partmask, 1); // The mask is 1 for the background
622  else ppart->calculate_background();
623  ppart->statistics();
624  if ( ppart->standard_deviation() > 0 ) {
625  if ( norm_flag )
626  ppart->normalize(ppart->average(), ppart->standard_deviation(), 0);
627  } else {
628  part->sel = 0;
629  }
630  write_img(part->fpart, ppart, 0);
631  delete ppart;
632  ppart = NULL;
633  }
634  }
635 
636  delete pmask;
637 
638 // cout << "pixel size = " << ppart->sampling(0) << endl;
639 
640 // write_img("part.mrc", ppart);
641 // write_img("mask.mrc", partmask);
642 
643  if ( !split ) {
644  if ( back_flag ) ppart->correct_background(partmask, 1);
645  else ppart->calculate_background();
646  ppart->statistics();
647  if ( norm_flag ) ppart->normalize(ppart->average(), ppart->standard_deviation(), 0);
648  for ( n=0, part = particles; part; part = part->next, n++ ) {
649  if ( ppart->image[n].standard_deviation() <= 0 ) part->sel = 0;
650  }
651  }
652 
653  delete partmask;
654 
655  return ppart;
656 }
657 
666 {
667  if ( radius < 1 ) radius = mg->mark_radius;
668 
669  Bimage* pmark = marker_extract_gold(mg->mark, p, mg->img_num, radius);
670 
671  if ( mg->pixel_size[0] > 0 ) pmark->sampling(mg->pixel_size);
672 
673  return pmark;
674 }
675 
684 {
685  if ( radius < 1 ) radius = rec->mark_radius;
686 
687  Bimage* pmark = marker_extract_gold(rec->mark, p, 0, radius);
688 
689  if ( rec->voxel_size[0] > 0 ) pmark->sampling(rec->voxel_size);
690 
691  return pmark;
692 }
693 
702 Bimage* marker_extract_gold(Bmarker* marker_list, Bimage* p, int img_num, double radius)
703 {
704  long i, j, c, n, x, y, z;
705  long oldx, oldy, oldz;
706  Bmarker* mark;
707  long nmark;
708  Vector3<long> box, iradius;
709 
710  for ( nmark=0, mark=marker_list; mark; mark=mark->next ) nmark++;
711 
712  if ( nmark < 1 ) return NULL;
713 
714  iradius[0] = iradius[1] = iradius[2] = (int)(2*radius); // Half box size
715  box[0] = box[1] = box[2] = (int)(4*radius); // Box size
716  if ( p->sizeZ() < 2 ) {
717  iradius[2] = 0;
718  box[2] = 1;
719  }
720 
721  if ( verbose & VERB_PROCESS )
722  cout << "Extracting " << nmark << " markers from image " << p->file_name()
723  << " (" << img_num << ") with box size " << box << endl;
724 
725  Bimage* pmark = new Bimage(p->data_type(), p->compound_type(), box[0], box[1], box[2], nmark);
726  pmark->sampling(p->sampling(0));
727 
728  for ( n=0, mark=marker_list; mark && n<pmark->images(); mark=mark->next, n++ ) {
729  pmark->origin(n, iradius); // Set the origin to the nominal center
730  for ( z=0; z<pmark->sizeZ(); z++ ) {
731  oldz = z - iradius[2] + (int) mark->loc[2];
732  for ( y=0; y<pmark->sizeY(); y++ ) {
733  oldy = y - iradius[1] + (int) mark->loc[1];
734  for ( x=0; x<pmark->sizeX(); x++ ) {
735  oldx = x - iradius[0] + (int) mark->loc[0];
736  if ( oldx >= 0 && oldx < p->sizeX() && oldy >= 0 && oldy < p->sizeY() && oldz >= 0 && oldz < p->sizeZ()) {
737  j = (((img_num*p->sizeZ() + oldz)*p->sizeY() + oldy)*p->sizeX() + oldx)*p->channels();
738  i = (((n*pmark->sizeZ() + z)*pmark->sizeY() + y)*pmark->sizeX() + x)*pmark->channels();
739  for ( c=0; c<p->channels(); c++, i++, j++ ) pmark->set(i, (*p)[j]);
740  }
741  }
742  }
743  }
744  }
745 
746  pmark->statistics();
747 
748  pmark->correct_background();
749 
750  pmark->normalize(pmark->average(), pmark->standard_deviation(), 0);
751 
752  return pmark;
753 }
754 
766 long project_extract_filaments(Bproject* project, int filament_width, int axis,
767  Bstring& base, Bstring& path, Bstring& ext, int split)
768 {
769  long i(1), nfil(0);
770  Bstring filename;
771  Bfield* field;
772  Bmicrograph* mg;
773  Breconstruction* rec;
774  Bimage* p;
775 
776  if ( path.length() && access(path.c_str(), F_OK) ) {
777  cerr << "Error: The directory " << path << " must exist to extract filaments!" << endl;
778  return -1;
779  }
780 
781  if ( !path.empty() ) if ( path[-1] != '/' ) path += "/";
782 
783  if ( ext.length() < 1 ) ext = "mrc";
784 
787 
788  if ( project->select < 1 ) {
789  if ( verbose )
790  cout << "Extracting filaments from micrographs" << endl << endl;
791  for ( field = project->field; field; field = field->next ) {
792  for ( mg = field->mg; mg; mg = mg->next, i++ ) {
793  nfil = filament_count(mg->fil);
794  if ( nfil > 0 ) {
795  if ( base.length() ) {
796  if ( nmg == 1 )
797  filename = base;
798  else
799  filename = base + Bstring(i, "_%03d");
800  } else {
801  if ( mg->ffil.length() < 1 ) mg->ffil = mg->fmg;
802  filename = mg->ffil.pre_rev('.');
803  if ( mg->fmg == mg->ffil )
804  filename = filename + Bstring(mg->img_num, "_fil%03d");
805  }
806  filename = filename + "." + ext;
807  if ( filename.length() < 1 )
808  cerr << "Error in project_extract_filaments: No filament file name!" << endl;
809  if ( split ) {
810  filament_setup_filenames(mg->fil, filename, path);
811  mg->ffil = 0;
812  } else {
813  if ( path.length() ) {
814  if ( filename.contains("/") ) filename = filename.post_rev('/');
815  filename = path + "/" + filename;
816  }
817  mg->ffil = filename;
818  }
819  p = read_img(mg->fmg, 1, mg->img_num);
820  if ( !p ) return -1;
821  micrograph_extract_filaments(mg, p, filament_width, axis);
822  delete p;
823  }
824  }
825  }
826  } else {
827  if ( verbose )
828  cout << "Extracting filaments from reconstructions" << endl << endl;
829  for ( rec = project->rec; rec; rec = rec->next, i++ ) {
830  nfil = filament_count(rec->fil);
831  if ( nfil > 0 ) {
832  if ( base.length() ) {
833  if ( nrec == 1 )
834  filename = base;
835  else
836  filename = base + Bstring(i, "_%03d");
837  } else {
838  if ( rec->ffil.length() < 1 ) rec->ffil = rec->frec;
839  filename = rec->ffil.pre_rev('.');
840  if ( rec->frec == rec->ffil )
841  filename = filename + "_fil.";
842  }
843  filename = filename + "." + ext;
844  if ( filename.length() < 1 )
845  cerr << "Error in project_extract_filaments: No filament file name!" << endl;
846  if ( split ) {
847  filament_setup_filenames(rec->fil, filename, path);
848  rec->ffil = 0;
849  } else {
850  if ( path.length() ) {
851  if ( filename.contains("/") ) filename = filename.post_rev('/');
852  filename = path + "/" + filename;
853  }
854  rec->ffil = filename;
855  }
856  p = read_img(rec->frec, 1, 0);
857  if ( !p ) return -1;
858  reconstruction_extract_filaments(rec, p, filament_width, axis);
859  delete p;
860  }
861  }
862  }
863 
864  return nfil;
865 }
866 
875 long micrograph_extract_filaments(Bmicrograph* mg, Bimage* p, double width, int axis)
876 {
877  if ( width < 1 ) width = mg->filament_width;
878 
879  long nfil = count_list((char *) mg->fil);
880 
881  if ( mg->pixel_size[0] > 0 ) p->sampling(mg->pixel_size);
882 
883  Bimage* pfil = filament_extract(mg->fil, p, width, axis);
884 
885  if ( pfil ) {
886  write_img(mg->ffil, pfil, 0);
887  delete pfil;
888  }
889 
890  return nfil;
891 }
892 
901 long reconstruction_extract_filaments(Breconstruction* rec, Bimage* p, double width, int axis)
902 {
903  if ( width < 1 ) width = rec->filament_width;
904 
905  long nfil = count_list((char *) rec->fil);
906 
907  if ( rec->voxel_size[0] > 0 ) p->sampling(rec->voxel_size);
908 
909  Bimage* pfil = filament_extract(rec->fil, p, width, axis);
910 
911  if ( pfil ) {
912  write_img(rec->ffil, pfil, 0);
913  delete pfil;
914  }
915 
916  return nfil;
917 }
918 
934 Bimage* filament_extract(Bfilament* filaments, Bimage* p, double width, int axis)
935 {
936  if ( axis < 1 || axis > 3 ) { // Default axis selection
937  if ( p->sizeZ() > 1 ) axis = 3;
938  else axis = 1;
939  }
940 
941  int split(0);
942  if ( filaments->ffil.length() ) split = 1;
943 
944  long n;
945  long nfil(1), nspline;
946  double length, maxlen(0);
947  Bfilament* fil;
948  Vector3<double>* spline;
949  Vector3<long> size((int)width, (int)width, (int)width);
950  Bimage* pfil = NULL;
951  Bimage* pone = NULL;
952 
953  if ( p->sizeX() < width ) size[0] = p->sizeX();
954  if ( p->sizeY() < width ) size[1] = p->sizeY();
955  if ( p->sizeZ() < width ) size[2] = p->sizeZ();
956 
957  if ( !split ) {
958  for ( nfil=0, fil=filaments; fil; fil=fil->next ) {
959  length = filament_length(fil) + 1;
960  if ( maxlen < length ) maxlen = length;
961  nfil++;
962  }
963  size[axis-1] = (int) maxlen;
964  pfil = new Bimage(Float, p->compound_type(), size[0], size[1], size[2], nfil);
965  pfil->sampling(p->sampling(0));
966  pfil->label(p->label());
967  }
968 
969  if ( verbose & VERB_DEBUG ) {
970  cout << "DEBUG filament_extract: size=" << size << endl;
971  cout << "DEBUG filament_extract: axis=" << axis << " split=" << split << endl;
972  }
973 
974  for ( n=0, fil=filaments; fil; fil=fil->next, n++ ) {
975  spline = vector3_spline_from_nodes(fil->node, nspline);
976 // pone = img_extract_filament(p, 0, width, axis, nspline, spline);
977  pone = p->extract_filament(0, width, axis, nspline, spline);
978  pone->calculate_background();
979  if ( split ) {
980  if ( fil->ffil.length() ) write_img(fil->ffil, pone, 0);
981  else cerr << "Error: No filename for filament " << fil->id << endl;
982  } else {
983  pfil->replace(n, pone, pone->background(long(0)));
984  }
985  delete pone;
986  delete [] spline;
987  }
988 
989  return pfil;
990 }
991 
1007  double boxing_interval, double rise, double angle)
1008 {
1009  int nfil(0);
1010  Bfield* field;
1011  Bmicrograph* mg;
1012  Breconstruction* rec;
1013 
1014  if ( project->select < 1 ) {
1015  for ( field=project->field; field; field=field->next )
1016  for ( mg=field->mg; mg; mg=mg->next )
1017  nfil += micrograph_filaments_to_particles(mg, box_size, boxing_interval, rise, angle);
1018  } else {
1019  for ( rec=project->rec; rec; rec=rec->next )
1020  nfil += reconstruction_filaments_to_particles(rec, box_size, boxing_interval, rise, angle);
1021  }
1022 
1023  return nfil;
1024 }
1025 
1041  double boxing_interval, double rise, double angle)
1042 {
1043  if ( box_size[0] < 1 ) box_size[0] = 40;
1044  if ( box_size[1] < 1 ) box_size[1] = box_size[0];
1045  box_size[2] = 1;
1046 
1047  mg->box_size = box_size;
1048  if ( boxing_interval < 1 ) boxing_interval = box_size[0]/2;
1049 
1050  if ( verbose & VERB_DEBUG )
1051  cout << "DEBUG micrograph_filaments_to_particles: box_size=" << mg->box_size << endl;
1052 
1053  particle_kill(mg->part);
1054 
1055  mg->part = filaments_to_particles(mg->fil, mg->pixel_size, mg->box_size, boxing_interval, rise, angle);
1056 
1057  mg_part_links(mg);
1058 
1059  return filament_count(mg->fil);
1060 }
1061 
1077  double boxing_interval, double rise, double angle)
1078 {
1079  if ( box_size[0] < 1 ) box_size[0] = 40;
1080  if ( box_size[1] < 1 ) box_size[1] = box_size[0];
1081  if ( box_size[2] < 1 ) box_size[2] = box_size[0];
1082 
1083  rec->box_size = box_size;
1084  if ( boxing_interval < 1 ) boxing_interval = box_size[0]/2;
1085 
1086  if ( verbose & VERB_DEBUG )
1087  cout << "DEBUG micrograph_filaments_to_particles: box_size=" << rec->box_size << endl;
1088 
1089  particle_kill(rec->part);
1090 
1091  rec->part = filaments_to_particles(rec->fil, rec->voxel_size, rec->box_size, boxing_interval, rise, angle);
1092 
1093  rec_part_links(rec);
1094 
1095  return filament_count(rec->fil);
1096 }
1097 
1114  Vector3<double> pixel_size, Vector3<long> box_size,
1115  double boxing_interval, double rise, double angle)
1116 {
1117  long i, n(1), nspline, it, nfil(0);
1118  Bparticle* part_list = NULL;
1119  Bparticle* part = NULL;
1120  Bfilament* fil;
1121  Vector3<double>* spline;
1122  Vector3<double> dir;
1123 
1124  double bi, dr(rise/pixel_size[0]), phi(0), st, t, od(0);
1125 
1126  if ( verbose & VERB_FULL ) {
1127  cout << "Converting filaments to particles:" << endl;
1128  cout << "Box size: " << box_size << endl;
1129  cout << "Boxing interval: " << boxing_interval << endl;
1130  cout << "Helical rise and angle: " << rise << " A " << angle << " degrees" << endl << endl;
1131  }
1132 
1133  for ( fil=filaments; fil; fil=fil->next ) {
1134  nfil++;
1135  spline = vector3_spline_from_nodes(fil->node, nspline);
1136  for ( bi=0; bi<nspline; bi += boxing_interval, n++ ) {
1137  i = (int) (bi + 0.5);
1138  part = particle_add(&part, n);
1139  if ( !part_list ) part_list = part;
1140  part->group = fil->id;
1141  part->loc = spline[i];
1142  part->ori = box_size/2;
1143  if ( i == 0 ) dir = spline[1] - spline[0];
1144  else dir = spline[i] - spline[i-1];
1145  st = sqrt(1 - dir[2]*dir[2]);
1146  if ( angle ) {
1147  t = i/dr;
1148  it = (int) (t + 0.5);
1149  phi = angle * it;
1150  od = (t - it)*dr;
1151  part->ori -= dir*od;
1152  }
1153  part->view = View(cos(phi)*st, sin(phi)*st, dir[2], angle_set_negPI_to_PI(phi - atan2(dir[1],dir[0])));
1154 // if ( spline[i][2] < 1 ) part->ori[2] = 0;
1155  part->sel = nfil;
1156  }
1157  delete[] spline;
1158  }
1159 
1160  if ( verbose & VERB_DEBUG )
1161  cout << "DEBUG filaments_to_particles: npart=" << n << endl;
1162 
1163  return part_list;
1164 }
1165 
1177 {
1178  int npart(0);
1179  Bfield* field;
1180  Bmicrograph* mg;
1181  Breconstruction* rec;
1182 
1183  if ( project->select < 1 ) {
1184  for ( field=project->field; field; field=field->next )
1185  for ( mg=field->mg; mg; mg=mg->next )
1186  npart += mask_filament_particles(mg->fpart, mg->part, mask_width);
1187  } else {
1188  for ( rec=project->rec; rec; rec=rec->next )
1189  npart += mask_filament_particles(rec->fpart, rec->part, mask_width);
1190  }
1191 
1192  return npart;
1193 }
1194 
1209  int rotation_axis, int back_flag, int mask_width)
1210 {
1211  if ( project->select ) return 0; // No 3D yet!
1212 
1213  if ( rotation_axis < 1 || rotation_axis > 3 ) return 0;
1214 
1215  if ( project->select < 1 && rotation_axis > 2 ) return 0;
1216 
1217  int npart(0);
1218  Bfield* field;
1219  Bmicrograph* mg;
1220  Breconstruction* rec;
1221 
1222  if ( verbose ) {
1223  cout << "Rotating and masking filament particles:" << endl;
1224  cout << "Rotation axis: " << (char) (119+rotation_axis) << endl;
1225  cout << "Mask width: " << mask_width << endl;
1226  if ( back_flag ) cout << "With background correction" << endl;
1227  cout << endl;
1228  }
1229 
1230  if ( project->select < 1 ) {
1231  for ( field=project->field; field; field=field->next )
1232  for ( mg=field->mg; mg; mg=mg->next )
1233  npart += rotate_mask_filament_particles(mg->fpart, mg->part,
1234  rotation_axis, back_flag, mask_width);
1235  } else {
1236  for ( rec=project->rec; rec; rec=rec->next )
1237  npart += rotate_mask_filament_particles(rec->fpart, rec->part,
1238  rotation_axis, back_flag, mask_width);
1239  }
1240 
1241  return npart;
1242 }
1243 
1244 /*
1245 @brief Masks particles extracted from filaments.
1246 @param &filename particle file name.
1247 @param *partlist particle parameters.
1248 @param mask_width width of mask to apply.
1249 @return int number of particles masked.
1250 
1251  The orientation of the filament is inferred from the view angle and
1252  the adjacent regions are set to the background.
1253 
1254 **/
1255 int mask_filament_particles(Bstring& filename, Bparticle* partlist, int mask_width)
1256 {
1257  int npart(0);
1258  long i, n, x, y, z;
1259  double d, halfwidth = mask_width/2.0;
1260  Vector3<double> u, v;
1261  Euler euler;
1262  Bparticle* part;
1263 
1264  Bimage* p = read_img(filename, 1, -1);
1265 
1266  long imagesize = p->sizeX()*p->sizeY()*p->sizeZ()*p->channels();
1267 
1268  for ( n=0, part = partlist; part; part = part->next, n++ ) {
1269  euler = Euler(part->view);
1270  u[0] = cos(euler.psi());
1271  u[1] = -sin(euler.psi());
1272  for ( i=n*imagesize, z=0; z<p->sizeZ(); z++ ) {
1273  v[2] = z - p->image[n].origin()[2];
1274  for ( y=0; y<p->sizeY(); y++ ) {
1275  v[1] = y - p->image[n].origin()[1];
1276  for ( x=0; x<p->sizeX(); x++, i++ ) {
1277  v[0] = x - p->image[n].origin()[0];
1278  d = (u.cross(v)).length();
1279  if ( d > halfwidth ) p->set(i, p->background(n));
1280  }
1281  }
1282  }
1283  npart++;
1284  }
1285 
1286  write_img(filename, p, 0);
1287 
1288  delete p;
1289 
1290  return npart;
1291 }
1292 
1293 /*
1294 @brief Rotates and masks particles extracted from filaments.
1295 @param &filename particle file name.
1296 @param *partlist particle parameters.
1297 @param rotation_axis axis to rotate to: 1=x, 2=y, 3=z.
1298 @param back_flag background correction flag.
1299 @param mask_width width of mask to apply.
1300 @return int number of particles masked.
1301 
1302  The orientation of the filament is inferred from the view angle and
1303  the particle is rotated to orient the filament axis along a cartesian axis.
1304  The mask is applied and adjacent regions are set to the background.
1305 **/
1307  int rotation_axis, int back_flag, int mask_width)
1308 {
1309  int npart(0);
1310  long i, j, n, x, y, z;
1311  double d(0), dx(0), dy(0), dz(0), halfwidth;
1312  Euler euler;
1313  Bparticle* part;
1314  Bimage* pex;
1315  Bimage* p = read_img(filename, 1, -1);
1316 
1317  if ( !p ) {
1318  error_show("Error in rotate_mask_filament_particles: No image!", __FILE__, __LINE__);
1319  return -1;
1320  }
1321 
1322  p->change_type(Float);
1323 
1324  if ( mask_width < 1 ) mask_width = (p->sizeX() > p->sizeY())? p->sizeX(): p->sizeY();
1325  halfwidth = mask_width/2.0;
1326 
1327  long imagesize = p->sizeX()*p->sizeY()*p->sizeZ()*p->channels();
1328 
1329  for ( n=0, part = partlist; part; part = part->next, n++ ) {
1330  pex = p->extract(n);
1331  pex->image[0] = p->image[n];
1332  euler = Euler(part->view);
1333  euler[1] = euler[2] = 0;
1334  if ( rotation_axis == 2 ) euler[0] += M_PI_2;
1335  pex->rotate(euler.matrix());
1336 // img_rotate(pex, euler);
1337 // pex->rotate(euler.view());
1338  for ( i=n*imagesize, j=0, z=0; z<p->sizeZ(); z++ ) {
1339  if ( p->sizeZ() > 1 ) {
1340  dz = fabs(z - p->image[n].origin()[2]);
1341  dz *= dz;
1342  }
1343  for ( y=0; y<p->sizeY(); y++ ) {
1344  dy = fabs(y - p->image[n].origin()[1]);
1345  if ( p->sizeZ() > 1 ) dy *= dy;
1346  for ( x=0; x<p->sizeX(); x++, i++, j++ ) {
1347  dx = fabs(x - p->image[n].origin()[0]);
1348  if ( p->sizeZ() < 2 ) {
1349  if ( rotation_axis == 2 ) d = dx;
1350  else d = dy;
1351  } else {
1352  dx *= dx;
1353  d = sqrt(dx + dy + dz);
1354  }
1355  if ( d > halfwidth ) p->set(i, p->background(n));
1356  else p->set(i, (*pex)[j]);
1357  }
1358  }
1359  }
1360  part->view[3] -= euler.psi();
1361  delete pex;
1362  npart++;
1363  }
1364 
1365  if ( back_flag )
1366  p->correct_background();
1367 
1368  write_img(filename, p, 0);
1369 
1370  delete p;
1371 
1372  return npart;
1373 }
1374 
filaments_to_particles
Bparticle * filaments_to_particles(Bfilament *filaments, Vector3< double > pixel_size, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1113
Bimage::extract_filament
Bimage * extract_filament(long img_num, double width, int axis, long nspline, Vector3< double > *spline)
Extracts a filament defined by a series of coordinates.
Definition: Bimage_extract.cpp:1128
Bbadarea::loc
Vector3< double > loc
Definition: mg_processing.h:146
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
rec_part_links
int rec_part_links(Breconstruction *rec)
Sets the links from particles to reconstructions.
Definition: mg_processing.cpp:265
spline.h
Header file for spline interpolation.
reconstruction_filaments_to_particles
int reconstruction_filaments_to_particles(Breconstruction *rec, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1076
Bmicrograph::filament_width
double filament_width
Definition: mg_processing.h:234
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:291
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:473
Breconstruction::fil
Bfilament * fil
Definition: mg_processing.h:308
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring::count
long count(const char c)
Definition: Bstring.cpp:269
Bproject::select
int select
Definition: mg_processing.h:342
UCharacter
@ UCharacter
Definition: utilities.h:153
Bparticle::view
View view
Definition: mg_processing.h:102
Bfilament
Definition: mg_processing.h:129
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
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bparticle
Definition: mg_processing.h:90
Bsub_image::standard_deviation
double standard_deviation()
Definition: Bimage.h:120
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
Breconstruction::filament_width
double filament_width
Definition: mg_processing.h:300
Bmicrograph::bad_radius
double bad_radius
Definition: mg_processing.h:236
project_count_reconstructions
long project_count_reconstructions(Bproject *project)
Counts all the reconstructions in the project structure.
Definition: mg_select.cpp:384
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bfilament::next
Bfilament * next
Definition: mg_processing.h:130
Euler::psi
double psi()
Definition: Euler.h:56
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
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
Bstring::empty
bool empty()
Definition: Bstring.h:71
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
Bparticle::mg
Bmicrograph * mg
Definition: mg_processing.h:105
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
project_filaments_to_particles
int project_filaments_to_particles(Bproject *project, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1006
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
Breconstruction::mark_radius
double mark_radius
Definition: mg_processing.h:304
Bfield
Definition: mg_processing.h:260
length
double length(vector< T > v)
Definition: Vector3.h:530
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::correct_background
int correct_background(long nn, int flag)
Corrects the background for one sub-image.
Definition: Bimage_background.cpp:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
M_PI_2
#define M_PI_2
Definition: utilities.h:66
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
Bbadarea
Definition: mg_processing.h:143
project_mask_filament_particles
int project_mask_filament_particles(Bproject *project, int mask_width)
Masks particles extracted from filaments.
Definition: mg_extract.cpp:1176
mg_part_links
int mg_part_links(Bmicrograph *mg)
Sets the links from particles to micrographs.
Definition: mg_processing.cpp:249
filament_setup_filenames
int filament_setup_filenames(Bfilament *fil, Bstring filename, Bstring filpath)
Sets up individual filament file names for extraction.
Definition: mg_extract.cpp:86
Bmarker
Definition: marker.h:21
Bmicrograph::fil
Bfilament * fil
Definition: mg_processing.h:243
project_extract_particles
long project_extract_particles(Bproject *project, double scale, int back_flag, int norm_flag, int fill_type, double fill, int mask_width, int split, Bstring &partbase, Bstring &partpath, Bstring &partext)
Extracts particle images from micrographs defined in a project.
Definition: mg_extract.cpp:161
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
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
Bimage::project
Bimage * project(char axis, int flags=1)
Projects a 3D image to a 2D image down one of the three major axes.
Definition: Bimage_project.cpp:31
Bfilament::id
int id
Definition: mg_processing.h:132
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bmicrograph::ffil
Bstring ffil
Definition: mg_processing.h:209
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
particle_extract
Bimage * particle_extract(Bparticle *particles, Bbadarea *bad_areas, Bimage *p, Vector3< long > size, double scale, double bad_radius, int back_flag, int norm_flag, double fill, int mask_width)
Extracts particle images from an image.
Definition: mg_extract.cpp:491
verbose
int verbose
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
Bfilament::node
Bfilnode * node
Definition: mg_processing.h:133
Bproject
Definition: mg_processing.h:336
particle_setup_filenames
int particle_setup_filenames(Bparticle *part, Bstring filename, Bstring partpath)
Sets up individual particle file names for extraction.
Definition: mg_extract.cpp:36
project_extract_filaments
long project_extract_filaments(Bproject *project, int filament_width, int axis, Bstring &base, Bstring &path, Bstring &ext, int split)
Extracts filament images from micrographs defined in a project.
Definition: mg_extract.cpp:766
Bbadarea::next
Bbadarea * next
Definition: mg_processing.h:144
Bparticle::sel
long sel
Definition: mg_processing.h:104
Bparticle::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:99
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
project_rotate_mask_filament_particles
int project_rotate_mask_filament_particles(Bproject *project, int rotation_axis, int back_flag, int mask_width)
Rotates and masks particles extracted from filaments.
Definition: mg_extract.cpp:1208
Bstring::truncate
Bstring truncate(const long n)
Definition: Bstring.cpp:503
vector3_spline_from_nodes
Vector3< double > * vector3_spline_from_nodes(Bfilnode *fnode, long &nspline)
Calculates a spline curve from a set of filament nodes.
Definition: mg_extract.cpp:128
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
Bfilament::ffil
Bstring ffil
Definition: mg_processing.h:131
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:100
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
mg_extract.h
Header file for functions to extract particles from micrographs.
marker_extract_gold
Bimage * marker_extract_gold(Bmarker *marker_list, Bimage *p, int img_num, double radius)
Extracts gold particle images from an image.
Definition: mg_extract.cpp:702
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
Bfilnode::next
Bfilnode * next
Definition: mg_processing.h:117
reconstruction_project_extract_particles
Bparticle * reconstruction_project_extract_particles(Breconstruction *rec, Bimage *p, double scale, int back_flag, int norm_flag, double fill, int mask_width)
Extracts and projects 3D particle images from a reconstruction and return a 2D list.
Definition: mg_extract.cpp:423
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
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
vector3_catmull_rom_spline
Vector3< double > * vector3_catmull_rom_spline(long ncoord, Vector3< double > *coords, long &nspline)
Calculates a 2D/3D spline curve using the Catmull-Rom algorithm.
Definition: spline.cpp:33
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
particle_add
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Definition: mg_processing.cpp:221
micrograph_extract_gold
Bimage * micrograph_extract_gold(Bmicrograph *mg, Bimage *p, double radius)
Extracts gold particle images from a micrograph.
Definition: mg_extract.cpp:665
filament_extract
Bimage * filament_extract(Bfilament *filaments, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:934
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
Bparticle::group
int group
Definition: mg_processing.h:94
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
micrograph_extract_filaments
long micrograph_extract_filaments(Bmicrograph *mg, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:875
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:302
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
linked_list.h
Header file for generalized linked list functions.
reconstruction_extract_filaments
long reconstruction_extract_filaments(Breconstruction *rec, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:901
Breconstruction::ffil
Bstring ffil
Definition: mg_processing.h:292
Bfilnode::loc
Vector3< double > loc
Definition: mg_processing.h:119
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
mask_filament_particles
int mask_filament_particles(Bstring &filename, Bparticle *partlist, int mask_width)
Definition: mg_extract.cpp:1255
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:614
Breconstruction::bad
Bbadarea * bad
Definition: mg_processing.h:309
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
marker_list
Tcl_Obj * marker_list(Bproject *project)
Definition: tcltk_bmarker.cpp:581
micrograph_extract_particles
long micrograph_extract_particles(Bmicrograph *mg, Bimage *p, double scale, int back_flag, int norm_flag, double fill, int mask_width)
Extracts particle images from a micrograph.
Definition: mg_extract.cpp:352
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Vector3::min
Type min()
Definition: Vector3.h:169
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
Euler::matrix
Matrix3 matrix()
Definition: Euler.h:67
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
reconstruction_extract_gold
Bimage * reconstruction_extract_gold(Breconstruction *rec, Bimage *p, double radius)
Extracts gold particle images from a reconstruction.
Definition: mg_extract.cpp:683
reconstruction_extract_particles
long reconstruction_extract_particles(Breconstruction *rec, Bimage *p, double scale, int back_flag, int norm_flag, double fill, int mask_width)
Extracts particle images from a reconstruction.
Definition: mg_extract.cpp:386
filament_count
long filament_count(Bfilament *fil)
Counts the number of filaments in a linked list of filaments.
Definition: mg_select.cpp:791
Bfilnode
Definition: mg_processing.h:116
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:528
Vector3::length
double length()
Definition: Vector3.h:222
Bmicrograph::bad
Bbadarea * bad
Definition: mg_processing.h:244
particle_count
long particle_count(Bparticle *part)
Counts the number of particles in a linked list.
Definition: mg_select.cpp:755
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Euler
Definition: Euler.h:28
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bparticle::rec
Breconstruction * rec
Definition: mg_processing.h:106
micrograph_filaments_to_particles
int micrograph_filaments_to_particles(Bmicrograph *mg, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1040
rotate_mask_filament_particles
int rotate_mask_filament_particles(Bstring &filename, Bparticle *partlist, int rotation_axis, int back_flag, int mask_width)
Definition: mg_extract.cpp:1306
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
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
filament_length
double filament_length(Bfilament *fil)
Calculates the length of a filament.
Definition: mg_processing.cpp:375
Vector3::cross
Vector3 cross(const Vector3 &v)
Definition: Vector3.h:252
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
Bimage::label
void label(string s)
Definition: Bimage.h:250
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
Vector3::max
Type max()
Definition: Vector3.h:175