Bsoft  2.1.0
Bernard's software package
Bimage_background.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
27 int Bimage::calculate_background(long nn, int flag)
28 {
29  if ( !data_pointer() ) return -1;
30 
31  if ( data_type() == Bit ) return 0;
32 
33  long i, j, xx, yy, zz, cc, nm(0);
34  double x2(0), y2(0), z2(0), r;
35  double bkg(0);
36  Vector3<long> h(x/2, y/2, z/2);
37  Vector3<double> ori(image[nn].origin());
38 
39  i = (long) (nn*x*y*z);
40  for ( zz=0; zz<z; zz++ ) {
41  if ( z > 1 ) {
42  z2 = (zz - ori[2])/h[2];
43  z2 *= z2;
44  }
45  for ( yy=0; yy<y; yy++ ) {
46  if ( y > 1 ) {
47  y2 = (yy - ori[1])/h[1];
48  y2 *= y2;
49  }
50  for ( xx=0; xx<x; xx++, i++ ) {
51  if ( x > 1 ) {
52  x2 = (xx - ori[0])/h[0];
53  x2 *= x2;
54  }
55  r = x2 + y2 + z2 - 1;
56  if ( flag == 2 ) r = -r;
57  else if ( flag == 3 ) r = -r - 0.5;
58  if ( r > 0 ) {
59  if ( compoundtype == TComplex ) {
60  bkg += complex(i).power();
61  nm++;
62  } else {
63  for ( cc=0, j=i*c; cc<c; cc++, j++ ) {
64  bkg += (*this)[j];
65  nm++;
66  }
67  }
68  }
69  }
70  }
71  }
72 
73  if ( nm ) image[nn].background(bkg/nm);
74 
75  if ( verbose & VERB_DEBUG )
76  cout << "DEBUG Bimage::calculate_background: img=" << nn << " nm=" << nm <<
77  " bkg=" << image[nn].background() << endl;
78 
79  return 0;
80 }
81 
92 {
93  if ( !data_pointer() ) return -1;
94 
95  if ( data_type() == Bit ) return 0;
96 
97  if ( verbose & VERB_FULL )
98  cout << "Calculating the background values of all images" << endl << endl;
99 
100 #ifdef HAVE_GCD
101  dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(size_t nn){
102  calculate_background(nn, flag);
103  });
104 #else
105 #pragma omp parallel for
106  for ( long nn=0; nn<n; nn++ )
107  calculate_background(nn, flag);
108 #endif
109 
110  return 0;
111 }
112 
124 int Bimage::calculate_background(Bimage* pmask, long nn, int flag)
125 {
126  if ( !data_pointer() ) return -1;
127 
128  if ( !pmask ) return -1;
129 
130  if ( data_type() == Bit ) return 0;
131 
132  long i(nn*x*y*z), j, k, m(0), cc, nm(0);
133  double bkg(0);
134 
135  if ( pmask->n == n ) m = i;
136 
137  if ( flag ) {
138  for ( k=0; k<image_size(); ++k, ++i, ++m ) if ( (*pmask)[m] > 0.5 ) {
139  for ( cc=0, j=i*c; cc<c; cc++, j++ ) bkg += (*this)[j];
140  nm++;
141  }
142  } else {
143  for ( k=0; k<image_size(); ++k, ++i, ++m ) if ( (*pmask)[m] < 0.5 ) {
144  for ( cc=0, j=i*c; cc<c; cc++, j++ ) bkg += (*this)[j];
145  nm++;
146  }
147  }
148 
149  if ( nm ) image[nn].background(bkg/nm);
150 
151  if ( verbose & VERB_DEBUG )
152  cout << "DEBUG img_calculate_background: img=" << nn << " nm=" << nm << " bkg=" << image[nn].background() << endl;
153 
154  return 0;
155 }
156 
157 
169 {
170  if ( !data_pointer() ) return -1;
171 
172  if ( !pmask ) return -1;
173 
174  if ( data_type() == Bit ) return 0;
175 
176  if ( verbose & VERB_FULL )
177  cout << "Calculating the background values of all images" << endl << endl;
178 
179 #ifdef HAVE_GCD
180  dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(size_t nn){
181  calculate_background(pmask, nn, flag);
182  });
183 #else
184 #pragma omp parallel for
185  for ( long nn=0; nn<n; nn++ )
186  calculate_background(pmask, nn, flag);
187 #endif
188 
189  return 0;
190 }
191 
202 int Bimage::correct_background(long nn, int flag)
203 {
204  if ( !data_pointer() ) return -1;
205 
206  if ( data_type() == Bit ) return 0;
207 
208  calculate_background(nn, flag);
209 
210  long i, j, xx, yy, zz, cc, nm(0);
211  double x2(0), y2(0), z2(0);
212  double bkg(image[nn].background());
213  Vector3<long> h(x/2, y/2, z/2);
214  Vector3<double> ori(image[nn].origin());
215 
216  i = (long) (nn*x*y*z);
217  for ( zz=0; zz<z; zz++ ) {
218  if ( z > 1 ) {
219  z2 = (zz - ori[2])/h[2];
220  z2 *= z2;
221  }
222  for ( yy=0; yy<y; yy++ ) {
223  if ( y > 1 ) {
224  y2 = (yy - ori[1])/h[1];
225  y2 *= y2;
226  }
227  for ( xx=0; xx<x; xx++, i++ ) {
228  if ( x > 1 ) {
229  x2 = (xx - ori[0])/h[0];
230  x2 *= x2;
231  }
232  if ( x2 + y2 + z2 > 1 ) {
233  for ( cc=0, j=i*c; cc<c; cc++, j++ ) {
234  set(j, bkg);
235  nm++;
236  }
237  }
238  }
239  }
240  }
241 
242  if ( verbose & VERB_DEBUG )
243  cout << "DEBUG Bimage::correct_background: img=" << nn << " bkg=" << bkg << endl;
244 
245  return 0;
246 }
247 
248 
259 {
260  if ( !data_pointer() ) return -1;
261 
262  if ( data_type() == Bit ) return 0;
263 
264  if ( verbose & VERB_FULL )
265  cout << "Correcting the background values of all images" << endl << endl;
266 
267 #ifdef HAVE_GCD
268  dispatch_apply(n, dispatch_get_global_queue(0, 0), ^(size_t nn){
269  correct_background(nn, flag);
270  });
271 #else
272 #pragma omp parallel for
273  for ( long nn=0; nn<n; nn++ )
274  correct_background(nn, flag);
275 #endif
276 
277  if ( statistics() )
278  cerr << tab << "in Bimage::correct_background" << endl;
279 
280  return 0;
281 }
282 
296 int Bimage::correct_background(Bimage* pmask, int flag)
297 {
298  if ( !data_pointer() ) return -1;
299 
300  if ( !pmask ) return -1;
301 
302  if ( data_type() == Bit ) return 0;
303 
304  if ( flag > 1 ) calculate_background(flag);
305  else calculate_background(pmask, flag);
306 
307  long i, j, k, m(0), nn, cc, nm;
308  double bkg;
309 
310  if ( verbose & VERB_FULL )
311  cout << "Correcting the background values of all images" << endl << endl;
312 
313  for ( i=nn=0; nn<n; nn++ ) {
314  if ( pmask->n == n ) m = i;
315  else m = 0;
316  bkg = image[nn].background();
317  nm = 0;
318  for ( k=0; k<image_size(); ++k, ++i, ++m ) if ( (*pmask)[m] < 0.5 ) {
319  for ( cc=0, j=i*c; cc<c; cc++, j++ ) set(j, bkg);
320  nm++;
321  }
322  }
323 
324  if ( statistics() )
325  cerr << tab << "in Bimage::correct_background" << endl;
326 
327  return 0;
328 }
329 
338 {
339  return Bimage::shift_background(0);
340 }
341 
352 {
353  if ( !data_pointer() ) return -1;
354 
355  if ( data_type() == Bit ) return 0;
356 
357  long i, j, nn, imagesize = x*y*z;
358  if ( compound_type() != TComplex ) imagesize *= c;
359 
360  double shift, v;
361  Complex<double> cv;
362 
363  if ( verbose & VERB_FULL )
364  cout << "Shifting the background values of all images to " << bkg << endl;
365 
366  for ( nn=i=0; nn<n; nn++ ) {
367 // shift = bkg - image[nn].background();
368 // image[nn].background(bkg);
369  shift = bkg - image[nn].background();
370  image[nn].background(bkg);
371  for ( j=0; j<imagesize; i++, j++ ) {
372  if ( compound_type() != TComplex ) {
373  v = (*this)[i];
374  v += shift;
375  set(i, v);
376  } else {
377  cv = complex(i);
378  cv += shift;
379  set(i, cv);
380  }
381  }
382  }
383 
384  if ( statistics() )
385  cerr << tab << "in Bimage::shift_background" << endl;
386 
387  return 0;
388 }
389 
verbose
int verbose
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
Complex
Definition: Complex.h:23
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Vector3< long >
TComplex
@ TComplex
Definition: Bimage.h:60
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::correct_background
int correct_background(long nn, int flag)
Corrects the background for one sub-image.
Definition: Bimage_background.cpp:202
Bsub_image::background
double background()
Definition: Bimage.h:125
Bimage::shift_background
int shift_background(double bkg)
Sets the background for each sub-image to a given value.
Definition: Bimage_background.cpp:351
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
Bimage.h
Header file for image class.
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::subtract_background
int subtract_background()
Subtracts the background for each sub-image.
Definition: Bimage_background.cpp:337
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22