| Bsoft 2.0.5 Bernard's software package

img_radon.cpp
Go to the documentation of this file.
1
12 #include "kernlut.h"
13 #include "gal_ricoI.h"
14 #include "sin_gal.h"
15 #include "pocs_tool.h"
16 #include "proj_tool.h"
17 #include "radon_util.h"
18 #include "img_radon.h"
19
20 #include "mg_processing.h"
21 #include "Bimage.h"
22 #include "symmetry.h"
23 #include "Matrix.h"
24 #include "linked_list.h"
25 #include "utilities.h"
26
27 // Declaration of global variables
28 extern int verbose; // Level of output to the screen
29
53 Bimage *
54 img_radon_transform (Bimage * p, int type, int nkernel, int kernel_power, int padd, int ntheta)
55 {
56 char order[12] = "xyz";
57 Vector3<double> shift;
58
59 kernel *ker = kernel_prep (nkernel, kernel_power);
60
61 p->change_type(Float);
62
63 if (type != 2)
64 sphere (p);
65 else
66 mean_to_0(p);
67
68 int nang = 180;
69
70 if (type == 1 || type == 3)
71 nang = 360;
72
73 if (verbose & (VERB_LABEL | VERB_PROCESS))
74 cout << "Forward radon transform:" << endl;
75
76 if (verbose & VERB_PROCESS)
77 {
78 cout << "Kernel size and power: " << nkernel << " " << kernel_power << endl;
79 cout << "Type: " << type << endl;
80 cout << "Number of angles: " << ntheta << endl;
81 cout << "Padding: " << padd << endl;
82 if (type != 3)
83 cout << "Transform size: PI x PI\n" << endl;
84 else
85 cout << "Transform size: 2 PI x 2 PI\n" << endl;
86 }
87
88 Bimage *prad = p;
89 Bimage *prad2 = NULL;
90
91 if (type == 2)
92 {
93 ntheta = prad->sizeZ();
94 // prad->z /= 2;
95 }
96 else
97 {
98 strcpy (order, "zxy");
99 p->reslice(order);
100 //write_img("p.pif", p);
101
102 prad = sin_gal (p, nang, shift, ker, padd, ntheta);
103 //write_img("prad.pif", prad);
104 strcpy (order, "xzy");
105 prad->reslice(order);
106 }
107
108 if (type != 1)
109 {
110 prad2 = sin_gal (prad, nang, shift, ker, padd, ntheta);
111 if (prad != p) delete prad;
112 prad = prad2;
113
114 strcpy (order, "yzx");
115 prad->reslice(order);
116 }
117
118 free (ker->kern);
119 free (ker);
120
121 return (prad);
122 }
123
140 Bimage *
141 img_radon_inverse_transform (Bimage * p, int type, int nkernel, int kernel_power, int padd)
142 {
143 char order[12] = "xyz";
144
145 kernel *ker = kernel_prep (nkernel, kernel_power);
146
147 p->change_type(Float);
148
149 mean_to_0(p);
150
151 if (verbose & (VERB_LABEL | VERB_PROCESS))
152 cout << "Backward radon transform:" << endl;
153
154 if (verbose & VERB_PROCESS)
155 {
156 cout << "Kernel size and power: " << nkernel << " " << kernel_power << endl;
157 cout << "Type: " << type << endl;
158 cout << "Padding: " << padd << endl << endl;
159 }
160
161 Bimage *pinv = p;
162 Bimage *pinv2;
163
164 if (type == 2)
165 {
166 pinv = copy_I_part_rad (p);
167 copy_II_part_rad (pinv);
168 // p = pinv;
169 }
170
171 if (type != 1)
172 {
173 strcpy (order, "zxy");
174 p->reslice(order);
175
176 pinv2 = gal_ricoI (pinv, type, ker, padd);
177 if ( p != pinv ) delete pinv;
178 pinv = pinv2;
179 }
180
181 if (type != 2)
182 {
183
184 strcpy (order, "xzy");
185 pinv->reslice(order);
186
187 pinv2 = gal_ricoI (pinv, type, ker, padd);
188 if (p != pinv) delete pinv;
189 pinv = pinv2;
190
191 strcpy (order, "yzx");
192 pinv->reslice(order);
193 }
194
195 free (ker->kern);
196 free (ker);
197
198 return (pinv);
199 }
200
220 int
221 img_radon_pocs_filter (Bimage * p, int n_cyc_out, int n_cyc_in, double rad_3D, double rad_plane, int support, Bimage * pmask)
222 {
223 char order[12] = "xyz";
224 int i;
225
226 p->change_type(Float);
227
228 mean_to_0(p);
229
230 if (verbose & (VERB_LABEL | VERB_PROCESS))
231 cout << "POCS filtering of a radon transform:" << endl;
232
233 if (verbose & VERB_PROCESS)
234 {
235 cout << "Outer and inner cycles: " << n_cyc_out << " " << n_cyc_in << endl;
236 cout << "Limiting radii, 3D and plane: " << rad_3D << " " << rad_plane << endl;
237 cout << "Finiteness support: " << support << endl << endl;
238 }
239
240 // load Radon transform like theta,phi,rho //
241 for (i = 0; i < n_cyc_out; i++)
242 {
243
244 if (support)
245 support_f (p, rad_3D);
246
247 // execute T2 --> rho,theta,phi //
248 strcpy (order, "zxy");
249 p->reslice(order);
250
251 filter_X_mask (p, pmask, n_cyc_in, rad_3D, 0);
252
253 // go to rho,phi,theta //
254 strcpy (order, "xzy");
255 p->reslice(order);
256
257 filter_X_mask (p, pmask, n_cyc_in, rad_plane, 1);
258
259 // come back at theta,phi,rho //
260 strcpy (order, "zyx");
261 p->reslice(order);
262 }
263
264 if (support)
265 support_f (p, rad_3D);
266
267 return 0;
268 }
269
291 Bimage *
292 img_radon_reconstruction (Bproject * project, Bsymmetry * sym, Bstring& file_mask, int rec_size, int ntheta, int table_size, double threshold, Vector3<double> origin, int nkernel, int kernel_power)
293 {
294 int i, padd;
295 double x, y, z, f, t, t1;
296
297 Bfield *field = project->field;
298 Bmicrograph *mg = field->mg;
299 Bparticle *part = mg->part;
300
301 if ( mg->fpart.length() < 1 )
302 {
303 cout << "No particle image file name given!" << endl << endl;
304 exit (-1);
305 }
306
307 kernel *ker = kernel_prep (nkernel, kernel_power);
308
309 Bimage *p = NULL;
310 if (rec_size < 1)
311 {
312 p = read_img (mg->fpart, 0, -1);
313 rec_size = findNextPowerOf(p->sizeX(), 2);
314 delete p;
315 }
316
317 ntheta = rec_size * 2;
318
319 if ( origin[0] < 1 ) origin[0] = rec_size / 2;
320 if ( origin[1] < 1 ) origin[1] = rec_size / 2;
321 if ( origin[2] < 1 ) origin[2] = rec_size / 2;
322
323 if (verbose & (VERB_LABEL | VERB_PROCESS))
324 cout << "Radon transform reconstruction from particle images:" << endl;
325
326 if (verbose & VERB_PROCESS)
327 {
328 cout << "Reconstruction size: " << ntheta / 2 << " " << ntheta / 2 << " " << rec_size << endl;
329 cout << "Symmetry: " << sym->label << endl;
330 cout << "Kernel size and power: " << nkernel << " " << kernel_power << endl;
331 cout << "Number of angles: " << ntheta << endl;
332 cout << "Threshold: " << threshold << endl;
333 if (file_mask.length())
334 cout << "Mask file: " << file_mask << endl << endl;
335 else
336 cout << endl;
337 }
338
339 LUTable *lut = create_table (rec_size, table_size);
340
341 Bimage *prec = new Bimage (Float, TSimple, ntheta / 2, ntheta / 2, rec_size, 1);
342 prec->image->origin(origin);
343
344 Bimage *pmask = new Bimage (Float, TSimple, ntheta / 2, ntheta / 2, 1, 1);
345
346 if (verbose & VERB_PROCESS)
347 cout << "Reconstruction dimensions: " << prec->size() << endl;
348
349 float *priga;
350 if ((priga = (float *) calloc (prec->sizeZ(), sizeof (float))) == 0)
351 {
352 perror ("img_radon_reconstruction: priga error calloc");
353 exit (0);
354 }
355
356 char order[12] = "yxz";
357 int psel = 0;
358 Vector3<double> d;
359 Bimage *prad;
360 Euler euler;
361 View *view, *v;
362 int nview = 1;
363 for (i = 0; i < sym->n; i++)
364 nview *= sym->op[i].order;
365
366 for (field = project->field; field; field = field->next)
367 {
368 for (mg = field->mg; mg; mg = mg->next)
369 {
370 for (part = mg->part; part; part = part->next)
371 {
372 psel = 0;
373 if (threshold)
374 {
375 if (part->fom[0] >= threshold)
376 psel = 1;
377 }
378 else
379 {
380 if (part->sel)
381 psel = 1;
382 }
383 if (psel) // Use only selected images
384 {
385 p = read_img (mg->fpart, 1, part->id - 1);
386 if (!p)
387 {
388 cerr << "Error: Image file " << mg->fpart << " not read!" << endl;
389 exit (-1);
390 }
391 p->change_type(Float);
392 p->background(0.0);
393 p->image->origin(part->ori);
394 img_resize_to_next_power2(p, FILL_BACKGROUND, p->background(long(0)));
395
396 padd = 1;
397 if ( p->image[0].origin()[0] > 0 && p->image[0].origin()[1] )
398 d = origin - p->image->origin();
399 prad = sin_gal (p, 180, d, ker, padd, ntheta);
400 delete p;
401
402 prad->reslice(order);
403 view = symmetry_get_all_views (sym, part->view);
404
405 for ( v=view; v; v=v->next )
406 {
407 euler = Euler(*v);
408 for (i = 0; i < ntheta / 2; i++)
409 {
410 t1 = (float) i *2. * M_PI / ntheta; // sinogram step //
411 x = 1.;
412 y = 0.;
413 z = 0.;
414 rotZ_cart (x, y, z, t1, &x, &y, &z);
415 rotZ_cart (x, y, z, euler.psi(), &x, &y, &z);
416 rotY_cart (x, y, z, euler.theta(), &x, &y, &z);
417 rotZ_cart (x, y, z, euler.phi(), &x, &y, &z);
418 f = asin (y);
419 t = atan2 (x, z);
420 write_line (prad, prec, i, f, t, pmask, lut, priga);
421 }
422 }
423 delete prad;
424 kill_list((char *) view, sizeof(View));
425 }
426 }
427 }
428 }
429
430 free (priga);
431
432 kill_table (lut);
433
434 weigh_radon_transf (prec, pmask);
435
436 if (file_mask.length())
437 write_img(file_mask, pmask, 0);
438
439 delete pmask;
440
441 free (ker->kern);
442 free (ker);
443
444 return (prec);
445 }
446
459 int img_resize_to_next_power2(Bimage* p, int fill_type, double fill)
460 {
461 Vector3<int> newsize(1,1,1);
462
463 if ( p->sizeX() > 1 ) newsize[0] = findNextPowerOf(p->sizeX(), 2);
464 if ( p->sizeY() > 1 ) newsize[1] = findNextPowerOf(p->sizeY(), 2);
465 if ( p->sizeZ() > 1 ) newsize[2] = findNextPowerOf(p->sizeZ(), 2);
466
467 if ( newsize[0] == p->sizeX() && newsize[1] == p->sizeY() && newsize[2] == p->sizeZ() )
468 return 0;
469
470 // cout << "resizing" << endl;
471
472 Vector3<int> translate(newsize[0]/2 - p->sizeX()/2, newsize[1]/2 - p->sizeY()/2, newsize[2]/2 - p->sizeZ()/2);
473
474 p->resize(newsize, translate, fill_type, fill);
475
476 return 0;
477 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bparticle::id
int id
Definition: mg_processing.h:92
copy_I_part_rad
Bimage * copy_I_part_rad(Bimage *p)
Definition: sin_gal.cpp:163
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bimage.h
Header file for image class.
Euler
Definition: Euler.h:28
rotZ_cart
int rotZ_cart(double x, double y, double z, double ang, double *xx, double *yy, double *zz)
Rotates a vector of cartesian coordinates along the Z axis.
Definition: radon_util.cpp:29
radon_util.h
Bparticle::view
View view
Definition: mg_processing.h:101
View::next
View * next
Definition: View.h:28
Bsymmetry::n
int n
Definition: symmetry.h:42
img_radon_pocs_filter
int img_radon_pocs_filter(Bimage *p, int n_cyc_out, int n_cyc_in, double rad_3D, double rad_plane, int support, Bimage *pmask)
Filters a radon transform using the POCS method.
Definition: img_radon.cpp:221
symmetry.h
Header file for general symmetry functions.
utilities.h
Header file for general utilities.
mean_to_0
int mean_to_0(Bimage *p)
Set each value equal to old_value-(first image pixel)
Definition: radon_util.cpp:193
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:102
img_radon_transform
Bimage * img_radon_transform(Bimage *p, int type, int nkernel, int kernel_power, int padd, int ntheta)
Calculates the Radon transform of an image.
Definition: img_radon.cpp:54
write_line
int write_line(Bimage *p, Bimage *prec, int angolo, float csi, float eta, Bimage *pmask, LUTable *lut, float *priga)
Definition: proj_tool.cpp:177
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
Matrix.h
Generalized matrix class.
img_radon.h
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
sphere
int sphere(Bimage *p)
Definition: radon_util.cpp:114
copy_II_part_rad
int copy_II_part_rad(Bimage *p)
Definition: sin_gal.cpp:195
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
Bfield::next
Bfield * next
Definition: mg_processing.h:258
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:239
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:207
Bparticle::sel
long sel
Definition: mg_processing.h:103
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.
Euler::psi
double psi()
Definition: Euler.h:56
create_table
LUTable * create_table(int ncol, int sizeT)
Definition: proj_tool.cpp:25
Euler::phi
double phi()
Definition: Euler.h:58
pocs_tool.h
Bproject::field
Bfield * field
Definition: mg_processing.h:343
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
gal_ricoI.h
proj_tool.h
Bmicrograph
Definition: mg_processing.h:200
Bsymop::order
int order
Definition: symmetry.h:26
verbose
int verbose
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
img_resize_to_next_power2
int img_resize_to_next_power2(Bimage *p, int fill_type, double fill)
Resizes without interpolation or rescaling to the next power of 2.
Definition: img_radon.cpp:459
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:709
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bsymmetry
Definition: symmetry.h:38
weigh_radon_transf
int weigh_radon_transf(Bimage *p, Bimage *pmask)
Weighs a radon transform with a mask.
Definition: proj_tool.cpp:289
Euler::theta
double theta()
Definition: Euler.h:57
Bparticle
Definition: mg_processing.h:89
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
kill_table
int kill_table(LUTable *lut)
Definition: proj_tool.cpp:139
gal_ricoI
Bimage * gal_ricoI(Bimage *p, int tipo, kernel *ker, int padd)
Reconstruct an image from a sinogram.
Definition: gal_ricoI.cpp:36
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
View
Definition: View.h:26
sin_gal
Bimage * sin_gal(Bimage *p, int tipo, Vector3< float > shift, kernel *ker, int padd, int n_theta)
Definition: sin_gal.cpp:35
mg_processing.h
Header file for micrograph processing.
img_radon_inverse_transform
Bimage * img_radon_inverse_transform(Bimage *p, int type, int nkernel, int kernel_power, int padd)
Calculates a 3D image from a Radon transform.
Definition: img_radon.cpp:141
kernel
Definition: kernlut.h:13
filter_X_mask
int filter_X_mask(Bimage *p, Bimage *pmask, int n_cicli, float r_maxx, int passata)
Definition: pocs_tool.cpp:67
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
findNextPowerOf
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
Definition: utilities.cpp:433
rotY_cart
int rotY_cart(double x, double y, double z, double ang, double *xx, double *yy, double *zz)
Rotates a vector of cartesian coordinates along the Y axis.
Definition: radon_util.cpp:50
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
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
img_radon_reconstruction
Bimage * img_radon_reconstruction(Bproject *project, Bsymmetry *sym, Bstring &file_mask, int rec_size, int ntheta, int table_size, double threshold, Vector3< double > origin, int nkernel, int kernel_power)
Reconstructs a Radon transform from a set of projections.
Definition: img_radon.cpp:292
kernel_prep
kernel * kernel_prep(int nk, int power)
Definition: kernlut.cpp:66
kernlut.h
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:448
Bproject
Definition: mg_processing.h:333
kernel::kern
float * kern
Definition: kernlut.h:20
sin_gal.h
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
LUTable
Definition: proj_tool.h:14
support_f
int support_f(Bimage *p, float rmax)
Definition: pocs_tool.cpp:290
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201