20 double hires,
double lores,
double search_radius,
Bimage* pmask,
45 double hires,
double lores,
double search_radius,
48 int n, nviews, ibest(0);
53 for ( nviews=0, v=view; v; v=v->
next ) nviews++;
56 double* cc =
new double[nviews];
59 for ( n=0, v=view; v; v=v->
next, n++ ) view_arr[n] = *v;
61 if ( search_radius < 0 ) search_radius = ptemp->
sizeX()/4;
63 if ( !p )
return best;
74 if ( origin.length() == 0 ) {
75 origin = ptemp->
size()/2;
80 cout <<
"Finding a template in a map:" << endl;
81 cout <<
"Template: " << ptemp->
file_name() << endl;
82 cout <<
"Map: " << p->
file_name() << endl;
83 cout <<
"Template origin: " << ptemp->
image->
origin() << endl;
84 cout <<
"Map origin: " << p->
image->
origin() << endl;
85 cout <<
"Number of views: " << nviews << endl;
86 cout <<
"Resolution range: ";
87 if ( lores <= 0 ) cout <<
"inf";
89 cout <<
" - " << hires <<
" A" << endl;
90 cout <<
"Shift search radius: " << search_radius <<
" pixels" << endl;
97 dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(
size_t k){
99 search_radius, pmask, cc[k], planf, planb);
102 #pragma omp parallel for
103 for ( n=0; n<nviews; n++ ) {
105 search_radius, pmask, cc[n], planf, planb);
110 cout <<
"View\ttx\tty\ttz\tvx\tvy\tvz\tva\tCC" << endl;
111 for ( n=0; n<nviews; n++ ) {
112 if ( best < cc[n] ) {
117 cout << n <<
tab << setprecision(2) << translate[n] <<
tab
118 << setprecision(4) << view_arr[n] <<
tab << cc[n] << endl;
121 bestview = view_arr[ibest];
131 cout <<
"Best view:\t" << ibest <<
tab << setprecision(2) << translate[ibest] <<
tab
132 << setprecision(4) << bestview <<
tab << best << endl;
142 *currview = bestview;
143 *currshift = translate[ibest];
169 double hires,
double lores,
double search_radius,
Bimage* pmask,
220 if ( !p )
return -1e37;
222 int n, nviews, ibest;
227 for ( nviews=0, v=view; v; v=v->
next ) nviews++;
230 double* cc =
new double[nviews];
232 for ( n=0, v=view; v; v=v->
next, n++ ) view_arr[n] = *v;
235 cout <<
"Number of views: " << nviews << endl;
238 dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(
size_t k){
239 cc[k] =
img_fcc_for_view(p, ptemp, view_arr[k], kernel, hires, lores, pmask);
242 #pragma omp parallel for
243 for ( n=0; n<nviews; n++ )
244 cc[n] =
img_fcc_for_view(p, ptemp, view_arr[n], kernel, hires, lores, pmask);
247 double ccmin(1), ccmax(-1), ccavg(0), ccstd(0);
249 for ( n=ibest=0; n<nviews; n++ ) {
250 if ( best < cc[n] ) {
254 if ( ccmin > cc[n] ) ccmin = cc[n];
255 if ( ccmax < cc[n] ) ccmax = cc[n];
257 ccstd += cc[n]*cc[n];
261 ccstd = ccstd/n - ccavg*ccavg;
262 if ( ccstd > 0 ) ccstd = sqrt(ccstd);
266 cout <<
"Min, max, avg, std: " << ccmin <<
" " << ccmax <<
" " << ccavg <<
" " << ccstd << endl;
268 p->
view(view_arr[ibest]);
299 double minrad = (lores>0)? ptemp->
real_size().
x()/lores: 0;
300 double maxrad = (hires>0)? ptemp->
real_size().
x()/hires: ptemp->
sizeX()/2;
301 if ( maxrad > ptemp->
sizeX()/2 ) maxrad = ptemp->
sizeX()/2;
310 double d, pwr1(0), pwr2(0), cc(0);
314 for ( c[2]=min.
z(); c.
z()<=max.
z(); c[2]+=1 ) {
315 for ( c[1]=min.
y(); c.
y()<=max.
y(); c[1]+=1 ) {
316 for ( c[0]=min.
x(); c.
x()<=max.
x(); c[0]+=1 ) {
318 if ( d >= minrad && d <= maxrad ) {
320 if ( ci.
x() < 0 ) ci[0] += p->
sizeX();
321 if ( ci.
y() < 0 ) ci[1] += p->
sizeY();
322 if ( ci.
z() < 0 ) ci[2] += p->
sizeZ();
324 if ( !pmask || ( pmask && (*pmask)[i] ) ) {
339 if ( d > 0 ) cc = cv.
amp()/sqrt(d);
342 cout << view <<
tab << n <<
tab << cc << endl;
365 FSI_Kernel* kernel,
double hires,
double lores,
double searchrad,
372 double minrad = (lores>0)? ptemp->
real_size().
x()/lores: 0;
374 if ( maxrad > ptemp->
sizeX()/2 ) maxrad = ptemp->
sizeX()/2;
381 double d, pwr1(0), pwr2(0);
386 for ( c[2]=min.
z(); c.
z()<=max.
z(); c[2]+=1 ) {
387 for ( c[1]=min.
y(); c.
y()<=max.
y(); c[1]+=1 ) {
388 for ( c[0]=min.
x(); c.
x()<=max.
x(); c[0]+=1 ) {
390 if ( d >= minrad && d <= maxrad ) {
392 if ( ci.
x() < 0 ) ci[0] += p->
sizeX();
393 if ( ci.
y() < 0 ) ci[1] += p->
sizeY();
394 if ( ci.
z() < 0 ) ci[2] += p->
sizeZ();
396 if ( !pmask || ( pmask && (*pmask)[i] ) ) {
409 double scale = 1/sqrt(pwr1*pwr2);
411 for ( i=0; i<pcc->
data_size(); i++ ) pcc->
set(i, (*pcc)[i] * scale);
438 double* d =
new double[nviews];
441 for ( n=0; n<nviews; n++ ) d[n] = bestview.
distance(view_arr[n]);
450 double ccmax, ccavg, ccd;
451 double s,
sb, t, a, r, rb;
453 for ( i=0, ccavg=0, ccmax=0; i<n; i++ ) {
454 if ( ccmax < cc[i] ) ccmax = cc[i];
460 for ( s=0.001, sb=0, rb=1e10; s<=1; s*=1.01 ) {
461 for ( i=0, r=0; i<n; i++ ) {
463 a = cc[i] - ccavg - ccd*exp(-0.5*t*t);
474 cout <<
"Gaussian fit:\t" << ccavg <<
tab << ccd <<
tab << sb <<
tab << rb << endl;
482 double s,
sb, t, a, ab,
b, bb, c, cb;
483 double* x =
new double[n];
484 double* y =
new double[n];
486 for ( i=0; i<n; i++ ) y[i] = cc[i];
488 for ( s=0.001, cb=0, sb=0; s<=1; s*=1.01 ) {
489 for ( i=0; i<n; i++ ) {
491 x[i] = exp(-0.5*t*t);
503 cout <<
"Gaussian fit:\t" << ab <<
tab << bb <<
tab << sb <<
tab << cb << endl;
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 refine_peak()
Refines the position of a peak to sub-voxel resolution.
Matrix3 transpose() const
Header file for functions to find a template in an image.
Header file for image class.
double img_fcc_for_view(Bimage *p, Bimage *ptemp, View view, FSI_Kernel *kernel, double hires, double lores, Bimage *pmask)
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
double img_find_density(Bimage *p, Bimage *ptemp, View *view, double hires, double lores, double search_radius, Bimage *pmask, View *currview, Vector3< float > *currshift)
Searches a 2D/3D density map for a template.
int gaussian_fit(int n, double *d, double *cc)
double img_fcc_search(Bimage *p, Bimage *ptemp, View *view, FSI_Kernel *kernel, double hires, double lores, Bimage *pmask)
Fast cross-correlation searches a 2D/3D density map for a template.
Vector3< double > img_find_density_for_view(Bimage *p, Bimage *ptemp, View view, double hires, double lores, double search_radius, Bimage *pmask, double &cc, fft_plan planf, fft_plan planb)
Vector3< size_t > size() const
double * distances_from_view(View bestview, int nviews, View *view_arr)
General image parameter class.
Vector3< double > real_size()
Header file for generalized linked list functions.
int rescale_to_avg_std(double nuavg=0, double nustd=1)
Rescales the image data to a given average and standard deviation.
Complex< double > complex(size_t j)
Returns a complex value at the given index.
Complex< double > fspace_interpolate(size_t img_num, Vector3< double > m, FSI_Kernel *kernel)
Calculates the complex value at an image location by kernel-based interpolation.
void file_name(Bstring s)
int find_peak(double radius, double sigma)
Finds the peak in an image to the nearest voxel.
Bimage * cross_correlate(Bimage *p, Bimage *pmask=NULL)
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
void view(double vx, double vy, double vz, double va)
void complex_to_real()
The real part of a complex image is written to a simple image.
Header file for matrix functions.
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.
double distance(const View &view)
double img_cc_for_view(Bimage *p, Bimage *ptemp, View view, FSI_Kernel *kernel, double hires, double lores, double searchrad, Bimage *pmask)
ostream & tab(ostream &out)
void origin(double ox, double oy, double oz)
int gaussian_fit2(int n, double *d, double *cc)
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.