Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_norm.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "img_background.h"
11 #include "img_extract.h"
12 #include "img_rescale.h"
13 #include "img_histogram.h"
14 #include "utilities.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 int img_mass_normalize_subimage(Bimage* p, int imgnum, float avg, float std, int norm_type, int bins);
20 
42 int img_mass_normalize(Bimage* p, float avg, float std, int norm_type)
43 {
44  int bins = (int) p->maximum();
45  if ( bins < 256 ) bins = 256;
46  if ( bins > 1024 ) bins = 1024;
47 
48  if ( std <= 0 ) { // Defaults
49  switch ( p->data_type() ) {
50  case UCharacter: avg = 127; std = 40; break;
51  case SCharacter: avg = 0; std = 40; break;
52  case UShort: avg = USHRT_MAX/2; std = 256; break;
53  case Short: avg = 0; std = 256; break;
54  case UInteger: avg = 100000; std = 1000; break;
55  case Integer: avg = 0; std = 1000; break;
56  case ULong: avg = 100000; std = 1000; break;
57  case Long: avg = 0; std = 1000; break;
58  default: avg = 0; std = 1; break;
59  }
60  }
61 
62  p->change_type(Float);
63 
64  if ( verbose & VERB_PROCESS ) {
65  cout << "Mass normalizing ";
66  if ( p->file_name().length() ) cout << p->file_name().c_str() << endl;
67  else cout << endl;
68  cout << "Normalizing type: ";
69  switch ( norm_type ) {
70  case 1: cout << "Gaussian"; break;
71  case 2: cout << "Poisson"; break;
72  default: cout << "Simple";
73  }
74  cout << endl << "Average and standard deviation: " << avg << " " << std << endl;
75  }
76 
77 #ifdef HAVE_GCD
78  dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
79  dispatch_apply(p->images(), dispatch_get_global_queue(0, 0), ^(size_t n){
80  img_mass_normalize_subimage(p, n, avg, std, norm_type, bins);
81  dispatch_sync(myq, ^{
82  if ( verbose )
83  cout << "Image " << n << " normalized" << endl;
84  });
85  });
86 #else
87 #pragma omp parallel for
88  for ( int n=0; n<p->images(); n++ ) {
89  img_mass_normalize_subimage(p, n, avg, std, norm_type, bins);
90  #pragma omp critical
91  {
92  if ( verbose )
93  cout << "Image " << n << " normalized" << endl;
94  }
95  }
96 #endif
97 
99  p->statistics();
100 
101  return 0;
102 }
103 
104 int img_mass_normalize_subimage(Bimage* p, int imgnum, float avg, float std, int norm_type, int bins)
105 {
106  float* fit = NULL;
107 
108  Bimage* ptemp = p->extract(imgnum);
109 
110  img_truncate_to_min_max(ptemp, ptemp->average() - 10*ptemp->standard_deviation(), ptemp->average() + 10*ptemp->standard_deviation());
111 
112  switch ( norm_type ) {
113  case 1:
114  fit = img_fit_gauss_to_histogram(ptemp, bins, 1);
115  ptemp->average(fit[1]);
116  ptemp->standard_deviation(fit[2]);
117  delete[] fit;
118  break;
119  case 2:
120  fit = img_fit_poisson_to_histogram(ptemp, bins);
121  ptemp->average(fit[2]);
122  ptemp->standard_deviation(sqrt(fit[2]));
123  delete[] fit;
124  break;
125  default:
126  ptemp->statistics();
127  }
128 
129  img_rescale_to_avg_std(ptemp, avg, std);
130 
131 // if ( img_rescale_to_avg_std(ptemp, avg, std) )
132 // cerr << "Error: The extrema for image " << imgnum+1 << " are the same: " <<
133 // ptemp->minimum() << " " << ptemp->maximum() << endl;
134 
135  p->replace(imgnum, ptemp);
136 
137  delete ptemp;
138 
139  return 0;
140 }
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1409
unsigned long images()
Definition: Bimage.h:402
int img_rescale_to_avg_std(Bimage *p, double avg, double std)
Rescales the image data to a given average and standard deviation.
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2851
Definition: Bimage.h:54
Header file for image utilities.
Definition: Bimage.h:50
Header file for general utilities.
Header file for image utilities.
Definition: Bimage.h:56
int verbose
#define VERB_PROCESS
Definition: utilities.h:99
double average(unsigned long cc, double xf, double yf, double zf, unsigned long nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1119
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.cpp:2403
General image parameter class.
Definition: Bimage.h:149
double maximum()
Definition: Bimage.h:420
Header file for functions to extract parts of images.
void file_name(Bstring s)
Definition: Bimage.h:209
int img_calculate_background(Bimage *p, int n)
Calculates the background for one sub-image.
float * img_fit_poisson_to_histogram(Bimage *p, int bins)
double standard_deviation()
Definition: Bimage.h:422
Header file for manipulating image backgrounds.
Definition: Bimage.h:53
int img_mass_normalize_subimage(Bimage *p, int imgnum, float avg, float std, int norm_type, int bins)
Definition: img_norm.cpp:104
Definition: Bimage.h:51
float * img_fit_gauss_to_histogram(Bimage *p, int bins, int n)
int img_truncate_to_min_max(Bimage *p, double min, double max)
Truncates image data to a given minimum and maximum.
Header file for 2D and 3D image I/O.
DataType data_type()
Definition: Bimage.h:290
Definition: Bimage.h:55
Bimage * extract(unsigned long nn)
Extracts one sub-image into new image structure.
Definition: Bimage.cpp:2764
int img_mass_normalize(Bimage *p, float avg, float std, int norm_type)
Normalizes a set of images to a desired average and standard deviation.
Definition: img_norm.cpp:42