Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_project.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "img_project.h"
11 #include "linked_list.h"
12 #include "symmetry.h"
13 #include "Complex.h"
14 #include "Matrix3.h"
15 #include "utilities.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 
21 
22 
23 double img_compare_one_projection(Bimage* p, Bimage* pref, size_t i,
24  double hires, double lores, double radius, double sigma,
25  Matrix* cc, fft_plan planf, fft_plan planb)
26 {
27  size_t j;
28  double cc1(0);
29  Bimage* px;
30 
31  Bimage* prefx = pref->extract(i);
32 
33  for ( j=0; j<p->images(); j++ ) {
34  px = p->extract(j);
35  px->find_shift(prefx, NULL, hires, lores, radius, sigma, 0, planf, planb, cc1);
36  (*cc)[i][j] = cc1;
37 // cc[j] = (px->image->origin() - prefx->image->origin()).length();
38 // cout << "Shift: " << (px->image->origin() - prefx->image->origin()).length() << endl;
39  delete px;
40  }
41 
42  delete prefx;
43 
44  return 0;
45 }
46 
47 Matrix* img_compare_projections_driver(Bimage* p, double SNR, double hires, double lores, double shift_limit)
48 {
49  if ( lores <= 0 ) lores = 1e30;
50  if ( hires < p->sampling().x() ) hires = p->sampling().x();
51  if ( hires > lores ) swap(hires, lores);
52  if ( shift_limit < 0 ) shift_limit = 1e30;
53 
54  p->change_type(Float);
55 
56  Bimage* pt = p->copy();
57 
58  if ( SNR < 101 ) pt->noise_gaussian(0, pt->standard_deviation()/sqrt(SNR));
59 
60 // write_img("pt.pif", pt);
61 
62  size_t n(p->images());
63  double sigma(0);
64 
65  Matrix* cc = new Matrix(n, n);
66 
67  fft_plan planf = p->fft_setup(FFTW_FORWARD, 1);
68  fft_plan planb = p->fft_setup(FFTW_BACKWARD, 1);
69 
70 #ifdef HAVE_GCD
71  dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(size_t i){
72  img_compare_one_projection(pt, p, i, hires, lores, shift_limit, sigma, cc, planf, planb);
73  });
74 #else
75 #pragma omp parallel for
76  for ( long i=0; i<n; i++ )
77  img_compare_one_projection(pt, p, i, hires, lores, shift_limit, sigma, cc, planf, planb);
78 #endif
79 
80  fft_destroy_plan(planf);
81  fft_destroy_plan(planb);
82 
83  delete pt;
84 
85  return cc;
86 }
87 
88 
98 Matrix img_compare_projections(Bimage* p, double SNR, double hires, double lores, double shift_limit)
99 {
100  Matrix* ccp = img_compare_projections_driver(p, SNR, hires, lores, shift_limit);
101  Matrix cc = *ccp;
102 
103  unsigned long i, j, m, n(p->images());
104  double ccmax(0), ccavg(0), ccstd(0);
105 
106  if ( verbose & VERB_FULL )
107  cout << "Image1\tImage2\tCC" << endl;
108  for ( i=m=0; i<n; i++ ) {
109  for ( j=0; j<n; j++, m++ ) {
110  if ( i == j ) ccmax += cc[i][j];
111  else {
112  ccavg += cc[i][j];
113  ccstd += cc[i][j]*cc[i][j];
114  }
115  if ( verbose & VERB_FULL )
116  cout << setprecision(5) << i << tab << j << tab << cc[i][j] << endl;
117  }
118  }
119 
120  m -= n;
121  ccmax /= n;
122  ccavg /= m;
123  ccstd = ccstd/m - ccavg * ccavg;
124  if ( ccstd > 0 ) ccstd = sqrt(ccstd);
125  else ccstd = 0;
126 
127  if ( verbose ) {
128  cout << "Correlating all projections:" << endl;
129  cout << "Imposed SNR: " << SNR << endl;
130  cout << "Resolution limits: " << hires << " " << lores << endl;
131  cout << "Shift limit: " << shift_limit << endl;
132  cout << "CC maximum: " << ccmax << endl;
133  cout << "CC average: " << ccavg << endl;
134  cout << "CC standard deviation: " << ccstd << endl;
135  cout << "Relative maximum sigma units: " << (ccmax - ccavg)/ccstd << endl;
136  cout << "Fisher's z-transform: " << fishers_z_transform(ccmax) << endl;
137 // if ( verbose & VERB_PROCESS )
138  cout << cc << endl;
139  }
140 
141  return cc;
142 }
143 
151 int img_compare_projections_lowpass(Bimage* p, double SNR, double shift_limit)
152 {
153 
154  unsigned long i, j, k, m, n(p->images());
155  double hires, lores(p->real_size().x());
156  double ccmax, ccavg, ccstd, cct;
157  Matrix* ccp;
158  Matrix cc;
159 
160  if ( verbose )
161  cout << "ResLim\tk\tCCmax\tCCavg\tCCstd\tCCth" << endl;
162  for ( hires = p->sampling().x(); hires < lores; hires *= 2 ) {
163  ccp = img_compare_projections_driver(p, SNR, hires, lores, shift_limit);
164  cc = *ccp;
165 
166  ccmax = ccavg = ccstd = 0;
167  for ( m=i=0; i<n; i++ ) {
168  for ( j=0; j<n; j++, m++ ) {
169  if ( i == j ) ccmax += cc[i][j];
170  else {
171  ccavg += cc[i][j];
172  ccstd += cc[i][j]*cc[i][j];
173  }
174  if ( verbose & VERB_FULL )
175  cout << setprecision(5) << i << tab << j << tab << cc[i][j] << endl;
176  }
177  }
178 
179  m -= n;
180  ccmax /= n;
181  ccavg /= m;
182  ccstd = ccstd/m - ccavg * ccavg;
183  if ( ccstd > 0 ) ccstd = sqrt(ccstd);
184  else ccstd = 0;
185 
186  k = (unsigned long) (p->real_size().x()/hires);
187  cct = p->sampling().x()/hires;
188  cct *= M_PI*cct;
189  if ( cct > 1 ) cct = 1;
190  cct = sqrt(SNR/(SNR + cct));
191 
192  delete ccp;
193 
194  if ( verbose )
195  cout << hires << tab << k << tab << ccmax << tab << ccavg << tab << ccstd << tab << cct << endl;
196  }
197 
198  return 0;
199 }
200 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1544
Header file for projection functions.
Header file for image class.
int verbose
Header file for general symmetry functions.
Header file for general utilities.
Class for complex numbers.
3x3 Matrix manipulation functions
int noise_gaussian(double ravg=0, double rstd=1)
Generates an image with a gaussian random distribution of densities.
General image parameter class.
Definition: Bimage.h:152
Vector3< double > real_size()
Definition: Bimage.h:437
#define M_PI
Definition: utilities.h:52
Header file for generalized linked list functions.
void sampling(double s)
Definition: Bimage.h:440
Type x()
Definition: Vector3.h:137
Bimage * extract(size_t nn)
Extracts one sub-image into new image structure.
double standard_deviation()
Definition: Bimage.h:465
size_t images()
Definition: Bimage.h:438
double img_compare_one_projection(Bimage *p, Bimage *pref, size_t i, double hires, double lores, double radius, double sigma, Matrix *cc, fft_plan planf, fft_plan planb)
Definition: img_project.cpp:23
Definition: Matrix.h:21
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2414
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
Definition: Bimage_fft.cpp:29
Matrix * img_compare_projections_driver(Bimage *p, double SNR, double hires, double lores, double shift_limit)
Definition: img_project.cpp:47
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:97
double fishers_z_transform(double value)
Calculates Fisher's z-transform.
Definition: math_util.cpp:268
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 img_compare_projections_lowpass(Bimage *p, double SNR, double shift_limit)
Compares a set of projections from a 3D density map.
#define VERB_FULL
Definition: utilities.h:102
Matrix img_compare_projections(Bimage *p, double SNR, double hires, double lores, double shift_limit)
Compares a set of projections from a 3D density map.
Definition: img_project.cpp:98
ostream & tab(ostream &out)
Definition: utilities.cpp:21