47 if ( lores > 0 && hires > lores ) swap(hires, lores);
50 if ( !pc )
return NULL;
55 cout <<
"Cross-correlation:" << endl;
56 if ( lores > 0 || hires > 0 ) {
57 cout <<
"Resolution range: " << hires <<
" - ";
58 if ( lores > 0 ) cout << lores <<
" A" << endl;
59 else cout <<
"inf A" << endl;
61 cout <<
"No resolution limits" << endl;
63 cout <<
"With a mask: " << pmask->
file_name() << endl;
79 for ( n=0; n<pc->
images(); n++ )
88 if ( lores > 0 && hires > lores ) swap(hires, lores);
91 if ( !pc )
return NULL;
96 cout <<
"Cross-correlation:" << endl;
97 if ( lores > 0 || hires > 0 ) {
98 cout <<
"Resolution range: " << hires <<
" - ";
99 if ( lores > 0 ) cout << lores <<
" A" << endl;
100 else cout <<
"inf A" << endl;
102 cout <<
"No resolution limits" << endl;
104 cout <<
"With a mask: " << pmask->
file_name() << endl;
120 for ( n=0; n<pc->
images(); n++ )
155 double lores,
double radius,
double sigma,
int refine_flag)
159 cout <<
"Finding shift by cross-correlation and polynomial fitting" << endl << endl;
161 cout <<
"Finding shift by cross-correlation" << endl << endl;
165 if ( !pc )
return -1;
177 for ( n=0; n<pc->
images(); n++ ) {
188 cout <<
"Image\t ox\t oy\t oz\t CC" << endl;
189 for ( n=0; n<p->
images(); n++ )
202 double lores,
double radius,
double sigma,
int refine_flag,
207 cout <<
"Finding shift by cross-correlation and polynomial fitting" << endl << endl;
209 cout <<
"Finding shift by cross-correlation" << endl << endl;
213 if ( !pc )
return -1;
225 for ( n=0; n<pc->
images(); n++ ) {
227 if ( origin.
x() > pc->
sizeX()/2 ) origin[0] -= pc->
sizeX();
228 if ( origin.
y() > pc->
sizeY()/2 ) origin[1] -= pc->
sizeY();
229 if ( origin.
z() > pc->
sizeZ()/2 ) origin[2] -= pc->
sizeZ();
236 cout <<
"Image\t ox\t oy\t oz\t CC" << endl;
237 for ( n=0; n<p->
images(); n++ )
272 if ( radius < 0 ) radius = 1e30;
273 else if ( radius < 1 ) radius = 0.9;
274 if ( sigma < 1e-30 ) sigma = 2e-30;
277 cout <<
"DEBUG img_find_peak: radius=" << radius << endl;
279 unsigned long i, n, x, y, z;
281 double x2, y2, z2, r;
282 double value, fac =
GOLDEN/sigma;
286 cout <<
"Finding the brightest voxel" << endl;
287 cout <<
"n\tx\ty\tz\tv" << endl;
290 for ( i=n=0; n<p->
images(); n++ ) {
296 for ( z=0; z<p->
sizeZ(); z++ ) {
298 if ( z2 < -zh ) z2 += p->
sizeZ();
299 if ( z2 > zh ) z2 -= p->
sizeZ();
301 for ( y=0; y<p->
sizeY(); y++ ) {
303 if ( y2 < -yh ) y2 += p->
sizeY();
304 if ( y2 > yh ) y2 -= p->
sizeY();
306 for ( x=0; x<p->
sizeX(); x++, i++ ) {
308 if ( x2 < -xh ) x2 += p->
sizeX();
309 if ( x2 > xh ) x2 -= p->
sizeX();
311 r = sqrt(x2 + y2 + z2) - radius;
313 if ( sigma > 1e-30 ) value *= 1/(1 + exp(fac*r));
326 if (
verbose & VERB_FULL ) cout << endl;
347 size_t i, j, n, kernel_size(5), nterm(7);
348 long x, y, z, xlo, xhi, ylo, yhi, zlo, zhi, ix, iy, iz;
353 double*
b =
new double[nterm];
354 double* vec =
new double[nterm];
359 cout <<
"Finding the shift in each image" << endl;
360 cout <<
"n\tx\ty\tz\tv" << endl;
363 for ( n=0; n<pc->
images(); n++ ) {
365 int_shift[0] = (long) shift.
x();
366 int_shift[1] = (long) shift.
y();
367 int_shift[2] = (long) shift.
z();
368 xlo = (long) (shift.
x() - kernel_size);
369 xhi = (long) (shift.
x() + kernel_size);
370 if ( xhi - xlo >= pc->
sizeX() ) {
372 xhi = pc->
sizeX() - 1;
374 ylo = (long) (shift.
y() - kernel_size);
375 yhi = (long) (shift.
y() + kernel_size);
376 if ( yhi - ylo >= pc->
sizeY() ) {
378 yhi = pc->
sizeY() - 1;
380 zlo = (long) (shift.
z() - kernel_size);
381 zhi = (long) (shift.
z() + kernel_size);
382 if ( zhi - zlo >= pc->
sizeZ() ) {
384 zhi = pc->
sizeZ() - 1;
387 cout <<
"DEBUG img_refine_peak: kernel limits = " <<
388 xlo <<
tab << xhi <<
tab << ylo <<
tab << yhi <<
tab << zlo <<
tab << zhi << endl;
389 for ( i=0; i<nterm; i++ ) {
390 for ( j=0; j<nterm; j++ ) a[i][j] = 0;
394 for ( z=zlo; z<=zhi; z++ ) {
396 if ( iz < 0 ) iz += pc->
sizeZ();
397 if ( iz >= (
long)pc->
sizeZ() ) iz -= pc->
sizeZ();
400 for ( y=ylo; y<=yhi; y++ ) {
402 if ( iy < 0 ) iy += pc->
sizeY();
403 if ( iy >= (
long)pc->
sizeY() ) iy -= pc->
sizeY();
406 for ( x=xlo; x<=xhi; x++ ) {
408 if ( ix < 0 ) ix += pc->
sizeX();
409 if ( ix >= (
long)pc->
sizeX() ) ix -= pc->
sizeX();
410 i = pc->
index(0,ix,iy,iz,n);
413 if ( !isfinite((*pc)[i]) ) {
414 error_show(
"Error in img_refine_peak", __FILE__, __LINE__);
415 cerr << ix <<
" " << iy <<
" " << iz <<
" " << (*pc)[i] << endl;
418 for ( j=0; j<nterm; j++ ) b[j] += vec[j]*(*pc)[i];
419 for ( i=0; i<nterm; i++ )
420 for ( j=0; j<=i; j++ ) a[i][j] += vec[i]*vec[j];
424 for ( i=0; i<nterm-1; i++ )
425 for ( j=i+1; j<nterm; j++ ) a[i][j] = a[j][i];
426 for ( i=0; i<nterm; i++ )
427 if ( fabs(a[i][i]) < 1e-37 ) a[i][i] = 1;
429 cout <<
"DEBUG img_refine_peak: Input matrix:" << endl;
432 cout <<
"DEBUG img_refine_peak: Input vector:";
433 for ( i=0; i<nterm; i++ ) cout <<
tab << b[i];
439 cout <<
"DEBUG img_refine_peak: Coefficients: " <<
440 b[0] <<
tab << b[1] <<
tab << b[2] <<
tab << b[3] <<
tab << b[4] <<
tab << b[5] <<
tab << b[6] << endl;
441 if ( b[4] ) shift[0] = -0.5*b[1]/b[4];
442 if ( b[5] ) shift[1] = -0.5*b[2]/b[5];
443 if ( b[6] ) shift[2] = -0.5*b[3]/b[6];
444 if ( fabs(int_shift.
x() - shift.
x()) > 2 ) shift[0] = int_shift.
x();
445 if ( fabs(int_shift.
y() - shift.
y()) > 2 ) shift[1] = int_shift.
y();
446 if ( fabs(int_shift.
z() - shift.
z()) > 2 ) shift[2] = int_shift.
z();
456 cout << n+1 <<
tab << shift.
x() <<
tab << shift.
y() <<
tab << shift.
z() << endl;
458 if (
verbose & VERB_FULL ) cout << endl;
481 if ( resolution_hi <= 0 ) resolution_hi = 0.1;
482 if ( resolution_lo < resolution_hi + 1 ) resolution_lo = resolution_hi + 1;
490 cout <<
"Cosine phase difference in the resolution range " << resolution_hi <<
" - " << resolution_lo << endl;
492 unsigned long i, j, x, y, z, n;
496 double hires2 = 1.0/(resolution_hi*resolution_hi);
497 double lores2 = 1.0/(resolution_lo*resolution_lo);
498 double dy, dz, sx2, sy2, sz2, s2;
502 float* dphi =
new float[datasize];
504 for ( n=0; n<ppd->
images(); n++ ) {
508 cout << n+1 <<
tab << shift.
x() <<
tab << shift.
y() <<
tab << shift.
z() << endl;
509 for ( z=0; z<ppd->
sizeZ(); z++ ) {
511 if ( iz < 0 ) iz += ppd->
sizeZ();
513 if ( z > (ppd->
sizeZ() - 1)/2 ) dz -= ppd->
sizeZ();
516 for ( y=0; y<ppd->
sizeY(); y++ ) {
518 if ( iy < 0 ) iy += ppd->
sizeY();
520 if ( y > (ppd->
sizeY() - 1)/2 ) dy -= ppd->
sizeY();
523 for ( x=0; x<(ppd->
sizeX()+1)/2; x++ ) {
525 if ( ix < 0 ) ix += ppd->
sizeX();
528 s2 = sx2 + sy2 + sz2;
529 if ( s2 >= lores2 && s2 <= hires2 ) {
538 dphi[i] = temp1.phi() - temp2.phi() -
TWOPI*(x*shift.
x() + y*shift.
y() + z*shift.
z());
541 dphi[j] = dphi[i] =
bcos(dphi[i]);
571 double resolution_hi,
double resolution_lo,
int weighting)
577 if ( resolution_hi <= 0 ) resolution_hi = 0.1;
578 if ( resolution_lo < resolution_hi + 1 ) resolution_lo = resolution_hi + 1;
579 if ( weighting < 0 ) weighting = 0;
580 if ( weighting > 3 ) weighting = 3;
582 unsigned long i, j, x, y, z, n;
584 double hires2 = 1.0/(resolution_hi*resolution_hi);
585 double lores2 = 1.0/(resolution_lo*resolution_lo);
586 double dy, dz, sx2, sy2, sz2, szy2, szyx2, dphi, amp;
594 for ( n=0; n<ppd->
images(); n++ ) {
597 for ( z=0; z<ppd->
sizeZ(); z++ ) {
599 if ( iz < 0 ) iz += ppd->
sizeZ();
601 if ( z > (ppd->
sizeZ() - 1)/2 ) dz -= ppd->
sizeZ();
604 if ( sz2 <= hires2 )
for ( y=0; y<ppd->
sizeY(); y++ ) {
606 if ( iy < 0 ) iy += ppd->
sizeY();
608 if ( y > (ppd->
sizeY() - 1)/2 ) dy -= ppd->
sizeY();
612 if ( szy2 <= hires2 )
for ( x=0; x<(ppd->
sizeX()+1)/2; x++ ) {
614 if ( ix < 0 ) ix += ppd->
sizeX();
618 if ( szyx2 <= hires2 && szyx2 >= lores2 ) {
627 dphi = temp1.
phi() - temp2.
phi() -
TWOPI*(x*shift.
x() + y*shift.
y() + z*shift.
z());
628 switch ( weighting ) {
636 amp = temp1.
amp() + temp2.
amp();
644 avg_pd += amp*fabs(dphi);
652 if ( sum_amp ) avg_pd /= sum_amp;
673 for ( i=0; i<datasize; i++ )
674 if ( (*pd)[i] < 0 ) p->
set(i, -p->
complex(i));
void change_type(char letter)
Get the data type indicated by a single letter code.
Header file for image class.
int combined_complex_product()
Calculates the complex conjugate product of a complex image resulting from combining and Fourier tran...
double bcos(double x)
Avoid non-zero values for cos(PI/2) and cos(-PI/2).
Header file for general utilities.
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
CompoundType compound_type()
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Generalized matrix class.
size_t index(size_t nx, size_t ny) const
double img_average_phase_difference(Bimage *p1, Bimage *p2, double resolution_hi, double resolution_lo, int weighting)
Calculates the average of the cosine of the phase difference between two images within given resoluti...
Bimage * img_phase_difference(Bimage *p1, Bimage *p2, double resolution_hi, double resolution_lo)
Calculates the cosine of the phase difference between two images.
Vector3< size_t > size() const
int img_flip_phases(Bimage *p, Bimage *pd)
Flips the phases of an image based on a phase difference map.
Vector3< T1 > vector3_origin_to_shift(Vector3< T1 > origin, Vector3< T2 > size)
int img_refine_peak(Bimage *pc)
Refines the position of a peak to sub-voxel resolution.
Utilities for timing functions.
General image parameter class.
Vector3< double > real_size()
double LU_decomposition()
Matrix inversion by LU decomposition.
Complex< double > complex(size_t j)
Returns a complex value at the given index.
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
General FFT for n-dimensional data.
void file_name(Bstring s)
Header file for trigonometry functions.
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
int img_find_peak(Bimage *p, double radius, double sigma)
Finds the peak in an image to the nearest voxel.
int write_img(const char *filename, Bimage *p)
General driver function to write multiple image formats This is the only image writing function that ...
void complex_to_real()
The real part of a complex image is written to a simple image.
unsigned char * data_pointer()
Header file for matrix functions.
int img_find_shift(Bimage *pref, Bimage *p, 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 * pack_two_in_complex(Bimage *p)
Packs two real images into one complex image.
Bimage * img_cross_correlate(Bimage *p1, Bimage *p2, double hires, double lores, Bimage *pmask)
Calculates a cross-correlation map by Fast Fourier transformation.
FourierType fourier_type()
ostream & tab(ostream &out)
Header file for general utilities.