Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_tomography.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "marker.h"
11 #include "img_tomography.h"
12 #include "math_util.h"
13 #include "Matrix.h"
14 #include "trigonometry.h"
15 #include "linked_list.h"
16 #include "utilities.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
32 {
33  Bimage* p = new Bimage(Float, TSimple, size, 1);
34  Vector3<double> origin(p->size()/2);
35  double outrad = sqrt(2.0)*radius;
36 
37  p->sphere(origin, outrad, 2, FILL_USER, 1);
38  p->sphere(origin, radius, 2, FILL_USER, -1);
39 
40  p->origin(origin);
41 
42  if ( verbose & VERB_DEBUG ) {
43  cout << "DEBUG img_gold_particle: rad=" << radius << endl;
44  write_img("gold.map", p);
45  }
46 
47  p->statistics();
48 
49  return p;
50 }
51 
71 Bmarker* img_find_gold_particles(Bimage* p, int img_select, double radius, size_t edge, double cutoff)
72 {
73  if ( radius < 1 ) return NULL;
74 
75  int img_start(0);
76  int img_end(p->images());
77  if ( img_select > -1 ) {
78  if ( img_select >= p->images() ) img_select = p->images() - 1;
79  img_start = img_select;
80  img_end = img_start + 1;
81  }
82 
83  Bimage* pgold = img_gold_particle(p->size(), radius);
84 
85  size_t i, j, x, y, z, n, nn, np(0);
86  long xx, yy, zz;
87  double max, fom_avg(0), fom_std(0), fom_max(0);
88  Vector3<size_t> ve(p->size()/2);
89  ve = ve.min(edge);
90 
91  if ( verbose & VERB_LABEL ) {
92  cout << "Finding particles:" << endl;
93  cout << "Particle radius: " << radius*p->sampling().x()
94  << " A " << radius << " pixels" << endl;
95  cout << "Edge: " << edge << " pixels" << endl;
96  }
97 
98  if ( verbose & VERB_DEBUG )
99  cout << "DEBUG img_find_gold_particles: radius=" << radius << endl;
100 
101  Bimage* pone = NULL;
102  Bimage* pcc = NULL;
103 
104  size_t imgsize(p->sizeX()*p->sizeY()*p->sizeZ());
105  size_t fomsize(imgsize);
106  if ( img_select < 0 ) fomsize *= p->images();
107  float* fom_one;
108  float* fom = new float[fomsize];
109  for ( i=0; i<fomsize; i++ ) fom[i] = 0;
110 
111 // if ( verbose & VERB_PROCESS )
112 // cout << "Image\tx\ty\tz" << endl;
113  for ( i=0, n=img_start; n<img_end; n++ ) { // i is the external counter
114  pone = p->extract(n);
115  pcc = pone->cross_correlate(pgold, 2*p->sampling().x(), 8*radius*p->sampling().x());
116  delete pone;
117  pcc->find_peaks(3);
118  fom_one = pcc->FOM();
119  for ( j=0; j<imgsize; j++, i++ ) if ( fom_one[j] ) {
120  fom[i] = fom_one[j];
121  fom_avg += fom[i];
122  fom_std += fom[i]*fom[i];
123  if ( fom_max < fom[i] ) fom_max = fom[i];
124  np++;
125  }
126  delete pcc;
127  if ( verbose & VERB_PROCESS )
128  cout << "Image " << n << ": " << np << " gold fiducials" << endl;
129  }
130 
131  fom_avg /= np;
132  fom_std = fom_std/np - fom_avg*fom_avg;
133  if ( fom_std > 0 ) fom_std = sqrt(fom_std);
134  else fom_std = 0;
135  if ( cutoff == 0 ) cutoff = fom_avg + fom_std;
136  else if ( cutoff < 0 ) cutoff = fom_max/2;
137  if ( verbose )
138  cout << "Cutoff = " << cutoff << endl;
139 
140  Bmarker* mark = NULL;
141  Bmarker* m = NULL;
142 
143  if ( np ) {
144  if ( verbose )
145  cout << "Marker\tx\ty\tz\tn\tFOM" << endl;
146  for ( j=nn=0, n=img_start; n<img_end; n++, nn++ ) {
147  for ( z=0; z<p->sizeZ(); z++ ) {
148  zz = (long)z - pgold->image->origin().z();
149  if ( zz < 0 ) zz += p->sizeZ();
150  if ( zz >= ve.z() && zz < p->sizeZ()-ve.z() )
151  for ( y=0; y<p->sizeY(); y++ ) {
152  yy = (long)y - pgold->image->origin().y();
153  if ( yy < 0 ) yy += p->sizeY();
154  if ( yy >= ve.y() && yy < p->sizeY()-ve.y() )
155  for ( x=0; x<p->sizeX(); x++ ) {
156  xx = (long)x - pgold->image->origin().x();
157  if ( xx < 0 ) xx += p->sizeX();
158  if ( xx >= ve.x() && xx < p->sizeX()-ve.x() ) {
159  i = p->index(x, y, z, nn);
160  if ( fom[i] > cutoff ) {
161  m = (Bmarker *) add_item((char **) &m, sizeof(Bmarker));
162  if ( !mark ) mark = m;
163  j++;
164  m->id = j;
165  m->loc[0] = x - pgold->image->origin().x();
166  m->loc[1] = y - pgold->image->origin().y();
167  m->loc[2] = z - pgold->image->origin().z();
168  m->img_num = n;
169  m->sel = 1;
170  m->fom = fom[i];
171  if ( m->loc.x() < 0 ) m->loc[0] += p->sizeX();
172  if ( m->loc.y() < 0 ) m->loc[1] += p->sizeY();
173  if ( m->loc.z() < 0 ) m->loc[2] += p->sizeZ();
174  if ( verbose )
175  cout << m->id << tab << m->loc.x() << tab <<
176  m->loc.y() << tab << m->loc.z() << tab <<
177  m->img_num << tab << m->fom << endl;
178  }
179  }
180  }
181  }
182  }
183  }
184  }
185 
186  delete pgold;
187 
188  delete[] fom;
189 
190  if ( verbose & VERB_LABEL )
191  cout << "Number of particles found: " << j << endl << endl;
192 
193  return mark;
194 }
195 
210  double radius, Bimage* pmark, Bmarker* mark)
211 {
212  double outrad = sqrt(2.0)*radius;
213  Bmarker* m;
214  Vector3<double> loc;
215 
216  Bimage* p = new Bimage(Float, TSimple, size, 1);
217  p->origin(p->size()/2);
218 
219  if ( pmark ) {
220  p->fill(pmark->image->background());
221  for ( m = mark; m; m = m->next ) if ( m->sel )
222  p->place(0, pmark, (m->loc - offset), outrad, 1, 0, 1);
223  } else {
224  for ( m = mark; m; m = m->next ) if ( m->sel ) {
225  loc = m->loc - offset;
226  p->sphere(loc, outrad, 2, FILL_USER, 1);
227  p->sphere(loc, radius, 2, FILL_USER, -1);
228  }
229  }
230 
231  p->statistics();
232 
233 // write_img("mark_proj.map", p);
234 
235  return p;
236 }
237 
251 int img_stretch(Bimage* p, double axis, double tilt)
252 {
253  p->change_type(Float);
254 
255  unsigned long i, j, x, y, z, n, xx, yy;
256  unsigned long datasize = p->data_size();
257  long ix, iy;
258  double dx, dy, a, v, oldx, oldy, fx, fy;
259  double cos_axis = bcos(axis);
260  double sin_axis = bsin(axis);
261  double cos_tilt = bcos(tilt);
262  float* sdata = new float[datasize];
263 
264  if ( verbose & VERB_FULL )
265  cout << "Stretching image by " << cos_tilt << endl;
266 
267  for ( i=n=0; n<p->images(); n++ ) {
268  for ( z=0; z<p->sizeZ(); z++ ) {
269  for ( y=0; y<p->sizeY(); y++ ) {
270  dy = y - p->image[n].origin().y();
271  for ( x=0; x<p->sizeX(); x++, i++ ) {
272  dx = x - p->image[n].origin().x();
273  a = atan2(dy, dx) - axis;
274  v = sqrt(dx*dx + dy*dy);
275  oldx = v*(bcos(a)*cos_axis - cos_tilt*bsin(a)*sin_axis) + p->image[n].origin().x();
276  oldy = v*(bcos(a)*sin_axis + cos_tilt*bsin(a)*cos_axis) + p->image[n].origin().y();
277  ix = (long) oldx;
278  iy = (long) oldy;
279  sdata[i] = 0;
280  if ( ix >= 0 && ix < p->sizeX()-1 && iy >= 0 && iy < p->sizeY()-1 ) {
281  fx = oldx - ix;
282  fy = oldy - iy;
283  for ( yy=iy; yy<iy+2; yy++ ) {
284  fy = 1 - fy;
285  for ( xx=ix; xx<ix+2; xx++ ) {
286  fx = 1 - fx;
287 // j = ((n*p->z + z)*p->sizeY() + yy)*p->x + xx;
288  j = p->index(0,xx,yy,z,n);
289  sdata[i] += fx*fy* (*p)[j];
290  }
291  }
292  }
293  }
294  }
295  }
296  }
297 
298  p->data_assign((unsigned char *) sdata);
299 
300  return 0;
301 }
302 
314 int img_clear_extraneous_areas(Bimage* p, double tilt_axis, double tilt_angle, size_t thickness)
315 {
316  if ( p->sizeZ() > 1 ) return -1;
317  if ( p->images() > 1 ) return -1;
318 
319  unsigned long i, x, y, c;
320 
321  p->calculate_background((size_t)0);
322 
323  if ( verbose & VERB_FULL )
324  cout << "Clearing extraneous areas from micrograph " << p->file_name() << endl;
325 
326  Vector3<double> v(-bsin(tilt_axis), bcos(tilt_axis), 0);
327  double a, b, d = 1e30;
328  if ( v.x() && d > fabs(p->sizeX()*0.5/v.x()) ) d = fabs(p->sizeX()*0.5/v.x());
329  if ( v.y() && d > fabs(p->sizeY()*0.5/v.y()) ) d = fabs(p->sizeY()*0.5/v.y());
330 
331  a = d*bcos(tilt_angle) + fabs(0.5*thickness*sin(tilt_angle));
332 
333  for ( i=y=0; y<p->sizeY(); y++ ) {
334  for ( x=0; x<p->sizeX(); x++ ) {
335  b = fabs(v.x()*(x - p->image->origin().x()) + v.y()*(y - p->image->origin().y()));
336  for ( c=0; c<p->channels(); c++, i++ )
337  if ( b > a ) p->set(i, p->image->background());
338  }
339  }
340 
341  return 0;
342 }
343 
344 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1544
Type z()
Definition: Vector3.h:139
void sizeZ(size_t nz)
Definition: Bimage.h:332
Header file for image class.
double bcos(double x)
Avoid non-zero values for cos(PI/2) and cos(-PI/2).
int place(size_t nn, Bimage *p, Vector3< double > loc, double radius, double scale, double shift, int operation)
Places a small image into a large image.
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
#define VERB_DEBUG
Definition: utilities.h:105
double background()
Definition: Bimage.h:126
Definition: marker.h:21
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Vector class for 3-value vectors used in 3D space. The internal variables are an array of 3 numbers...
Definition: Quaternion.h:17
size_t find_peaks(size_t kernelsize)
Finds peaks in a map with periodic boundaries.
Generalized matrix class.
size_t index(size_t nx, size_t ny) const
Definition: Bimage.h:336
#define VERB_PROCESS
Definition: utilities.h:100
Vector3< size_t > size() const
Definition: Bimage.h:327
size_t statistics()
Calculates the statistics for an image.
long b
Definition: jpegint.h:371
double bsin(double x)
Avoid non-zero values for sin(PI) and sin(-PI).
Definition: Bimage.h:51
General image parameter class.
Definition: Bimage.h:152
Header file for generalized linked list functions.
int img_stretch(Bimage *p, double axis, double tilt)
Stretches an image to what it should correspond to at 0 degree tilt.
size_t channels()
Definition: Bimage.h:325
void sampling(double s)
Definition: Bimage.h:440
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.
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:623
float * FOM()
Definition: Bimage.h:239
int verbose
Type min()
Definition: Vector3.h:143
Type x()
Definition: Vector3.h:137
float fom
Definition: marker.h:28
void file_name(Bstring s)
Definition: Bimage.h:213
Bimage * cross_correlate(Bimage *p, Bimage *pmask=NULL)
Definition: Bimage.h:780
int id
Definition: marker.h:23
Header file for trigonometry functions.
#define FILL_USER
Definition: utilities.h:165
Bimage * img_marker_projection(Vector3< size_t > size, Vector3< double > offset, double radius, Bimage *pmark, Bmarker *mark)
Generates a projection image of the global marker model.
void origin(double x, double y, double z)
Definition: Bimage.h:132
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:977
Bimage * extract(size_t nn)
Extracts one sub-image into new image structure.
Bmarker * next
Definition: marker.h:22
int write_img(const char *filename, Bimage *p)
General driver function to write multiple image formats This is the only image writing function that ...
Definition: rwimg.cpp:255
size_t images()
Definition: Bimage.h:438
size_t data_size()
Definition: Bimage.h:225
void sizeX(size_t nx)
Definition: Bimage.h:330
int img_num
Definition: marker.h:24
void sizeY(size_t ny)
Definition: Bimage.h:331
Vector3< float > loc
Definition: marker.h:25
Bmarker * img_find_gold_particles(Bimage *p, int img_select, double radius, size_t edge, double cutoff)
Finds gold particles in micrograph images.
int img_clear_extraneous_areas(Bimage *p, double tilt_axis, double tilt_angle, size_t thickness)
Clears areas from a micrograph not used in a reconstruction.
void fill(double v)
Definition: Bimage.h:585
#define VERB_FULL
Definition: utilities.h:102
#define VERB_LABEL
Definition: utilities.h:99
Bimage * img_gold_particle(Vector3< size_t > size, double radius)
Generates an image representing a gold particle.
Type y()
Definition: Vector3.h:138
Header file for tomographic applications.
Header file for image parameter functions.
int sel
Definition: marker.h:29
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bsub_image * image
Definition: Bimage.h:183
void origin(double ox, double oy, double oz)
Definition: Bimage.h:483
Header file for general utilities.