38 int bin,
double hires,
double lores,
double radius)
40 angle_step = fabs(angle_step);
41 if ( angle_step < 0.0001 ) angle_step = 0.0001;
42 if ( angle_start > angle_end ) swap(angle_start, angle_end);
44 if ( bin < 1 ) bin = 1;
45 if ( bin > 4 ) bin = 4;
47 if ( lores > 0 && hires > lores ) swap(hires, lores);
48 if ( hires < 2*bin*p->sampling().x() ) hires = 2*bin*p->
sampling().x();
49 if ( lores < hires ) lores = p->
size().x()*p->
sampling().x();
51 if ( radius < 1 ) radius = p->
sizeX()/2;
57 size_t i, j, na, max_iter(10);
58 double rise, angle, offset, bestrise(0), bestangle(0);
59 double cc, dd, bestcc(0), mincc(1);
64 cout <<
"Searching for helical parameters by cross-correlation:" << endl;
65 cout <<
"Angular range " << angle_start*180.0/
M_PI <<
" - "
66 << angle_end*180.0/
M_PI <<
" degrees" << endl;
67 cout <<
"Angular increment: " << angle_step*180.0/
M_PI <<
" degrees" << endl;
68 cout <<
"Resolution limits: " << hires <<
" - " << lores <<
" A" << endl;
69 cout <<
"Radius: " << radius << endl;
70 cout <<
"Binning: " << bin << endl << endl;
74 angle_end += angle_step/2;
76 for ( na=0, angle = angle_start; angle <= angle_end; angle += angle_step ) na++;
83 cout <<
"DEBUG img_find_helix_parameters: binned size = " << pb->
size() << endl;
93 Bstring title(
"Helical parameter search");
112 cout <<
"Rise\tAngle\tOffset\tCC" << endl;
113 for ( i=0, angle = angle_start; angle <= angle_end; angle += angle_step, i++ ) {
118 for ( j=0; j<max_iter && fabs(shift.
z()) > 0.001; j++ ) {
122 shift = pb->
find_shift(pt, NULL, hires, lores, pb->
sizeZ()/4.0, 0, 1, cc);
124 if ( fabs(shift.
z()) > 0.001 ) {
125 translate[2] += dd*shift.
z();
131 offset = pb->
sampling().z()*sqrt(translate.
x()*translate.
x() + translate.
y()*translate.
y());
133 (*plot)[i+na] = angle*180.0/
M_PI;
134 (*plot)[i+2*na] = cc;
140 if ( mincc > cc ) mincc = cc;
142 cout << setprecision(6) << rise <<
tab << angle*180.0/
M_PI
143 <<
tab << offset <<
tab << cc << endl << flush;
148 if ( bestrise < 0 ) {
149 bestrise = -bestrise;
150 bestangle = -bestangle;
154 cout <<
"Best helical rise: " << bestrise <<
" A" << endl;
155 cout <<
"Best helical rotation angle: " << bestangle*180.0/
M_PI <<
" degrees" << endl;
156 cout <<
"Best correlation coefficient: " << bestcc << endl << endl;
161 txt =
Bstring(bestrise,
"Best helical rise: %g A");
163 txt =
Bstring(bestangle*180.0/
M_PI,
"Best helical rotation angle: %g degrees");
165 txt =
Bstring(bestcc,
"Best correlation coefficient: %g");
167 txt =
Bstring(mincc,
"Minimum correlation coefficient: %g");
190 double rise_step,
double angle_start,
double angle_end,
double angle_step,
191 int bin,
double radius)
193 rise_step = fabs(rise_step);
194 if ( rise_step < 0.001 ) rise_step = 0.001;
195 if ( rise_start > rise_end ) swap(rise_start, rise_end);
197 angle_step = fabs(angle_step);
198 if ( angle_step < 0.0001 ) angle_step = 0.0001;
199 if ( angle_start > angle_end ) swap(angle_start, angle_end);
201 if ( bin < 1 ) bin = 1;
202 if ( bin > 4 ) bin = 4;
204 if ( radius < 1 ) radius = p->
sizeX()/2;
211 double rise, angle, bestrise(0), bestangle(0);
212 double cc, bestcc(0), mincc(1);
217 cout <<
"Searching for helical parameters:" << endl;
218 cout <<
"Rise range " << rise_start <<
" - "
219 << rise_end <<
" A" << endl;
220 cout <<
"Rise increment: " << rise_step <<
" A" << endl;
221 cout <<
"Angular range " << angle_start*180.0/
M_PI <<
" - "
222 << angle_end*180.0/
M_PI <<
" degrees" << endl;
223 cout <<
"Angular increment: " << angle_step*180.0/
M_PI <<
" degrees" << endl;
224 cout <<
"Binning: " << bin << endl;
226 cout <<
"Radius: " << radius << endl << endl;
230 rise_end += rise_step/2;
231 angle_end += angle_step/2;
233 for ( na=0, rise = rise_start; rise < rise_end; rise += rise_step )
234 for ( angle = angle_start; angle <= angle_end; angle += angle_step ) na++;
241 cout <<
"DEBUG img_find_helix_parameters: binned size = " << pb->
size() << endl;
250 Bstring title(
"Helical parameter search");
269 cout <<
"Rise\tAngle\tCC" << endl;
270 for ( i=0, rise = rise_start; rise < rise_end; rise += rise_step ) {
271 for ( angle = angle_start; angle <= angle_end; angle += angle_step, i++ ) {
273 translate[2] = rise/pb->
sampling().z();
283 (*plot)[i+na] = angle*180.0/
M_PI;
284 (*plot)[i+2*na] = cc;
290 if ( mincc > cc ) mincc = cc;
292 cout << setprecision(6) << rise <<
tab << angle*180.0/
M_PI
293 <<
tab << cc << endl << flush;
299 if ( bestrise < 0 ) {
300 bestrise = -bestrise;
301 bestangle = -bestangle;
305 cout <<
"Best helical rise: " << bestrise <<
" A" << endl;
306 cout <<
"Best helical rotation angle: " << bestangle*180.0/
M_PI <<
" degrees" << endl;
307 cout <<
"Best correlation coefficient: " << bestcc << endl << endl;
312 txt =
Bstring(bestrise,
"Best helical rise: %g A");
314 txt =
Bstring(bestangle*180.0/
M_PI,
"Best helical rotation angle: %g degrees");
316 txt =
Bstring(bestcc,
"Best correlation coefficient: %g");
318 txt =
Bstring(mincc,
"Minimum correlation coefficient: %g");
326 double angle_start,
double angle_end,
double angle_step,
327 int bin,
double hires,
double lores,
double radius,
331 long m = (long) ((angle_end - angle_start)/angle_step) + 1;
337 for ( k = i*m, angle = angle_start; angle <= angle_end; angle += angle_step, k++ ) {
341 p1->
find_shift(p2, NULL, hires, lores, p1->
sizeX()/4, 0, 1, planf, planb, cc[k]);
369 double angle_start,
double angle_end,
double angle_step,
370 int bin,
double hires,
double lores,
double radius)
372 if ( hires > lores ) swap(hires, lores);
374 if ( radius <= 0 || radius > p->
sizeX()/2 ) radius = p->
sizeX()/2;
397 long nt(ptiles->
images());
398 long m = (long) ((angle_end - angle_start)/angle_step) + 1;
404 cout <<
"Calculating cross-correlations between helical segments:" << endl;
405 cout <<
"Thickness: " << tile_size.z() << endl;
406 cout <<
"Angular range " << angle_start*180.0/
M_PI <<
" - "
407 << angle_end*180.0/
M_PI <<
" degrees" << endl;
408 cout <<
"Angular increment: " << angle_step*180.0/
M_PI <<
" degrees" << endl;
409 cout <<
"Resolution limits: " << hires <<
" - " << lores <<
" A" << endl;
410 cout <<
"Radius: " << radius << endl;
411 cout <<
"Binning: " << bin << endl << endl;
414 double* cc =
new double[m*nt];
415 for ( i=0; i<m*nt; i++ ) cc[i] = 0;
421 dispatch_apply(nt-1, dispatch_get_global_queue(0, 0), ^(
size_t i){
423 angle_start, angle_end, angle_step,
424 bin, hires, lores, radius,
429 Bstring title(
"Helix segment correlation");
439 for ( i=1; i<ncol; i++ ) {
454 cout << endl <<
"Angle";
455 for ( i=0; i<nt-1; i++ ) cout <<
"\tCC(" << i+1 <<
")";
457 for ( k=0, angle = angle_start; angle <= angle_end; angle += angle_step, k++ ) {
458 cout << setprecision(4) << angle*180.0/
M_PI;
459 for ( i=0; i<nt-1; i++ ) cout <<
tab << cc[i*m + k];
464 for ( k=0, angle = angle_start; angle <= angle_end; angle += angle_step, k++ ) {
465 (*plot)[k] = angle*180.0/
M_PI;
466 for ( i=0; i<nt-1; i++ ) (*plot)[(i+1)*m+k] = cc[i*m + k];
494 cout <<
"Elliptical distortion: " << endl;
495 cout <<
"Major axis angle: " << angle*180.0/
M_PI <<
" degrees" << endl;
496 cout <<
"Shift at major axis: " << shift <<
" pixels" << endl << endl;
499 unsigned long xx, yy, zz, nn, i, j, inz, ix, iy;
501 double dx, dy, a, r, s, rx, ry, fx, fy;
502 float* nudata =
new float[datasize];
504 for ( i=0; i<datasize; i++ ) nudata[i] = 0;
506 for ( j=nn=0; nn<p->
images(); nn++ ) {
507 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
509 for ( yy=0; yy<p->
sizeY(); yy++ ) {
511 for ( xx=0; xx<p->
sizeX(); xx++, j++ ) {
513 r = sqrt(dx*dx + dy*dy);
514 a = 2*(atan2(dy, dx) - angle);
520 ix = (
unsigned long) rx;
521 iy = (
unsigned long) ry;
522 if ( ix < p->sizeX()-1 && iy < p->
sizeY()-1 ) {
525 i = inz + iy*p->
sizeX() + ix;
526 nudata[j] = (1-fx)*(1-fy)*(*p)[i] + fx*(1-fy)*(*p)[i+1] +
527 (1-fx)*fy*(*p)[i+p->
sizeX()] + fx*fy*(*p)[i+p->
sizeX()+1];
552 if ( p->
sizeZ() > 1 ) {
553 cerr <<
"Error: Line transforms can only be calculated from 2D images!" << endl;
563 cout <<
"Transforming lines of size: " << p->
sizeX() << endl;
565 unsigned long i, xx, yy, nn, hx(p->
sizeX()/2);
566 double dx, shift, phi, sx(p->
sizeX());
569 for ( i=nn=0; nn<p->
images(); nn++ ) {
572 cout <<
"Shift: " << shift << endl;
573 for ( yy=0; yy<p->
sizeY(); yy++ ) {
576 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
577 dx = (xx < hx)? xx: xx - sx;
579 data[xx].shift_phi(phi);
587 cout <<
"Finished transforming" << endl << endl;
609 if ( p->
sizeZ() > 1 ) {
610 cerr <<
"Error: Cross sections can only be calculated from 2D images!" << endl;
614 if ( hires < p->sampling().x() ) hires = p->
sampling().x();
618 cout <<
"Rotating image" << endl;
625 unsigned long i, j, xx, yy, nn;
627 double pad_factor(2), a, cosang, sinang, dx;
629 double pix_rise = helix_rise/p->
sampling().y();
630 double apz = helix_angle/pix_rise;
636 cout <<
"2D reconstruction:" << endl;
637 cout <<
"Size: " << size << endl;
638 cout <<
"Helical rise and angle: " << helix_rise <<
" " << helix_angle*180/
M_PI << endl;
639 cout <<
"Rotation per pixel along axis: " << apz*180/
M_PI << endl;
640 cout <<
"Resolution limit: " << hires <<
" Ã…" << endl;
641 cout <<
"Fourier transform size: " << ft_size << endl;
652 double hx = pp->sizeX()/2;
653 double sx = pp->sizeX();
654 double rscale = pcs->
sizeX()/(scale*sx);
655 double mx = sx * pp->sampling().
x() / hires;
656 if ( mx > sx ) mx = sx;
659 cout <<
"DEBUG img_helical_cross_section: sampling=" << pp->sampling().
x() <<
" mx=" << mx << endl;
661 for ( i=nn=0; nn<pp->images(); nn++ ) {
662 for ( yy=0; yy<pp->sizeY(); yy++ ) {
664 a = -(yy - pp->image[nn].origin().
y())*apz;
668 for ( xx=0; xx<pp->sizeX(); xx++, i++ ) {
669 dx = ( xx < hx )? xx: xx - sx;
670 if ( fabs(dx) <= mx ) {
672 ix = (long) floor(dx*cosang + 0.5);
673 iy = (long) floor(dx*sinang + 0.5);
674 if ( ix < 0 ) ix += pcs->
sizeX();
675 if ( iy < 0 ) iy += pcs->
sizeY();
676 if ( ix >= 0 && iy >= 0 && ix < pcs->sizeX() && iy < pcs->sizeY() ) {
678 pcs->
add(j, pp->complex(i));
690 cout <<
"Weighing reconstruction" << endl;
691 for ( i=0; i<pcs->
size().volume() * pcs->
images(); i++ )
692 if ( fom[i] > 0 ) pcs->
set(i, pcs->
complex(i) / fom[i]);
699 cout <<
"Backtransforming reconstruction" << endl << endl;
719 if ( snradius < 1 || snradius > p->
sizeX()/2 ) snradius = p->
sizeX()/4;
721 unsigned long nn, i, xx, yy, fn, bn;
722 double x2, y2, r2(snradius*snradius), v, fa, fv, ba, bv, snr;
725 cout <<
"Image\tSNR" << endl;
726 for ( i=nn=0; nn<p->
images(); nn++ ) {
728 fa = fv = ba = bv = 0;
729 for ( yy=0; yy<p->
sizeY(); yy++ ) {
732 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
736 if ( x2 + y2 <= r2 ) {
751 if ( fv < 0 ) fv = 0;
752 if ( bv < 0 ) bv = 0;
757 cerr <<
"Error: The background variance is zero or less! (" << bv <<
")" << endl;
760 cout << nn+1 <<
tab << snr << endl;
791 unsigned long i, xx, yy, zz;
792 double a, ca, sa, dx, dy, cx, cy;
795 cout <<
"Extruding the image:" << endl;
796 cout <<
"Helical rise and rotation: " << helix_rise <<
" A " << helix_angle*180.0/
M_PI <<
"°" << endl;
797 cout <<
"Length: " << length << endl;
800 unsigned long nusize = (
unsigned long) (p->
size().volume() * length);
801 float* nudata =
new float[nusize];
802 for ( i=0; i<nusize; i++ ) nudata[i] = 0;
804 for ( i=zz=0; zz<length; zz++ ) {
805 a = zz*helix_angle*p->
sampling().x()/helix_rise;
809 for ( yy=0; yy<p->
sizeY(); yy++ ) {
811 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
839 if ( lim_lo > lim_hi ) swap(lim_lo, lim_hi);
840 if ( lim_lo < 1 ) lim_lo = 1;
841 if ( lim_hi < 1 || lim_hi > p->
sizeX() ) lim_hi = p->
sizeX();
843 unsigned long i, xx, yy, zz, nn, left, right;
844 long n1, n2, hw(width/2);
845 double ox(p->
image->
origin().x()), a,
b, ci, slope_max, slope_min;
846 if ( ox < hw || ox > p->
sizeX() - hw ) ox = p->
sizeX()/2;
848 double* vx =
new double[p->
sizeX()];
849 double* vy =
new double[p->
sizeX()];
851 long leftmin = (long) (ox - lim_hi/2);
852 long leftmax = (long) (ox - lim_lo/2);
853 long rightmin = (long) (ox + lim_lo/2);
854 long rightmax = (long) (ox + lim_hi/2);
855 if ( leftmin < 0 ) leftmin = 0;
856 if ( leftmax <= leftmin ) leftmax = leftmin + 1;
857 if ( rightmax >= p->
sizeX() ) rightmax = p->
sizeX() - 1;
858 if ( rightmin >= rightmax ) rightmin = rightmax - 1;
860 for ( xx=0; xx<p->
sizeX(); xx++ ) vx[xx] = xx;
863 cout <<
"Calculating filament width:" << endl;
864 cout <<
"Origin: " << ox << endl;
865 cout <<
"Moving line window width: " << width << endl;
866 cout <<
"Allowed filament width range: " << lim_lo <<
" - " << lim_hi << endl;
868 lim_lo*p->
sampling().y() <<
" - " << lim_hi*p->
sampling().y() <<
" A" << endl << endl;
871 Bstring title(
"Filament width");
888 cout <<
"y\ty(A)\tleft\tright\twidth\twidth(A)" << endl;
889 for ( i=nn=0; nn<p->
images(); nn++ ) {
890 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
891 for ( yy=0; yy<p->
sizeY(); yy++ ) {
893 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) vy[xx] = (*p)[i];
894 slope_max = slope_min = 0;
895 for ( xx=0; xx<p->
sizeX(); xx++ ) {
897 if ( n1 < 0 ) n1 = 0;
902 if ( xx >= leftmin && xx <= leftmax && slope_max <
b ) {
906 if ( xx >= rightmin && xx <= rightmax && slope_min >
b ) {
916 << right <<
tab << right - left <<
tab
917 << (right - left)*p->
sampling().x() << endl;
949 if ( width < 1 || width >= p->
sizeX() ) width = p->
sizeX()/2;
952 cout <<
"Calculating filament density per pixel length:" << endl;
953 cout <<
"Filament width: " << width <<
954 " (" << width*p->
sampling().x() <<
" A)" << endl;
955 cout <<
"Image width: " << p->
sizeX() << endl << endl;
958 unsigned long i, xx, yy, zz, nn;
959 double t, tl, tr, hw((p->
sizeX()-width)/2), hwm(p->
sizeX()-hw);
964 for ( nn=0; nn<p->
images(); nn++ ) {
965 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
966 for ( yy=0; yy<p->
sizeY(); yy++ ) {
968 i = p->
index(0, 0, yy, zz, nn);
969 for ( xx=0; xx<hw; xx++, i++ )
971 for ( ; xx<hwm; xx++, i++ )
973 for ( ; xx<p->
sizeX(); xx++, i++ )
978 if ( tl < tr ) t -= tl;
980 i = nn*pd->sizeX() + yy;
void change_type(char letter)
Get the data type indicated by a single letter code.
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 edge(int type, Vector3< size_t > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Bimage * img_filament_density(Bimage *p, double width)
Estimates the density per pixel length of a filament.
Header file for image class.
Bplot * img_find_helix_parameters(Bimage *p, double angle_start, double angle_end, double angle_step, int bin, double hires, double lores, double radius)
Finds the best helix parameters for helical map.
void fftw(fftw_plan plan, int howmany, fftw_complex *in, int istride, int idist, fftw_complex *out, int ostride, int odist)
int img_helix_segment_correlation_one(Bimage *p, long i, double angle_start, double angle_end, double angle_step, int bin, double hires, double lores, double radius, fft_plan planf, fft_plan planb, double *cc)
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.
Bplot * img_filament_width(Bimage *p, int width, int lim_lo, int lim_hi)
Estimates the width of a filament.
Header file for general symmetry functions.
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
Dealing with images with helical symmetry.
int img_transform_lines(Bimage *p)
double img_snvariance(Bimage *p, double snradius)
Calculates the ratio of variance inside and outside a given radius.
Vector class for 3-value vectors used in 3D space. The internal variables are an array of 3 numbers...
size_t index(size_t nx, size_t ny) const
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.
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Vector3< size_t > size() const
double interpolate(size_t cc, double xx, double yy, double zz, size_t nn, double fill) const
Interpolates using a given location.
size_t statistics()
Calculates the statistics for an image.
Bimage * img_helical_cross_section(Bimage *p, double helix_rise, double helix_angle, double scale, double hires)
Calculates a helical cross section from line transforms of a filament.
int part_ft_size(int xsize, double scale, int pad_factor)
Sets the Fourier transform size for 2D particle images for reconstruction.
General image parameter class.
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Bimage * pad_copy(size_t sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
float * FOM_initialize()
Allocates and initializes the FOM block.
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Complex< double > complex(size_t j)
Returns a complex value at the given index.
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
void simple_to_complex()
A simple image is converted to a complex image.
void FOM_delete()
Deallocates the FOM block.
int img_distort_elliptically(Bimage *p, double angle, double shift)
Transforms a tubular cylinder to an elliptical profile.
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.
Bimage * extract(size_t nn)
Extracts one sub-image into new image structure.
void reslice(const char *order)
void add_text(Bstring &s)
unsigned char * data_pointer()
Bimage * copy()
Copies the header information and data of an image into a new image structure.
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
Header file for matrix functions.
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
int img_extrude_cross_section(Bimage *p, int length, double helix_rise, double helix_angle, int fill_type, double fill)
Extrudes a 2D cross section into a 3D continuous helix.
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.
int integer_interpolation(int integer_factor)
void spectrum(double value, double cmin, double cmax)
Functions for reconstruction.
ostream & tab(ostream &out)
Bplot * img_helix_segment_correlation(Bimage *p, int thickness, double angle_start, double angle_end, double angle_step, int bin, double hires, double lores, double radius)
Calculates the correlation over rotation angles between helical segments.
double linear_least_squares(int n1, int n2, double *x, double *y, double &a, double &b)
Does a linear least squares fit between two vectors.