46 int wrap,
double ellipticity,
double angle,
double threshold,
Bimage* pmask)
48 unsigned long i, j, k, n, x, y, z, iradius, imin, imax, imax1, nrad(0);
49 double a, xx, yy, zz, radius, f, f1;
52 if ( maxrad < minrad ) swap(minrad, maxrad);
53 if ( minrad < 0 ) minrad = 0;
55 if ( rad_step < 0.1 ) {
63 imin = (int) (minrad/rad_step);
64 imax = (int) (maxrad/rad_step + 1);
66 if ( maxrad <= 0 || imax > radius ) imax = (
unsigned long) radius;
68 maxrad = imax1*rad_step;
71 cout <<
"DEBUG img_radial: radius=" << radius <<
" maxrad=" << maxrad << endl;
78 float* num =
new float[imax];
81 cout <<
"Calculating radial profile:" << endl;
82 cout <<
"Radial step size: " << rad_step <<
" A" << endl;
83 cout <<
"Minimum and maximum radius: " << minrad <<
" " << maxrad <<
" A" << endl;
84 cout <<
"Minimum and maximum pixels: " << imin <<
" " << imax1 << endl;
85 cout <<
"Ellipticity and angle: " << ellipticity <<
" " << angle << endl;
87 cout <<
"Threshold: " << threshold << endl;
89 cout <<
"Mask: " << pmask->
file_name() << endl;
92 cout <<
"Calculating a radial profile" << endl << endl;
94 for ( i=n=0; n<p->
images(); n++ ) {
95 for ( j=0; j<imax; j++ ) num[j] = 0;
99 for ( z=0; z<p->
sizeZ(); z++ ) {
101 if ( wrap && 2*fabs(zz) >= p->
sizeZ() ) zz += (zz < 0)? p->
sizeZ(): -p->
sizeZ();
103 for ( y=0; y<p->
sizeY(); y++ ) {
105 if ( wrap && 2*fabs(yy) >= p->
sizeY() ) yy += (yy < 0)? p->
sizeY(): -p->
sizeY();
107 for ( x=0; x<p->
sizeX(); x++, i++ )
if ( ( !pmask ) || ( pmask && (*pmask)[i] ) ) {
109 if ( wrap && 2*fabs(xx) >= p->
sizeX() ) xx += (xx < 0)? p->
sizeX(): -p->
sizeX();
111 radius = sqrt(xx*xx + yy*yy + zz*zz)/rad_step;
112 if ( ellipticity != 1 ) {
113 a = atan2(yy, xx) - angle;
114 radius *= sqrt((ellipticity*ellipticity+1)/
115 (2*(ellipticity*ellipticity*
bcos(a)*
bcos(a)+
118 iradius = (
unsigned long) radius;
119 if ( iradius >= imin && iradius < imax1 ) {
120 f = radius - iradius;
125 if ( threshold < p->minimum() ) {
126 prad->
add(iradius, f1*(*p)[i]);
127 prad->
add(iradius+1, f*(*p)[i]);
128 }
else if ( (*p)[i] > threshold ) {
129 prad->
add(iradius, f1);
130 prad->
add(iradius+1, f);
137 if ( threshold < p->minimum() )
for ( j=0, k=imax*n; j<imax; j++, k++ )
138 if ( num[j] ) prad->
set(k, (*prad)[k]/num[j]);
144 cout <<
"Pixels in profile: " << nrad << endl;
147 cout <<
"DEBUG img_radial: Calculating statistics" << endl;
178 double rad_step,
int wrap)
184 double rad_step,
int wrap,
Bimage* pmask)
186 unsigned long i, j, n, x, y, z, iradius, imin, imax, imax1, nrad=0;
187 double xx, yy, zz, radius, f, f1;
189 if ( maxrad < minrad ) swap(minrad, maxrad);
190 if ( minrad < 0 ) minrad = 0;
192 if ( rad_step < 0.1 ) {
200 if ( p->
sizeZ() < 2 ) scale[2] = 1;
202 imin = (
unsigned long) (minrad/rad_step);
203 imax = (
unsigned long) (maxrad/rad_step + 1);
204 radius = sqrt((p->
real_size()).length())/(2*rad_step);
205 if ( maxrad <= 0 ) imax = (
unsigned long) radius;
207 maxrad = imax1*rad_step;
212 float* fom = p->
FOM();
213 float* radfom = NULL;
216 float* num =
new float[imax];
223 cout <<
"Calculating a radial profile:" << endl;
224 cout <<
"Radial step size: " << rad_step <<
" A" << endl;
225 cout <<
"Minimum and maximum radius: " << minrad <<
" " << maxrad <<
" A" << endl;
226 cout <<
"Minimum and maximum pixels: " << imin <<
" " << imax1 << endl;
227 if ( wrap ) cout <<
"With wrapping" << endl;
229 cout <<
"Mask file: " << pmask->
file_name() << endl;
231 cout <<
"Calculating a radial profile" << endl << endl;
234 cout <<
"Image\toriX\toriY\toriZ" << endl;
235 for ( i=n=0; n<p->
images(); n++ ) {
236 for ( j=0; j<imax; j++ ) num[j] = 0;
241 cout << n+1 <<
tab << origin.
x() <<
tab << origin.
y() <<
tab << origin.
z() << endl;
242 for ( z=0; z<p->
sizeZ(); z++ ) {
243 zz = fabs(z - origin.
z());
244 if ( wrap && 2*zz >= p->
sizeZ() ) zz = p->
sizeZ() - zz;
247 for ( y=0; y<p->
sizeY(); y++ ) {
248 yy = fabs(y - origin.
y());
249 if ( wrap && 2*yy >= p->
sizeY() ) yy = p->
sizeY() - yy;
252 for ( x=0; x<p->
sizeX(); x++, i++ )
if ( ( !pmask ) || ( pmask && (*pmask)[i] ) ) {
253 xx = fabs(x - origin.
x());
254 if ( wrap && 2*xx >= p->
sizeX() ) xx = p->
sizeX() - xx;
257 radius = sqrt(xx + yy + zz);
258 iradius = (
unsigned long) radius;
259 if ( ( iradius >= imin ) && ( iradius < imax1 ) ) {
260 f = radius - iradius;
265 prad->
add(iradius, f1*(*p)[i]);
266 prad->
add(iradius+1, f*(*p)[i]);
269 radfom[iradius] += f1*fom[i];
270 radfom[iradius+1] += f*fom[i];
276 for ( j=0, iradius = n*imax; j<imax; j++, iradius++ ) {
278 prad->
set(iradius, (*prad)[iradius]/num[j]);
279 if ( fom ) radfom[iradius] /= num[j];
288 cout <<
"Pixels in radial averages: " << nrad << endl;
291 cout <<
"DEBUG img_radial_average: Calculating statistics" << endl;
322 unsigned long nrad = p->
sizeX()/2;
326 unsigned long i, x, y, n;
328 double dx, dy, r, angle, f, f1, cosang, sinang;
329 double ellmin = 1-ellipticity;
330 double ellmax = 1+ellipticity;
331 double* num =
new double[nrad];
334 cout <<
"Elliptical average:" << endl;
335 cout <<
"Minimum and maximum radii: " << rmin <<
" " << rmax << endl;
337 cout <<
"Ellipticity and angle: " <<
338 ellipticity <<
" " << ellipse_angle*180.0/
M_PI << endl;
341 for ( n=0; n<p->
images(); n++ ) {
343 cout <<
"DEBUG img_elliptical_average: doing image " << n+1 << endl;
344 for ( i=0; i<nrad; i++ ) num[i] = 0;
345 for ( y=0; y<p->
sizeY(); y++ ) {
347 for ( x=0; x<p->
sizeX(); x++ ) {
349 r = sqrt(dx*dx + dy*dy);
352 angle = atan2(dy, dx) - ellipse_angle;
353 cosang =
bcos(angle);
354 sinang =
bsin(angle);
355 r *= sqrt(ellmax*cosang*cosang + ellmin*sinang*sinang);
357 cerr <<
"Warning: r for " << ir <<
" not finite!" << endl;
359 if ( r >= rmin && r <= rmax ) {
366 prad->
add(n*nrad+ir, f1*(*p)[i]);
370 prad->
add(n*nrad+ir, f*(*p)[i]);
377 for ( ir=0, i=n*nrad; ir<nrad; ir++, i++ ) {
378 if ( num[ir] ) prad->
set(i, (*prad)[i]/num[ir]);
379 else prad->
set(i, 0);
386 cout <<
"Pixels in elliptical averages: " << npix << endl << endl;
418 double rad_step,
Bimage* pmask)
423 unsigned long i, j, k, n, x, y, z, iradius, imin, imax;
424 double dx, dy, dz, dx2, dy2, dz2, da, radius, f, f1;
426 if ( maxrad < minrad ) swap(minrad, maxrad);
427 if ( minrad < 0 ) minrad = 0;
429 if ( rad_step < 0.1 ) {
436 if ( p->
sizeZ() < 2 ) scale[2] = 1;
438 imin = (
unsigned long) (minrad/rad_step);
439 imax = (
unsigned long) (maxrad/rad_step + 1);
440 radius = sqrt((p->
real_size()).length2())/(2*rad_step);
441 if ( maxrad <= 0 || imax > radius ) imax = (
unsigned long) radius;
442 maxrad = (imax - 1)*rad_step;
447 float* num =
new float[2*imax];
454 cout <<
"Calculating a directional radial profile:" << endl;
455 cout <<
"Angle: " << angle*180.0/
M_PI << endl;
456 cout <<
"Radial step size: " << rad_step <<
" A" << endl;
457 cout <<
"Minimum and maximum radius: " << minrad <<
" " << maxrad <<
" A" << endl;
458 cout <<
"Minimum and maximum pixels: " << imin <<
" " << imax - 1 << endl;
460 cout <<
"Mask file: " << pmask->
file_name() << endl;
463 cout <<
"Calculating a radial profile" << endl << endl;
465 for ( i=n=0; n<p->
images(); n++ ) {
466 for ( j=0; j<2*imax; j++ ) num[j] = 0;
467 for ( z=0; z<p->
sizeZ(); z++ ) {
470 for ( y=0; y<p->
sizeY(); y++ ) {
473 for ( x=0; x<p->
sizeX(); x++, i++ )
if ( !pmask || ( pmask && (*pmask)[i] ) ) {
477 da = atan2(dy, dx) - angle;
481 if ( da <= 0.1 ) aflag = 0;
482 else if ( da >=
M_PI_2 - 0.1 ) aflag = 1;
484 radius = sqrt(dx2 + dy2 + dz2);
485 iradius = (
unsigned long) radius;
486 if ( iradius >= imin && iradius < imax - 1 ) {
487 f = radius - iradius;
489 if ( aflag == 1 ) iradius += imax;
493 prad->
add(iradius, f1*(*p)[i]);
494 prad->
add(iradius+1, f*(*p)[i]);
500 for ( j=0, k=2*imax*n; j<2*imax; j++, k++ )
501 if ( num[j] ) prad->
set(k, (*prad)[k]/num[j]);
508 cout <<
"DEBUG img_directional_average: Calculating statistics" << endl;
527 if ( size.
volume() < 1 )
return NULL;
532 if ( p->
sizeX() < 2 ) origin[0] = 0;
533 if ( p->
sizeY() < 2 ) origin[1] = 0;
534 if ( p->
sizeZ() < 2 ) origin[2] = 0;
537 unsigned long i, n, x, y, z, ir, nexp(0);
542 cout <<
"Calculating a full image from a radial profile:" << endl;
543 cout <<
"Size: " << p->
size() << endl;
544 cout <<
"Origin: " << origin << endl;
547 for ( n=i=0; n<p->
images(); n++ ) {
548 for ( z=0; z<p->
sizeZ(); z++ ) {
549 d[2] = z - origin.
z();
550 for ( y=0; y<p->
sizeY(); y++ ) {
551 d[1] = y - origin.
y();
552 for ( x=0; x<p->
sizeX(); x++, i++ ) {
553 d[0] = x - origin.
x();
555 ir = (
unsigned long) r;
557 if ( ir < prad->sizeX() ) {
562 if ( ir < prad->sizeX() ) v += f*(*prad)[ir];
563 else v += f*prad->image[n].background();
576 cout <<
"Pixels generated: " << nexp << endl << endl;
600 int i, npoint = p->
sizeX();
605 for ( i=0; i<pref->
sizeX(); i++ ) simp->
x[i] = (*pref)[i];
606 for ( i=0; i<p->
sizeX(); i++ ) simp->
fx[i] = (*p)[i];
632 cout <<
"Radial fit initial parameters:" << endl;
633 cout <<
"Dimension scale: " << simp->
param[0] << endl;
634 cout <<
"Amplitude scale and shift: " << simp->
param[1] <<
" " << simp->
param[2] << endl;
641 double fraction, xdat, f;
643 if (
verbose & VERB_PROCESS ) {
644 cout <<
"Radial fit final parameters:" << endl;
645 cout <<
"Dimension scale: " << simp->
param[0] << endl;
646 cout <<
"Relative dimension scale: " << simp->
param[0]*pref->
sampling().x()/p->
sampling().x() << endl;
647 cout <<
"Amplitude scale and shift: " << simp->
param[1] <<
" " << simp->
param[2] << endl;
648 cout <<
"R: " << R << endl << endl;
649 cout <<
"Radial averages:" << endl;
652 cout <<
"Radial fit final parameters" << endl << endl;
654 if (
verbose & VERB_PROCESS ) {
655 for ( i=0; i<pref->
sizeX(); i++ ) {
656 xdat = i/simp->
param[0];
658 if ( idat >= 0 && idat < p->sizeX() - 1 ) {
659 fraction = xdat - idat;
660 f = simp->
param[1]*((1.0 - fraction)*(*p)[idat]
661 + fraction*(*p)[idat+1]) + simp->
param[2];
665 cout << pref->
sampling().x()*i <<
" " << f <<
" " << (*pref)[i] << endl;
669 float* result =
new float[simp->
nparam];
670 for ( i=0; i<simp->
nparam; i++ ) result[i] = simp->
param[i];
697 float* yref = simp->
x;
700 double fraction, xref, df, R(0), sum(0), weight;
702 for ( i=0; i<simp->
npoint; i++ ) {
703 xref = simp->
param[0]*i;
705 if ( iref >= 0 && iref < simp->npoint - 1 ) {
706 fraction = xref - iref;
707 df = (1.0 - fraction)*yref[iref] + fraction*yref[iref + 1]
717 if ( sum && n > 0.2*simp->
npoint ) R = sqrt(R/sum);
721 cout <<
"DEBUG img_radial_R: " << simp->
param[0] <<
" " <<
722 simp->
param[1] <<
" " << simp->
param[2] <<
" R=" << R << endl;
Bsimplex * init_simplex(int nvar, int nparam, int nconstant, int npoint)
Initializes a Bsimplex structure.
double bcos(double x)
Avoid non-zero values for cos(PI/2) and cos(-PI/2).
Header file for general utilities.
Bimage * img_directional_average(Bimage *p, double angle, double minrad, double maxrad, double rad_step)
Calculates the radial average of an image.
Bimage * img_elliptical_average(Bimage *p, int rmin, int rmax, double ellipticity, double ellipse_angle)
Calculates the elliptical average of an image.
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
int kill_simplex(Bsimplex *simp)
Frees all memory associated with a Bsimplex structure.
Generalized matrix class.
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Bimage * img_radial(Bimage *p, double minrad, double maxrad, double rad_step, int wrap, double ellipticity, double angle, double threshold, Bimage *pmask)
Calculates the elliptical radial average or volume of an image.
size_t statistics()
Calculates the statistics for an image.
double bsin(double x)
Avoid non-zero values for sin(PI) and sin(-PI).
General image parameter class.
Vector3< double > real_size()
unsigned char * data_alloc()
float * FOM_initialize()
Allocates and initializes the FOM block.
void file_name(Bstring s)
Header file for trigonometry functions.
void origin(double x, double y, double z)
void add(size_t j, double v)
void set(size_t j, double v)
Sets a single value at the given index.
double standard_deviation()
Nelder and Mead downhill simplex method for generalized parameter fitting.
Library routines for radial transformations and calculations.
Bimage * img_radial_to_full(Bimage *prad, Vector3< int > size, Vector3< float > origin)
Generates a full 2D or 3D image from a radial profile in a 1D image.
double img_radial_R(Bsimplex *simp)
Bimage * img_radial_average(Bimage *p, int minrad, int maxrad, int wrap)
double simplex(Bsimplex *simp, int maxcycles, double tolerance, double(funk)(Bsimplex *))
Estimates parameters using a generalized search algorithm.
float * img_radial_fit(Bimage *p, Bimage *pref)
Fits a radial profile to a reference radial profile.
ostream & tab(ostream &out)
void origin(double ox, double oy, double oz)