Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_filter.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "img_filter.h"
11 #include "img_histogram.h"
12 #include "Complex.h"
13 #include "Matrix.h"
14 #include "matrix_linear.h"
15 #include "math_util.h"
16 #include "utilities.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 
32 {
33  if ( value < 1e-10 ) value = 1;
34 
35  if ( verbose & VERB_PROCESS ) {
36  cout << "Generating a uniform kernel:" << endl;
37  cout << "Size: " << ksize << endl;
38  cout << "Value: " << value << endl << endl;
39  }
40 
41  Bimage* p = new Bimage(Float, TSimple, ksize.x(), ksize.y(), ksize.z(), 1);
42 
43  p->origin(ksize/2);
44 
45  p->fill(1);
46 
47  return p;
48 }
49 
57 Bimage* img_gaussian_kernel(Vector3<int> ksize, float sigma, float max)
58 {
59  if ( sigma < 0.001 ) sigma = 1;
60  if ( fabs(max) < 1e-10 ) max = 1;
61 
62  int side = 2*( (int) (4*sigma + 0.9) ) + 1;
63  if ( ksize.volume() < 3 ) ksize = Vector3<int>(side, side, side);
64 
65  Vector3<float> h((ksize.x()-1)/2.0, (ksize.y()-1)/2.0, (ksize.z()-1)/2.0);
66 
67  if ( verbose & VERB_PROCESS ) {
68  cout << "Generating a gaussian kernel:" << endl;
69  cout << "Size: " << ksize << endl;
70  cout << "Sigma: " << sigma << endl;
71  cout << "Maximum: " << max << endl << endl;
72  }
73 
74  Bimage* p = new Bimage(Float, TSimple, ksize.x(), ksize.y(), ksize.z(), 1);
75 
76  p->origin(h);
77 
78  int i, x, y, z;
79  double x2, y2, z2, s2;
80  double invsigma2 = -0.5/(sigma*sigma);
81 
82  for ( i=z=0; z<p->sizeZ(); z++ ) {
83  z2 = z - h.z();
84  z2 *= z2;
85  for ( y=0; y<p->sizeY(); y++ ) {
86  y2 = y - h.y();
87  y2 *= y2;
88  for ( x=0; x<p->sizeX(); x++, i++ ) {
89  x2 = x - h.x();
90  x2 *= x2;
91  s2 = x2 + y2 + z2;
92  p->set(i, max*exp(s2*invsigma2));
93  }
94  }
95  }
96 
97  return p;
98 }
99 
100 Bimage* img_gaussian_kernel(float sigma, float max)
101 {
102  if ( sigma < 0.001 ) sigma = 1;
103 
104  int side = 2*( (int) (4*sigma + 0.9) ) + 1;
105  Vector3<int> ksize(side, side, side);
106 
107  return img_gaussian_kernel(ksize, sigma, max);
108 }
109 
117 Bimage* img_laplacian_of_gaussian_kernel(Vector3<int> ksize, float sigma, float max)
118 {
119  if ( ksize.volume() <= 1 ) {
120  int side = 2*( (int) (4*sigma + 0.9) ) + 1;
121  ksize = Vector3<int>(side, side, side);
122  }
123 
124  if ( sigma < 0.001 ) sigma = 1;
125  if ( max < 1e-10 ) max = 1;
126 
127  if ( ksize.volume() < 1 ) {
128  cerr << "Error: Kernel size = 0!" << endl;
129  return NULL;
130  }
131 
132  if ( verbose & VERB_PROCESS ) {
133  cout << "Generating a laplacian-of-gaussian kernel:" << endl;
134  cout << "Size: " << ksize << endl;
135  cout << "Sigma: " << sigma << endl;
136  cout << "Maximum: " << max << endl << endl;
137  }
138 
139  Vector3<float> h((ksize.x()-1)/2.0, (ksize.y()-1)/2.0, (ksize.z()-1)/2.0);
140 
141  Bimage* p = new Bimage(Float, TSimple, ksize.x(), ksize.y(), ksize.z(), 1);
142 
143  p->origin(h);
144 
145  int i, x, y, z, ndim = 0;
146  if ( ksize.x() > 1 ) ndim++;
147  if ( ksize.y() > 1 ) ndim++;
148  if ( ksize.z() > 1 ) ndim++;
149  double x2, y2, z2, s2;
150  double invsigma2 = 1/(sigma*sigma);
151  double invsigma4 = invsigma2*invsigma2;
152  double scale = max/(ndim*invsigma2);
153 
154  for ( i=z=0; z<p->sizeZ(); z++ ) {
155  z2 = z - h.z();
156  z2 *= z2;
157  for ( y=0; y<p->sizeY(); y++ ) {
158  y2 = y - h.y();
159  y2 *= y2;
160  for ( x=0; x<p->sizeX(); x++, i++ ) {
161  x2 = x - h.x();
162  x2 *= x2;
163  s2 = x2 + y2 + z2;
164  p->set(i, scale*(s2*invsigma4 - ndim*invsigma2)*exp(-0.5*s2*invsigma2));
165  }
166  }
167  }
168 
169  return p;
170 }
171 
185 int img_filter_extremes(Bimage* p, int mod_flag)
186 {
187  if ( !p->data_pointer() ) {
188  cerr << "Error: No image data to process!" << endl << endl;
189  return -1;
190  }
191 
192  if ( p->maximum() <= p->minimum() ) p->statistics();
193 
194  // Calculate the histogram and remove large numbers of white and black pixels
195  int i, j, bins = 256;
196  if ( p->data_type() <= SCharacter ) bins = (int) (p->maximum() - p->minimum() + 1.1);
197 
198  int ihi, imin, nmin, imax = bins/2, nmax = 0, isig = 0;
199  double g, sigma = 8.0/(bins*bins);
200  double scale, offset;
201 
202  int* hist = p->histogram(bins, scale, offset);
203 
204  for ( i=bins/4, j=(3*bins)/4; i<j; i++ ) {
205  if ( nmax < hist[i] ) { // Find the maximum in the middle part of the histogram
206  nmax = hist[i];
207  imax = i;
208  }
209  }
210 
211  while ( imax+isig < bins - 1 && imax-isig > 0 &&
212  hist[imax] < 2*hist[imax+isig] && hist[imax] < 2*hist[imax-isig] )
213  isig++;
214 
215  if ( verbose & VERB_DEBUG ) {
216  cout << "DEBUG img_filter_extremes: Gaussian parameters:" << endl;
217  cout << "DEBUG img_filter_extremes: max=" << nmax << " imax=" << imax << " sigma=" << isig << endl;
218  }
219 
220  if ( isig > 0 ) sigma = -0.5/(isig*isig);
221  ihi = 0;
222  for ( i=0; i<bins/4; i++ ) { // Find the highest peak in the first quarter
223  g = nmax*exp(sigma*(imax - i)*(imax - i));
224  if ( hist[i] > 20*g ) ihi = i;
225  }
226 
227  if ( verbose & VERB_DEBUG )
228  cout << "DEBUG img_filter_extremes: highest peak in first quarter = " << ihi << endl;
229 
230  imin = ihi;
231  nmin = p->sizeX()*p->sizeY()*p->sizeZ();
232  for ( i=ihi; i<bins/4; i++ ) { // Find the minimum after the highest peak in the first quarter
233  if ( nmin > hist[i] ) {
234  nmin = hist[i];
235  imin = i;
236  }
237  }
238 
239  ihi = bins - 1;
240  for ( i=bins-1, j=(3*bins)/4; i>j; i-- ) { // Find the highest peak in the last quarter
241  g = nmax*exp(sigma*(imax - i)*(imax - i));
242  if ( hist[i] > 20*g ) ihi = i;
243  }
244 
245  if ( verbose & VERB_DEBUG )
246  cout << "DEBUG img_filter_extremes: highest peak in last quarter = " << ihi << endl;
247 
248  imax = ihi;
249  nmin = p->sizeX()*p->sizeY()*p->sizeZ();
250  for ( i=(bins*3)/4; i<ihi; i++ ) { // Find the minumum before the highest peak in the last quarter
251  if ( nmin >= hist[i] ) {
252  nmin = hist[i];
253  imax = i;
254  }
255  }
256 
257  delete[] hist;
258 
259  if ( imin >= imax ) return -1;
260 
261  float min = imin/scale + p->minimum();
262  float max = imax/scale + p->minimum();
263 
264  if ( verbose & VERB_DEBUG )
265  cout << "DEBUG img_filter_extremes: imin=" << imin << " imax=" << imax << " min=" << min << " max=" << max << endl;
266 
267  if ( mod_flag )
268  p->truncate_to_min_max(min, max);
269  else
270  p->truncate_to_avg(min, max);
271 
272  return 0;
273 }
274 
285 {
286  if ( !p->data_pointer() ) {
287  cerr << "Error: No image data to process!" << endl << endl;
288  return -1;
289  }
290 
291  p->statistics();
292 
293  float threshold = p->average() + 3*p->standard_deviation();
294  if ( threshold >= p->maximum() ) threshold = (p->maximum() - p->average())*0.75 + p->average();
295 
296 // img_blobs(p, threshold, 0, 100, p->average(), 0);
297  p->blobs(threshold, 0, 100, p->average(), 0);
298 
299  threshold = p->average() - 3*p->standard_deviation();
300  if ( threshold <= p->minimum() ) threshold = (p->minimum() - p->average())*0.75 + p->average();
301 
302 // img_blobs(p, threshold, 0, 100, p->average(), 0);
303  p->blobs(threshold, 0, 100, p->average(), 0);
304 
305  return 0;
306 }
307 
Type z()
Definition: Vector3.h:141
void sizeZ(size_t nz)
Definition: Bimage.h:330
Header file for image utilities.
Header file for general utilities.
#define VERB_DEBUG
Definition: utilities.h:104
int img_filter_mg_extremes(Bimage *p)
Filters the extremes out of a micrograph image.
Definition: img_filter.cpp:284
Class for complex numbers.
Definition: Bimage.h:60
Generalized matrix class.
#define VERB_PROCESS
Definition: utilities.h:99
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1147
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.h:71
General image parameter class.
Definition: Bimage.h:153
Bimage * img_gaussian_kernel(Vector3< int > ksize, float sigma, float max)
Generates a gaussian kernel image.
Definition: img_filter.cpp:57
double maximum()
Definition: Bimage.h:458
Header file for image utilities.
size_t blobs(double threshold, double min_size, double max_size, double setvalue, int sign)
Identifies contiguous regions above a given threshold (blobs) and eliminate those less than a minimum...
Type x()
Definition: Vector3.h:139
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:915
double standard_deviation()
Definition: Bimage.h:460
unsigned char * data_pointer()
Definition: Bimage.h:232
int verbose
void sizeX(size_t nx)
Definition: Bimage.h:328
Header file for matrix functions.
int img_filter_extremes(Bimage *p, int mod_flag)
Filters the extremes out of an image.
Definition: img_filter.cpp:185
void sizeY(size_t ny)
Definition: Bimage.h:329
int truncate_to_avg(double minim, double maxim)
Sets voxels in image data exceeding a given minimum and maximum to the average.
Bimage * img_laplacian_of_gaussian_kernel(Vector3< int > ksize, float sigma, float max)
Generates a laplacian-of-gaussian kernel.
Definition: img_filter.cpp:117
Header file for 2D and 3D image I/O.
Bimage * img_average_kernel(Vector3< int > ksize, float value)
Generates a kernel with a uniform non-zero value.
Definition: img_filter.cpp:31
void fill(double v)
Definition: Bimage.h:559
DataType data_type()
Definition: Bimage.h:303
double volume()
Definition: Vector3.h:207
int * histogram(size_t bins, double &scale, double &offset)
Calculates the histogram of an image.
Type y()
Definition: Vector3.h:140
double minimum()
Definition: Bimage.h:457
void origin(double ox, double oy, double oz)
Definition: Bimage.h:478
Header file for general utilities.