32 int i, j, k, n2 = n/2;
33 double sx(0), sx2(0), sy(0), sy2(0), sxy, d;
35 double* cc =
new double[n];
36 for ( i=0; i<n; i++ ) cc[i] = 0;
38 for ( i=0; i<n2; i++ ) {
45 d = (n2*sx2 - sx*sx)*(n2*sy2 - sy*sy);
46 if ( d > 0 ) d = sqrt(d);
48 cerr <<
"Error in cross_correlate_two_way: denominator is negative!" << endl;
52 for ( i=0; i<n2; i++ ) {
54 for ( j=0, k=i; j<n2; j++, k++ ) {
58 cc[i] = (n2*sxy - sx*sy)/d;
61 for ( i=n2; i<n; i++ ) {
63 for ( j=n2, k=i; j<n; j++, k-- ) {
64 if ( k < 0 ) k = n - 1;
67 cc[i] = (n2*sxy - sx*sy)/d;
87 double res_hi,
double res_lo,
fft_plan planf)
90 if ( !pc )
return NULL;
108 if ( res_lo ) min_rad = (size_t) (p->
real_size().
x()/res_lo);
109 if ( min_rad < 1 ) min_rad = 1;
111 size_t i, j, na, r, m, offset;
114 double* ccs =
new double[pps->
sizeX()];
115 for ( i=0; i<pps->
sizeX(); i++ ) ccs[i] = 0;
117 for ( r=min_rad, m=0, offset=r*pps->
sizeX(); r<pps->
sizeY(); r++, m++, offset+=pps->
sizeX() ) {
120 f = na*1.0L/pps->
sizeX();
121 for ( i=0; i<pps->
sizeX(); i++ ) {
122 j = (size_t) (i*f + 0.5);
123 if ( j >= na ) j -= na;
129 for ( i=0; i<pps->
sizeX(); i++ ) ccs[i] /= m;
153 double res_lo,
double res_hi,
double max_shift,
int filter_flag,
int refine_flag)
156 int iter, maxiter(5);
164 common_size = p1->
size().min(p2->
size());
165 common_size = common_size.
max(1);
168 tile_size = tile_size.
max(1);
169 tile_size = tile_size.
min(common_size);
170 if ( tile_size.
x() >= common_size.
x() && common_size.
x() > 1 ) {
171 tile_size[0] = (int) (0.4*common_size.
x());
174 if ( tile_size.
y() >= common_size.
y() && common_size.
y() > 1 ) {
175 tile_size[1] = (int) (0.4*common_size.
y());
178 if ( tile_size.
z() >= common_size.
z() && common_size.
z() > 1 ) {
179 tile_size[2] = (int) (0.4*common_size.
z());
183 if ( max_shift < 1 ) max_shift = tile_size.
x()/4;
186 half_tile[0] = (tile_size.
x() - 1)/2;
187 half_tile[1] = (tile_size.
y() - 1)/2;
188 half_tile[2] = (tile_size.
z() - 1)/2;
199 cout <<
"Aligning images ";
200 if ( refine_flag ) cout <<
"with ";
201 else cout <<
"without ";
202 cout <<
"shift refinement." << endl << endl;
205 if ( common_size.
x() > 2*tile_size.
x() )
206 start1[0] = (common_size.
x()
207 - (int) (tile_size.
x()*(common_size.
x()/tile_size.
x())))/2;
208 if ( common_size.
y() > 2*tile_size.
y() )
209 start1[1] = (common_size.
y()
210 - (int) (tile_size.
y()*(common_size.
y()/tile_size.
y())))/2;
211 if ( common_size.
z() > 2*tile_size.
z() )
212 start1[2] = (common_size.
z()
213 - (int) (tile_size.
z()*(common_size.
z()/tile_size.
z())))/2;
216 cout <<
"Common size: " << common_size << endl;
217 cout <<
"Tile size: " << tile_size << endl;
218 cout <<
"Maximum shift: " << max_shift << endl;
219 cout <<
"Reference start: " << start1 << endl << endl;
222 if ( res_hi < 0.1 || res_hi > tile_size.
x()*p1->
sampling().x() ) res_hi = 4.0*p1->
sampling().x();
229 pex1 = p1->
extract_tiles(0, start1, common_size, tile_size, 0);
233 for ( i=0; i<pex1->
images(); i++ ) {
236 if ( !set1 ) set1 = m1;
237 if ( !set2 ) set2 = m2;
244 for ( iter=0; iter<maxiter && best_t.
fom>0.1; iter++ ) {
247 pex1->
find_shift(pex2, NULL, res_hi, res_lo, max_shift, 0, refine_flag);
248 for ( i=0, m2=set2; i<pex1->
images(); i++, m2=m2->
next ) {
250 if ( shift.
x() > half_tile.
x() ) shift[0] -= tile_size.
x();
251 if ( shift.
y() > half_tile.
y() ) shift[1] -= tile_size.
y();
252 if ( shift.
z() > half_tile.
z() ) shift[2] -= tile_size.
z();
254 m2->loc = tile_start[i] + half_tile - shift;
262 if ( best_t.
fom > t.
fom ) best_t = t;
264 for ( i=0, m1=set1, m2=set2; i<pex1->
images(); i++, m1=m1->
next, m2=m2->
next ) {
265 m2->loc[0] = best_t.
scale.
x()*(m1->loc.x()*cos(best_t.
angle) -
266 m1->loc.y()*sin(best_t.
angle)) + t.
trans.
x() + 0.5;
267 m2->loc[1] = best_t.
scale.
y()*(m1->loc.x()*sin(best_t.
angle) +
268 m1->loc.y()*cos(best_t.
angle)) + t.
trans.
y() + 0.5;
269 m2->loc[2] = best_t.
scale.
z()*m1->loc.z() + t.
trans.
z();
270 tile_start[i] = m2->loc - half_tile;
276 cout <<
"Best fit:\t" <<
320 double res_hi,
double res_lo,
double shift_limit,
fft_plan planf,
fft_plan planb)
323 cout <<
"DEBUG img_align2D_pps: res_hi=" << res_hi <<
" res_lo=" << res_lo << endl;
325 unsigned long max_rad = (
unsigned long) (p->
real_size().
x()/res_hi);
326 if ( max_rad > p->
sizeX()/2 ) max_rad = p->
sizeX()/2;
327 unsigned long max_ang =
NANG*max_rad;
328 unsigned long hang = max_ang/2;
330 int select_on_pps_cc(1);
332 double cc1, cc2, cc3, cc, ccp, f, ang1, ang2;
338 for ( i=0, cc2 = 0; i<hang; i++ )
if ( cc2 < ccs[i] ) {
343 if ( j < 0 ) j += hang;
346 if ( j >= hang ) j -= hang;
348 f = (cc1 - cc3)/(2*(cc1 - 2*cc2 + cc3));
349 ang1 = (shift + f)*
M_PI*2.0L/max_ang;
354 for ( i=hang, cc2 = 0; i<max_ang; i++ )
if ( cc2 < ccs[i] ) {
359 if ( j < hang ) j += hang;
362 if ( j >= max_ang ) j -= hang;
364 f = (cc1 - cc3)/(2*(cc1 - 2*cc2 + cc3));
365 ang2 = -(shift + f)*
M_PI*2.0L/max_ang +
M_PI;
366 if ( select_on_pps_cc )
if ( ccp < cc2 ) ang1 = ang2;
367 if ( ccp < cc2 ) ccp = cc2;
377 if ( !fullasu && !select_on_pps_cc ) {
418 int ann_min,
int ann_max,
Bimage* prs_mask,
double shift_limit,
422 cout <<
"DEBUG img_align2D: ann_min=" << ann_min <<
" ann_max=" << ann_max << endl;
424 int nannuli(pref->
sizeX()/2);
443 double angle, best_angle(0);
444 double da, dx, dy, ap(1e37);
445 double cc, best_cc(-1e37);
449 for ( i=0; i<5 && !done; i++ ) {
451 angle =
img_correlate_annuli(pol, polref, fullasu, ann_min, ann_max, planf_1D, planb_1D, pimgcc);
453 prot = pref->
rotate(pref->
size(), fabs(angle));
454 translate = p->
find_shift(prot, prs_mask, 0, 0, shift_limit, 0, 1, planf_2D, planb_2D, cc);
456 if ( best_cc < cc ) {
458 best_shift = translate;
461 da = fabs(angle - ap);
462 dx = fabs(translate.
x() - prev_translate.x());
463 dy = fabs(translate.
y() - prev_translate.y());
464 prev_translate = translate;
466 if ( i > 0 && da < 0.005 && dx < 0.1 && dy < 0.1 ) done = 1;
495 double res_lo,
double res_hi,
double shift_limit)
517 double cc =
img_align2D(p, pref, 1, 0, ann_min, ann_max, prs_mask, shift_limit,
518 planf_1D, planb_1D, planf_2D, planb_2D);
521 cout <<
"Alignment correlation: " << cc << endl << endl;
561 if ( ann_min > ann_max ) swap(ann_min, ann_max);
562 if ( ann_min < 0 ) ann_min = 0;
563 if ( ann_max >= pol->
sizeY() ) ann_max = pol->
sizeY() - 1;
568 cout <<
"Doing row cross-correlations from annulus " << ann_min <<
" to annulus " << ann_max << endl;
571 long i, j, ij, icc, n;
574 double scale, sum1, sum2;
577 float* cc =
new float[2*pcc->
sizeX()];
578 for ( i=0; i<2*pcc->
sizeX(); i++ ) cc[i] = 0;
586 for ( i=ann_min, offset=i*linesize; i<=ann_max; i++, offset+=linesize ) {
590 for ( j=0; j<pcc->
sizeX(); j++ ) temp1[j] = 0;
591 if ( !fullasu )
for ( j=0; j<pcc->
sizeX(); j++ ) temp2[j] = 0;
592 for ( j=0; j<pcc->
sizeX()/2; j++ ) {
594 if ( ij < 0 ) ij += pcc->
sizeX();
597 temp1[ij] = d1 * d2.
conj();
598 temp1[i] = temp1[ij].
conj();
601 temp2[i] = temp2[ij].
conj();
606 if ( sum1 && sum2 ) {
607 scale = 0.5/sqrt(sum1*sum2);
608 for ( j=0; j<pcc->
sizeX(); j++ ) temp1[j] *= scale;
609 if ( !fullasu )
for ( j=0; j<pcc->
sizeX(); j++ ) temp2[j] *= scale;
612 for ( j=0; j<pcc->
sizeX(); j++ ) cc[j] += temp1[j].real();
615 for ( j=0, ij=pcc->
sizeX(); j<pcc->
sizeX(); j++, ij++ ) cc[ij] += temp2[j].real();
622 n = ( fullasu )? pcc->
sizeX(): 2*pcc->
sizeX();
625 for ( j=0; j<n; j++ ) {
626 if ( cc_max < cc[j] ) {
631 double angle = icc*
M_PI*(2.0/pcc->
sizeX());
635 if ( fabs(angle) < 1e-10 ) angle = 0;
638 cout <<
"DEBUG img_correlate_annuli: " << icc <<
tab << angle <<
tab << angle*180.0/
M_PI <<
tab << cc[icc] << endl;
645 cout <<
"Best in-plane rotation angle: " << angle*180.0/
M_PI << endl;
fft_plan fft_setup_plan(size_t x, size_t y, size_t z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
int mask_fspace_banded(float *band)
Generate reciprocal space mask based on a specification of bands.
Complex unpack_second(Complex c)
Bimage * unpack_combined_transform()
Unpacks a complex transform obtained from two real images.
Header file for functions to align images.
Header file for image class.
Transform img_align(Bimage *p1, Bimage *p2, Vector3< int > tile_size, double res_lo, double res_hi, double max_shift, int filter_flag, int refine_flag)
Aligns the second image to the first.
void fftw(fftw_plan plan, int howmany, fftw_complex *in, int istride, int idist, fftw_complex *out, int ostride, int odist)
Bimage * extract_tiles(size_t nn, size_t ntiles, Vector3< size_t > *coords, Vector3< size_t > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Header file for general utilities.
double * cross_correlate_two_way(int n, double *x, double *y)
double * img_pps_angular_correlation(Bimage *p, Bimage *pref, double res_hi, double res_lo, fft_plan planf)
Bimage * polar_power_spectrum(double resolution, size_t num_angle)
Calculates the polar power spectrum of a 2D transform amplitude or intensity image.
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
double img_align2D_pps(Bimage *p, Bimage *pref, int fullasu, double res_hi, double res_lo, double shift_limit, fft_plan planf, fft_plan planb)
Finds the best in-plane alignment for two 2D images using polar power spectra.
Vector3< size_t > size() const
void complex_to_amplitudes()
The amplitudes from a complex image is written to a simple image.
General image parameter class.
Vector3< double > real_size()
Header file for generalized linked list functions.
int fspace_bandpass(double res_hi, double res_lo, double width)
Applies a bandpass filter to an image.
int ps_marker_errors(Bstring &filename, Bmarker *set1, Bmarker *set2, Transform t, Vector3< int > size, float err_scale)
Output a postscript file that graphs the deviations of points in a Bmarker from their predicted locat...
Header file for postscript tools dealing with Bmarkers.
double img_correlate_annuli(Bimage *pol, Bimage *polref, int fullasu, int ann_min, int ann_max, fft_plan planf, fft_plan planb, double &cc_max)
Correlate annuli of a polar image.
void file_name(Bstring s)
double rotate_cross_correlate_two_way(Bimage *pref, double angle, double res_hi, double res_lo, double shift_limit, fft_plan planf, fft_plan planb)
void origin(double x, double y, double z)
int filter_extremes()
Filters the extremes out of a micrograph image.
unsigned char * data_pointer()
Bimage * copy()
Copies the header information and data of an image into a new image structure.
double img_align2D(Bimage *p, Bimage *pref, int fullasu, double res_polar, int ann_min, int ann_max, Bimage *prs_mask, double shift_limit, fft_plan planf_1D, fft_plan planb_1D, fft_plan planf_2D, fft_plan planb_2D)
Finds the best in-plane alignment for two 2D images using polar power spectra.
Transform markers_find_rottrans(Bmarker *set1, Bmarker *set2, float tolerance)
Finds the rotation and shift between two sets of vectors.
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
int rotate()
Rotates an image using parameters defined in the image in place.
int find_shift(Bimage *pref, Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Calculates a cross-correlation map to find the shift for the pair of images.
Bimage * cartesian_to_cylindrical(size_t nannuli, size_t nphi, int flag=0)
Calculates an image with cylindrical coordinates.
Complex unpack_first(Complex c)
Bimage * pack_two_in_complex(Bimage *p)
Packs two real images into one complex image.
void view_angle(double d)
Header file for image parameter functions.
ostream & tab(ostream &out)
void origin(double ox, double oy, double oz)