| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_background.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "img_background.h"
11 #include "Complex.h"
12 #include "Matrix.h"
13 #include "utilities.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
27 Bimage* img_background_mask(Bimage* p)
28 {
29 if ( !p->data_pointer() ) return NULL;
30
31 Bimage* pmask = new Bimage(UCharacter, TSimple, p->sizeX(), p->sizeY(), p->sizeZ(), p->images());
32
33 unsigned long i, n, x, y, z, nm;
34 double x2(0), y2(0), z2(0);
35 double hx = (p->sizeX() - 1)/2.0;
36 double hy = (p->sizeY() - 1)/2.0;
37 double hz = (p->sizeZ() - 1)/2.0;
38 double ox = p->sizeX()/2;
39 double oy = p->sizeY()/2;
40 double oz = p->sizeZ()/2;
41
42 if ( verbose & VERB_PROCESS )
43 cout << "Calculating a background mask" << endl << endl;
44
45 for ( i=n=0; n<p->images(); n++ ) {
46 nm = 0;
47 if ( p->image[n].origin().x() > 0 && p->image[n].origin().x() < p->sizeX() ) ox = p->image[n].origin().x();
48 if ( p->image[n].origin().y() > 0 && p->image[n].origin().y() < p->sizeY() ) oy = p->image[n].origin().y();
49 if ( p->image[n].origin().z() > 0 && p->image[n].origin().z() < p->sizeZ() ) oz = p->image[n].origin().z();
50 for ( z=0; z<p->sizeZ(); z++ ) {
51 if ( p->sizeZ() > 1 ) {
52 z2 = (z - oz)/hz;
53 z2 *= z2;
54 }
55 for ( y=0; y<p->sizeY(); y++ ) {
56 if ( p->sizeY() > 1 ) {
57 y2 = (y - oy)/hy;
58 y2 *= y2;
59 }
60 for ( x=0; x<p->sizeX(); x++, i++ ) {
61 if ( p->sizeX() > 1 ) {
62 x2 = (x - ox)/hx;
63 x2 *= x2;
64 }
65 if ( x2 + y2 + z2 > 1 ) {
66 pmask->set(i, 1);
67 nm++;
68 }
69 }
70 }
71 }
72 if ( verbose & VERB_DEBUG )
73 cout << "DEBUG img_background_mask: img=" << n << " nm=" << nm << endl;
74 }
75
76 return pmask;
77 }
78
89 int img_calculate_background(Bimage* p, int n)
90 {
91 if ( !p->data_pointer() ) return -1;
92
93 if ( p->data_type() == Bit ) return 0;
94
95 unsigned long i, j, x, y, z, c, nm(0);
96 double x2(0), y2(0), z2(0);
97 double background(0);
98 double hx = (p->sizeX() - 1)/2.0;
99 double hy = (p->sizeY() - 1)/2.0;
100 double hz = (p->sizeZ() - 1)/2.0;
101 double ox = p->sizeX()/2;
102 double oy = p->sizeY()/2;
103 double oz = p->sizeZ()/2;
104
105 if ( p->image[n].origin().x() > 0 && p->image[n].origin().x() < p->sizeX() ) ox = p->image[n].origin().x();
106 if ( p->image[n].origin().y() > 0 && p->image[n].origin().y() < p->sizeY() ) oy = p->image[n].origin().y();
107 if ( p->image[n].origin().z() > 0 && p->image[n].origin().z() < p->sizeZ() ) oz = p->image[n].origin().z();
108
109 i = (unsigned long) (n*p->size().volume());
110 for ( z=0; z<p->sizeZ(); z++ ) {
111 if ( p->sizeZ() > 1 ) {
112 z2 = (z - oz)/hz;
113 z2 *= z2;
114 }
115 for ( y=0; y<p->sizeY(); y++ ) {
116 if ( p->sizeY() > 1 ) {
117 y2 = (y - oy)/hy;
118 y2 *= y2;
119 }
120 for ( x=0; x<p->sizeX(); x++, i++ ) {
121 if ( p->sizeX() > 1 ) {
122 x2 = (x - ox)/hx;
123 x2 *= x2;
124 }
125 if ( x2 + y2 + z2 > 1 ) {
126 j = i*p->channels();
127 for ( c=0; c<p->channels(); c++, j++ ) {
128 background += (*p)[j];
129 nm++;
130 }
131 }
132 }
133 }
134 }
135
136 if ( nm ) p->image[n].background(background/nm);
137
138 if ( verbose & VERB_DEBUG )
139 cout << "DEBUG img_calculate_background: img=" << n << " nm=" << nm << endl;
140
141 return 0;
142 }
143
153 int img_calculate_background(Bimage* p)
154 {
155 if ( !p->data_pointer() ) return -1;
156
157 if ( p->data_type() == Bit ) return 0;
158
159 if ( verbose & VERB_PROCESS )
160 cout << "Calculating the background values of all images" << endl << endl;
161
162 #ifdef HAVE_GCD
163 dispatch_apply(p->images(), dispatch_get_global_queue(0, 0), ^(size_t n){
164 img_calculate_background(p, n);
165 });
166 #else
167 #pragma omp parallel for
168 for ( int n=0; n<p->images(); n++ )
169 img_calculate_background(p, n);
170 #endif
171
172 return 0;
173 }
174
188 int img_calculate_background(Bimage* p, Bimage* pmask, int n)
189 {
190 if ( !p->data_pointer() ) return -1;
191
192 if ( !pmask ) return -1;
193
194 if ( p->data_type() == Bit ) return 0;
195
196 unsigned long i, j, k, c, nm(0);
197 double background(0);
198
199 i = (unsigned long) (n*p->size().volume());
200 for ( k=0; k<p->size().volume(); k++, i++ ) if ( (*pmask)[i] < 0.5 ) {
201 j = i*p->channels();
202 for ( c=0; c<p->channels(); c++, j++ ) background += (*p)[j];
203 nm++;
204 }
205
206 if ( nm ) p->image[n].background(background/nm);
207
208 if ( verbose & VERB_DEBUG )
209 cout << "DEBUG img_calculate_background: img=" << n << " nm=" << nm << " bkg=" << p->image[n].background() << endl;
210
211 return 0;
212 }
213
214
227 int img_calculate_background(Bimage* p, Bimage* pmask)
228 {
229 if ( !p->data_pointer() ) return -1;
230
231 if ( !pmask ) return -1;
232
233 if ( p->data_type() == Bit ) return 0;
234
235 if ( verbose & VERB_PROCESS )
236 cout << "Calculating the background values of all images" << endl << endl;
237
238 #ifdef HAVE_GCD
239 dispatch_apply(p->images(), dispatch_get_global_queue(0, 0), ^(size_t n){
240 img_calculate_background(p, pmask, n);
241 });
242 #else
243 #pragma omp parallel for
244 for ( int n=0; n<p->images(); n++ )
245 img_calculate_background(p, pmask, n);
246 #endif
247
248 return 0;
249 }
250
261 int img_correct_background(Bimage* p, int n)
262 {
263 if ( !p->data_pointer() ) return -1;
264
265 if ( p->data_type() == Bit ) return 0;
266
267 img_calculate_background(p, n);
268
269 unsigned long i, j, x, y, z, c;
270 double x2(0), y2(0), z2(0);
271 double background(p->image[n].background());
272 double hx = (p->sizeX() - 1)/2.0;
273 double hy = (p->sizeY() - 1)/2.0;
274 double hz = (p->sizeZ() - 1)/2.0;
275 double ox = p->sizeX()/2;
276 double oy = p->sizeY()/2;
277 double oz = p->sizeZ()/2;
278
279 if ( p->image[n].origin().x() > 0 && p->image[n].origin().x() < p->sizeX() ) ox = p->image[n].origin().x();
280 if ( p->image[n].origin().y() > 0 && p->image[n].origin().y() < p->sizeY() ) oy = p->image[n].origin().y();
281 if ( p->image[n].origin().z() > 0 && p->image[n].origin().z() < p->sizeZ() ) oz = p->image[n].origin().z();
282
283 i = (unsigned long) (n*p->size().volume());
284 for ( z=0; z<p->sizeZ(); z++ ) {
285 if ( p->sizeZ() > 1 ) {
286 z2 = (z - oz)/hz;
287 z2 *= z2;
288 }
289 for ( y=0; y<p->sizeY(); y++ ) {
290 if ( p->sizeY() > 1 ) {
291 y2 = (y - oy)/hy;
292 y2 *= y2;
293 }
294 for ( x=0; x<p->sizeX(); x++, i++ ) {
295 if ( p->sizeX() > 1 ) {
296 x2 = (x - ox)/hx;
297 x2 *= x2;
298 }
299 if ( x2 + y2 + z2 > 1 ) {
300 j = i*p->channels();
301 for ( c=0; c<p->channels(); c++, j++ )
302 p->set(j, background);
303 }
304 }
305 }
306 }
307
308 if ( verbose & VERB_DEBUG )
309 cout << "DEBUG img_correct_background: img=" << n << " bkg=" << background << endl;
310
311 return 0;
312 }
313
314
324 int img_correct_background(Bimage* p)
325 {
326 if ( !p->data_pointer() ) return -1;
327
328 if ( p->data_type() == Bit ) return 0;
329
330 if ( verbose & VERB_PROCESS )
331 cout << "Correcting the background values of all images" << endl << endl;
332
333 #ifdef HAVE_GCD
334 dispatch_apply(p->images(), dispatch_get_global_queue(0, 0), ^(size_t n){
335 img_correct_background(p, n);
336 });
337 #else
338 #pragma omp parallel for
339 for ( int n=0; n<p->images(); n++ )
340 img_correct_background(p, n);
341 #endif
342
343 if ( p->statistics() )
344 cerr << tab << "in img_correct_background" << endl;
345
346 return 0;
347 }
348
362 int img_correct_background(Bimage* p, Bimage* pmask)
363 {
364 if ( !p->data_pointer() ) return -1;
365
366 if ( !pmask ) return -1;
367
368 if ( p->data_type() == Bit ) return 0;
369
370 img_calculate_background(p, pmask);
371
372 unsigned long i, j, k, n, c, nm;
373 double background;
374
375 if ( verbose & VERB_PROCESS )
376 cout << "Correcting the background values of all images" << endl << endl;
377
378 for ( i=n=0; n<p->images(); n++ ) {
379 background = p->image[n].background();
380 nm = 0;
381 for ( k=0; k<p->size().volume(); k++, i++ ) if ( (*pmask)[i] < 0.5 ) {
382 j = i*p->channels();
383 for ( c=0; c<p->channels(); c++, j++ ) p->set(j, background);
384 nm++;
385 }
386 }
387
388 if ( p->statistics() )
389 cerr << tab << "in img_correct_background" << endl;
390
391 return 0;
392 }
393
402 int img_subtract_background(Bimage* p)
403 {
404 return img_shift_background(p, 0);
405 }
406
415 int img_subtract_background2(Bimage* p)
416 {
417 if ( !p->data_pointer() ) return -1;
418
419 if ( p->data_type() == Bit ) return 0;
420
421 unsigned long imagesize = p->sizeX()*p->sizeY()*p->sizeZ();
422 if ( p->compound_type() != TComplex ) imagesize *= p->channels();
423
424 unsigned long i, j, n;
425 double background, v;
426 Complex<double> cv;
427
428 if ( verbose & VERB_PROCESS )
429 cout << "Subtracting the background values of all images" << endl << endl;
430
431 double min = p->data_type_min();
432 double max = p->data_type_max();
433
434 for ( n=i=0; n<p->images(); n++ ) {
435 background = p->image[n].background();
436 for ( j=0; j<imagesize; i++, j++ ) {
437 if ( p->compound_type() == TSimple ) {
438 v = (*p)[i];
439 v -= background;
440 if ( v < min ) v = min;
441 if ( v > max ) v = max;
442 p->set(i, v);
443 } else {
444 cv = p->complex(i);
445 cv -= background;
446 if ( cv.real() < min ) cv.real(min);
447 if ( cv.real() > max ) cv.real(max);
448 if ( cv.imag() < min ) cv.imag(min);
449 if ( cv.imag() > max ) cv.imag(max);
450 p->set(i, cv);
451 }
452 }
453 }
454
455 if ( p->statistics() )
456 cerr << tab << "in img_subtract_background2" << endl;
457
458 return 0;
459 }
460
471 int img_shift_background(Bimage* p, double background)
472 {
473 if ( !p->data_pointer() ) return -1;
474
475 if ( p->data_type() == Bit ) return 0;
476
477 unsigned long i, j, n, imagesize = p->sizeX()*p->sizeY()*p->sizeZ();
478 if ( p->compound_type() != TComplex ) imagesize *= p->channels();
479
480 double shift, v;
481 Complex<double> cv;
482
483 double min = p->data_type_min();
484 double max = p->data_type_max();
485
486 if ( verbose & VERB_PROCESS )
487 cout << "Shifting the background values of all images to " << background << endl;
488
489 for ( n=i=0; n<p->images(); n++ ) {
490 shift = background - p->image[n].background();
491 p->image[n].background(background);
492 for ( j=0; j<imagesize; i++, j++ ) {
493 if ( p->compound_type() != TComplex ) {
494 v = (*p)[i];
495 v += shift;
496 if ( v < min ) v = min;
497 if ( v > max ) v = max;
498 p->set(i, v);
499 } else {
500 cv = p->complex(i);
501 cv += shift;
502 if ( cv.real() < min ) cv.real(min);
503 if ( cv.real() > max ) cv.real(max);
504 if ( cv.imag() < min ) cv.imag(min);
505 if ( cv.imag() > max ) cv.imag(max);
506 p->set(i, cv);
507 }
508 }
509 }
510
511 if ( p->statistics() )
512 cerr << tab << "in img_shift_background" << endl;
513
514 return 0;
515 }
516
Bimage::images
unsigned long images()
Definition: Bimage.h:402
Bimage::complex
Complex< double > complex(unsigned long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:793
Bimage::sizeX
void sizeX(unsigned long nx)
Definition: Bimage.h:332
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:104
img_subtract_background2
int img_subtract_background2(Bimage *p)
Subtracts the background for each sub-image.
Definition: img_background.cpp:415
Bsub_image::background
double background()
Definition: Bimage.h:123
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:292
Complex.h
Class for complex numbers.
Bit
Definition: Bimage.h:47
img_subtract_background
int img_subtract_background(Bimage *p)
Subtracts the background for each sub-image.
Definition: img_background.cpp:402
Matrix.h
Generalized matrix class.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:99
TComplex
Definition: Bimage.h:68
img_shift_background
int img_shift_background(Bimage *p, double background)
Sets the background for each sub-image to a given value.
Definition: img_background.cpp:471
Bimage::statistics
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.cpp:2403
Bimage::channels
unsigned long channels()
Definition: Bimage.h:327
TSimple
Definition: Bimage.h:67
Bimage::data_type_max
double data_type_max()
Get the maximum of a datatype.
Definition: Bimage.cpp:1331
Bimage::size
Vector3< int > size()
Definition: Bimage.h:329
Bimage
General image parameter class.
Definition: Bimage.h:149
UCharacter
Definition: Bimage.h:48
img_background_mask
Bimage * img_background_mask(Bimage *p)
Calculates a background mask.
Definition: img_background.cpp:27
Bimage::set
void set(unsigned long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:885
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:129
img_correct_background
int img_correct_background(Bimage *p, int n)
Corrects the background for one sub-image.
Definition: img_background.cpp:261
Complex
Definition: Complex.h:23
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:225
img_background.h
Header file for manipulating image backgrounds.
verbose
int verbose
Complex::imag
Type imag()
Definition: Complex.h:102
Bimage::sizeZ
void sizeZ(unsigned long nz)
Definition: Bimage.h:334
img_calculate_background
int img_calculate_background(Bimage *p, int n)
Calculates the background for one sub-image.
Definition: img_background.cpp:89
rwimg.h
Header file for 2D and 3D image I/O.
Complex::real
Type real()
Definition: Complex.h:101
Bimage::sizeY
void sizeY(unsigned long ny)
Definition: Bimage.h:333
Bimage::data_type
DataType data_type()
Definition: Bimage.h:290
Vector3::volume
double volume()
Definition: Vector3.h:207
Bimage::data_type_min
double data_type_min()
Get the minimum of a datatype.
Definition: Bimage.cpp:1301
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::image
Bsub_image * image
Definition: Bimage.h:180