Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_multifit.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "img_multifit.h"
11 #include "utilities.h"
12 #include "timer.h"
13 
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
35 Bimage* img_multifit(Bimage* p, Bimage* ptemp, View* view,
36  double alpha, double alpha_step, double hires, double lores, Bimage* pmask, double threshold)
37 {
38  int mode(0); // Mode: 0=global, 1=refine, 2=symmetry
39  size_t i, n(0), nviews;
40  double alpha_min(0);
41  double alpha_max(TWOPI - alpha_step/2);
42  double a;
43  double fommin(1), fommax(0), fomavg(0), fomstd(0);
44  View* v;
45 
46  if ( alpha >= 0 ) { // Case of refinement
47  mode = 1;
48  alpha_min = alpha - alpha_step;
49  alpha_max = alpha + alpha_step;
50  } else if ( alpha_step >= 1.999*M_PI ) mode = 2; // Symmetry
51 
52  if ( !p ) return NULL;
53 
54  p->change_type(Float);
55  ptemp->change_type(Float);
56  p->rescale_to_avg_std(0, 1);
57  ptemp->rescale_to_avg_std(0, 1);
59  ptemp->calculate_background();
60 
61  Vector3<double> origin(ptemp->image->origin());
62 
63  if ( origin.sum() == 0 ) {
64  origin = p->size()/2;
65  p->origin(origin);
66  }
67 
68  for ( nviews=0, v=view; v; v=v->next )
69  for ( a=alpha_min; a<=alpha_max; a+=alpha_step ) nviews++;
70 
71  View* view_arr = new View[nviews];
72 
73  for ( n=0, v=view; v; v=v->next )
74  for ( a=alpha_min; a<=alpha_max; a+=alpha_step, n++ ) {
75  view_arr[n] = *v;
76  if ( mode < 2 ) view_arr[n][3] = a;
77  }
78 
79  if ( verbose ) {
80  cout << "Finding a template in a map:" << endl;
81  cout << "Number of views: " << nviews << endl;
82  cout << "Alpha step size: " << alpha_step*180.0/M_PI << endl;
83  cout << "Origin: " << origin << endl;
84  }
85 
86  Bimage* pfit = new Bimage(Float, TView, p->size(), 1);
87  float* fom = pfit->FOM_initialize();
88  pfit->sampling(p->sampling());
89 
90  if ( verbose & VERB_RESULT )
91  cout << "View\tCCmax" << endl;
92 
93  for ( n=0; n<nviews; n++ ) {
94  fommax = img_multifit_for_view(p, ptemp, view_arr[n], hires, lores, pmask, threshold, pfit);
95  if ( verbose )
96  cout << view_arr[n] << tab << fommax << endl;
97  }
98 
99  size_t datasize = (size_t) pfit->size().volume();
100 
101  for ( i=0, fommax=-1, fommin=1, fomavg=fomstd=0; i<datasize; i++ ) {
102  if ( fommin > fom[i] ) fommin = fom[i];
103  if ( fommax < fom[i] ) fommax = fom[i];
104  fomavg += fom[i];
105  fomstd += fom[i]*fom[i];
106  }
107 
108  delete[] view_arr;
109 
110  if ( verbose ) {
111  fomavg /= datasize;
112  fomstd = fomstd/datasize - fomavg*fomavg;
113  if ( fomstd > 0 ) fomstd = sqrt(fomstd);
114  else fomstd = 0;
115  cout << "FOM minimum and maximum: " << fommin << " " << fommax << endl;
116  cout << "FOM average: " << fomavg << endl;
117  cout << "FOM standard deviation: " << fomstd << endl << endl;
118  }
119 
120  return pfit;
121 }
122 
140 double img_multifit_for_view(Bimage* p, Bimage* ptemp, View view,
141  double hires, double lores, Bimage* pmask, double threshold, Bimage* pfit)
142 {
143  Matrix3 mat = view.matrix();
144 // mat = mat.transpose();
145 
146  Bimage* prot = ptemp->rotate(ptemp->size(), mat);
147 
148  Bimage* pcc = p->cross_correlate(prot, hires, lores, pmask);
149 
150  size_t i, datasize = (size_t) pfit->size().volume();
151  double fommax(0);
152  float* fom = pfit->FOM();
153 
154  for ( i=0; i<datasize; i++ ) {
155  if ( fom[i] < (*pcc)[i] ) {
156  fom[i] = (*pcc)[i];
157  pfit->set(i, view);
158  }
159  if ( fommax < (*pcc)[i] ) fommax = (*pcc)[i];
160  }
161 
162  delete prot;
163  delete pcc;
164 
165  return fommax;
166 }
167 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1544
Header file for image class.
View * next
Definition: View.h:28
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
Header file for searching for a template in a map and returning multiple hits in a model...
Definition: Bimage.h:54
int verbose
Vector3< size_t > size() const
Definition: Bimage.h:327
Utilities for timing functions.
General image parameter class.
Definition: Bimage.h:152
#define M_PI
Definition: utilities.h:53
int rescale_to_avg_std(double nuavg=0, double nustd=1)
Rescales the image data to a given average and standard deviation.
float * FOM_initialize()
Allocates and initializes the FOM block.
Definition: Bimage.cpp:677
void sampling(double s)
Definition: Bimage.h:440
float * FOM()
Definition: Bimage.h:239
Bimage * cross_correlate(Bimage *p, Bimage *pmask=NULL)
Definition: Bimage.h:792
void origin(double x, double y, double z)
Definition: Bimage.h:132
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:977
#define VERB_RESULT
Definition: utilities.h:99
Matrix3 matrix()
Definition: View.h:137
Definition: View.h:26
int rotate()
Rotates an image using parameters defined in the image in place.
Bimage * img_multifit(Bimage *p, Bimage *ptemp, View *view, double alpha, double alpha_step, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template.
double img_multifit_for_view(Bimage *p, Bimage *ptemp, View view, double hires, double lores, Bimage *pmask, double threshold, Bimage *pfit)
Searches a 2D/3D density map for a template using a specific view.
#define TWOPI
Definition: utilities.h:62
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bsub_image * image
Definition: Bimage.h:183
void origin(double ox, double oy, double oz)
Definition: Bimage.h:483