30 double hi_res,
double lo_res,
double shift_limit,
33 if ( nref < 0 || nref >= n ) nref = 0;
36 double cc(0), shift_avg(0), cc_avg(0);
42 cout <<
"Progressive alignment:" << endl;
43 cout <<
"Image\tShift\t\t\tCC" << endl;
46 for ( nn=nref+1, i=1; i<n; nn++, i++ ) {
47 if ( nn >= n ) nn = 0;
63 for ( nn=1; nn<n; nn++ ) {
65 shift_avg +=
shift.length();
74 cout <<
"Shift average: " << shift_avg << endl;
75 cout <<
"CC average: " << cc_avg << endl;
104 p1->
fft(FFTW_BACKWARD, 0);
109 cout <<
"origin = " << p1->
image->
origin() << endl;
120 cout <<
"shift = " <<
shift << endl;
121 cout <<
"fom = " << p1->
image->
FOM() << endl;
140 if ( nref < 0 || nref >= n ) nref = 0;
143 double shift_avg(0), cc_avg(0);
149 cout <<
"Progressive alignment:" << endl;
150 cout <<
"Image\tShift\t\t\tCC" << endl;
153 for ( nn=nref+1, i=1; i<n; nn++, i++ ) {
154 if ( nn >= n ) nn = 0;
167 for ( nn=1; nn<n; nn++ ) {
169 shift_avg +=
shift.length();
178 cout <<
"Shift average: " << shift_avg << endl;
179 cout <<
"CC average: " << cc_avg << endl;
204 double hi_res,
double lo_res,
double shift_limit,
207 if ( ref_num < 0 || ref_num >= n ) ref_num = 0;
221 edge_size = edge_size.
max(1);
225 if ( edge_width > 0 )
236 pmask, hi_res, lo_res, shift_limit,
planf,
planb);
237 double cc_avg(0), cc_avg_old(0), shift_avg, shift_avg_old(0);
249 cout <<
"Iterative refinement:" << endl;
250 for ( i=1; i<=10 && fabs(shift_avg - shift_avg_old) > 0.01
251 && cc_avg > cc_avg_old; i++ ) {
255 cout <<
"Iteration " << i << endl;
257 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t nn){
261 #pragma omp parallel for
262 for ( nn=0; nn<n; nn++ ) {
270 shift_avg_old = shift_avg;
273 for ( nn=0; nn<n; nn++ ) sh[nn] = sh[ref_num] - sh[nn];
276 cout <<
"Image\tShift\t\t\tCC" << endl;
277 for ( cc_avg=0, shift_avg=0, nn=0; nn<n; nn++ ) {
284 if ( nn ) shift_avg += sh[nn].
distance(sh[nn-1]);
286 cout << nn+1 <<
tab << sh[nn] <<
tab << pt->
image[nn].
FOM() << endl;
293 cout <<
"Shift average per frame: " << shift_avg << endl;
294 cout <<
"CC average: " << cc_avg << endl;
303 vector<double> vsh(3);
307 for ( nn=0; nn<n; nn++ ) {
308 vsh = {sh[nn][0], sh[nn][1], pt->
image[nn].
FOM()};
313 job[
"shift_average"] = shift_avg;
317 cout <<
"Shifting original frames" << endl << endl;
322 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t nn){
327 #pragma omp parallel for
328 for ( nn=0; nn<n; nn++ ) {
362 double hi_res,
double lo_res,
double shift_limit,
363 double edge_width,
double gauss_width)
365 if ( ref_num < 0 || ref_num >= n ) ref_num = 0;
378 edge_size = edge_size.
max(1);
382 if ( edge_width > 0 )
385 double scale(hi_res/(2*
sampling(0)[0]));
387 nusize = nusize.
max(1);
388 if ( nusize[0]%2 ) nusize[0]++;
389 if ( nusize[1]%2 ) nusize[1]++;
390 if ( z > 1 && nusize[2]%2 ) nusize[2]++;
400 cout <<
"new size = " << nusize << endl;
401 cout <<
"new sampling = " << pt->
sampling(0) << endl;
402 cout <<
"new origin = " << pt->
image->
origin() << endl;
410 double cc_avg(0), cc_avg_old(0), shift_avg, shift_avg_old(0);
422 cout <<
"Iterative refinement:" << endl;
423 for ( i=1; i<=10 && fabs(shift_avg - shift_avg_old) > 0.01
424 && cc_avg > cc_avg_old; i++ ) {
428 cout <<
"Iteration " << i << endl;
430 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t nn){
434 #pragma omp parallel for
435 for ( nn=0; nn<n; nn++ ) {
443 shift_avg_old = shift_avg;
446 for ( nn=0; nn<n; nn++ ) sh[nn] = sh[ref_num] - sh[nn];
449 cout <<
"Image\tShift\t\t\tCC" << endl;
450 for ( cc_avg=0, shift_avg=0, nn=0; nn<n; nn++ ) {
457 if ( nn ) shift_avg += sh[nn].
distance(sh[nn-1]);
459 cout << nn+1 <<
tab << sh[nn] <<
tab << pt->
image[nn].
FOM() << endl;
468 cout <<
"Shift average per frame: " << shift_avg << endl;
469 cout <<
"CC average: " << cc_avg << endl;
475 vector<double> vsh(3);
479 for ( nn=0; nn<n; nn++ ) {
480 vsh = {sh[nn][0], sh[nn][1], pt->
image[nn].
FOM()};
485 job[
"shift_average"] = shift_avg;
489 cout <<
"Shifting original frames" << endl << endl;
494 dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(
size_t nn){
499 #pragma omp parallel for
500 for ( nn=0; nn<n; nn++ ) {
529 long i, j, k, n2(nn/2);
530 double sx(0), sx2(0), sy(0), sy2(0), sxy, d;
532 double* cc =
new double[nn];
533 for ( i=0; i<nn; i++ ) cc[i] = 0;
535 for ( i=0; i<n2; i++ ) {
542 d = (n2*sx2 - sx*sx)*(n2*sy2 - sy*sy);
543 if ( d > 0 ) d = sqrt(d);
545 cerr <<
"Error in cc_two_way: denominator is negative!" << endl;
549 for ( i=0; i<n2; i++ ) {
551 for ( j=0, k=i; j<n2; j++, k++ ) {
552 if ( k >= nn ) k = 0;
555 cc[i] = (n2*sxy - sx*sy)/d;
558 for ( i=n2; i<nn; i++ ) {
560 for ( j=n2, k=i; j<nn; j++, k-- ) {
561 if ( k < 0 ) k = nn - 1;
564 cc[i] = (n2*sxy - sx*sy)/d;
593 int ann_min,
int ann_max,
double ang_min,
double ang_max,
596 if ( ann_min > ann_max ) swap(ann_min, ann_max);
597 if ( ann_min < 0 ) ann_min = 0;
598 if ( ann_max >= y ) ann_max = y - 1;
603 if ( fabs(ang_min - ang_max) < 1e-10 ) {
611 cout <<
"Doing row cross-correlations from annulus " << ann_min <<
" to annulus " << ann_max << endl;
616 double scale, sum1, sum2;
619 float* cc =
new float[2*pcc->
sizeX()];
620 for ( i=0; i<2*pcc->
sizeX(); i++ ) cc[i] = 0;
628 for ( i=ann_min, offset=i*linesize; i<=ann_max; i++, offset+=linesize ) {
632 for ( j=0; j<pcc->
sizeX(); j++ ) temp1[j] = 0;
633 for ( j=0; j<pcc->
sizeX()/2; j++ ) {
635 if ( ij < 0 ) ij += pcc->
sizeX();
638 temp1[ij] = d1 * d2.
conj();
639 temp1[i] = temp1[ij].
conj();
643 if ( sum1 && sum2 ) {
644 scale = 0.5/sqrt(sum1*sum2);
645 for ( j=0; j<pcc->
sizeX(); j++ ) temp1[j] *= scale;
648 for ( j=0; j<pcc->
sizeX(); j++ ) cc[j] += temp1[j].real();
659 if ( imin < 0 && imax < 0 ) {
660 imin += pcc->
sizeX();
661 imax += pcc->
sizeX();
663 if ( imax > pcc->
sizeX() ) imax = pcc->
sizeX();
665 if ( imin < 0 && imax > 0 ) {
666 imin += pcc->
sizeX();
667 for ( j=0; j<imin; j++ ) {
668 if ( cc_max < cc[j] ) {
673 for ( j=imax; j<pcc->
sizeX(); j++ ) {
674 if ( cc_max < cc[j] ) {
680 if ( imin < 0 ) imin = 0;
681 for ( j=imin; j<imax; j++ ) {
682 if ( cc_max < cc[j] ) {
688 double angle = icc*
M_PI*(2.0/pcc->
sizeX());
692 if ( fabs(angle) < 1e-10 ) angle = 0;
695 cout <<
"DEBUG Bimage::correlate_annuli: " << icc <<
tab << angle <<
tab << angle*180.0/
M_PI <<
tab << cc[icc] << endl;
702 cout <<
"Best in-plane rotation angle: " << angle*180.0/
M_PI << endl;
741 if ( res_lo ) min_rad = (long) (
real_size()[0]/res_lo);
742 if ( min_rad < 1 ) min_rad = 1;
744 long i, j, na, r, m, offset;
747 vector<double> ccs(pps->
sizeX(), 0);
749 for ( r=min_rad, m=0, offset=r*pps->
sizeX(); r<pps->
sizeY(); r++, m++, offset+=pps->
sizeX() ) {
752 f = na*1.0L/pps->
sizeX();
753 for ( i=0; i<pps->
sizeX(); i++ ) {
754 j = (long) (i*f + 0.5);
755 if ( j >= na ) j -= na;
761 for ( i=0; i<pps->
sizeX(); i++ ) ccs[i] /= m;
795 double shift_limit,
double angle_limit,
799 cout <<
"DEBUG Bimage::align2D_pps: res_hi=" << res_hi <<
" res_lo=" << res_lo << endl;
802 long max_rad = (long) (
real_size()[0]/res_hi);
803 if ( max_rad > x/2 ) max_rad = x/2;
804 long max_ang(nang*max_rad);
805 long hang(max_ang/2);
809 long imin(0), imax(hang);
811 imin = max_ang*(angle - angle_limit)/
TWOPI;
812 imax = max_ang*(angle + angle_limit)/
TWOPI;
820 double cc1, cc2, cc3, cc, f;
825 for ( i=imin, cc2 = 0; i<imax; i++ )
if ( cc2 < ccs[i] ) {
830 for ( i=0, cc2 = 0; i<imax; i++ )
if ( cc2 < ccs[i] ) {
834 for ( i=imin; i<hang; i++ )
if ( cc2 < ccs[i] ) {
840 if ( j < 0 ) j += hang;
843 if ( j >= hang ) j -= hang;
845 f = (cc1 - cc3)/(2*(cc1 - 2*cc2 + cc3));
882 int ann_min,
int ann_max,
Bimage* prs_mask,
double shift_limit,
double angle_limit,
886 cout <<
"DEBUG Bimage::align2D: ann_min=" << ann_min <<
" ann_max=" << ann_max << endl;
890 double ang_min(0), ang_max(
TWOPI);
892 ang_min = angle - angle_limit;
893 ang_max = angle + angle_limit;
896 long nannuli(pref->
sizeX()/2);
915 double best_angle(0);
916 double da, dx, dy, ap(1e37);
917 double cc(0), best_cc(-1e37);
921 for ( i=0; i<5 && !done; i++ ) {
924 ang_min, ang_max, planf_1D, planb_1D, pimgcc);
926 prot = pref->
rotate(pref->
size(), fabs(angle));
927 translate =
find_shift(prot, prs_mask, 0, 0, shift_limit, 0, 1, planf_2D, planb_2D, cc);
929 if ( best_cc < cc ) {
931 best_shift = translate;
934 da = fabs(angle - ap);
935 dx = fabs(translate[0] - prev_translate[0]);
936 dy = fabs(translate[1] - prev_translate[1]);
937 prev_translate = translate;
939 if ( i > 0 && da < 0.005 && dx < 0.1 && dy < 0.1 ) done = 1;
969 double res_lo,
double res_hi,
double shift_limit,
double angle_limit)
985 double cc =
align2D(pref, 0, ann_min, ann_max, prs_mask, shift_limit,
986 angle_limit, planf_1D, planb_1D, planf_2D, planb_2D);
989 cout <<
"Alignment correlation: " << cc << endl << endl;