Bsoft  2.1.0
Bernard's software package
Bimage_filter.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "matrix_linear.h"
11 #include "math_util.h"
12 #include "utilities.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 long kmin(long i, long hk)
18 {
19  return (i < hk)? hk - i: 0;
20 }
21 
22 long kmax(long i, long km, long m)
23 {
24  long hk = km/2;
25  return (i + km < m + hk)? km: m + hk - i;
26 }
27 
34 int Bimage::kernel_gaussian(double sigma, double max)
35 {
36  if ( sigma < 0.001 ) sigma = 1;
37  if ( fabs(max) < 1e-10 ) max = 1;
38 
39  if ( verbose & VERB_PROCESS ) {
40  cout << "Generating a gaussian kernel:" << endl;
41  cout << "Size: " << size() << endl;
42  cout << "Sigma: " << sigma << endl;
43  cout << "Maximum: " << max << endl << endl;
44  }
45 
46  Vector3<double> h(size()/2);
47  origin(h);
48 
49  long i, xx, yy, zz;
50  double x2, y2, z2, s2;
51  double invsigma2(-0.5/(sigma*sigma));
52 
53  for ( i=zz=0; zz<z; zz++ ) {
54  z2 = zz - h[2];
55  z2 *= z2;
56  for ( yy=0; yy<y; yy++ ) {
57  y2 = yy - h[1];
58  y2 *= y2;
59  for ( xx=0; xx<x; xx++, i++ ) {
60  x2 = xx - h[0];
61  x2 *= x2;
62  s2 = x2 + y2 + z2;
63  set(i, max*exp(s2*invsigma2));
64  }
65  }
66  }
67 
68  return 0;
69 }
70 
77 int Bimage::kernel_laplacian_of_gaussian(double sigma, double max)
78 {
79  if ( sigma < 0.001 ) sigma = 1;
80  if ( max < 1e-10 ) max = 1;
81 
82  if ( verbose & VERB_PROCESS ) {
83  cout << "Generating a laplacian-of-gaussian kernel:" << endl;
84  cout << "Size: " << size() << endl;
85  cout << "Sigma: " << sigma << endl;
86  cout << "Maximum: " << max << endl << endl;
87  }
88 
89  Vector3<double> h(size()/2);
90  origin(h);
91 
92  long i, xx, yy, zz, ndim(0);
93  if ( x > 1 ) ndim++;
94  if ( y > 1 ) ndim++;
95  if ( z > 1 ) ndim++;
96  double x2, y2, z2, s2;
97  double invsigma2 = 1/(sigma*sigma);
98  double invsigma4 = invsigma2*invsigma2;
99  double scale = max/(ndim*invsigma2);
100 
101  for ( i=zz=0; zz<z; zz++ ) {
102  z2 = zz - h[2];
103  z2 *= z2;
104  for ( yy=0; yy<y; yy++ ) {
105  y2 = yy - h[1];
106  y2 *= y2;
107  for ( xx=0; xx<x; xx++, i++ ) {
108  x2 = xx - h[0];
109  x2 *= x2;
110  s2 = x2 + y2 + z2;
111  set(i, scale*(s2*invsigma4 - ndim*invsigma2)*exp(-0.5*s2*invsigma2));
112  }
113  }
114  }
115 
116  return 0;
117 }
118 
119 
120 int Bimage::average_line_sum(long nn, long i, long ik, long nk)
121 {
122  if ( size()[ik] < 2 ) return 0;
123 
124  long ii(i*x), j, j1, j2, jc, cc, inc(1);
125  long nl(size()[ik]), ll(nl*c), hk(nk/2), nv(0);
126  vector<double> s(c,0);
127 
128  if ( ik == 1 ) {
129  ii = (i%x) + (i/x) * x * y;
130  inc = x;
131  } else if ( ik == 2 ) {
132  ii = i;
133  inc = x * y;
134  }
135 
136  ii += nn * x * y * z;
137 
138  double step = inc*hk;
139  vector<double> v(ll);
140 
141 // cout << ii << tab << ll << tab << hk << tab << inc << tab << s[0] << endl;
142  // Initial sum
143  for ( j=ii, j1=0; j1<hk; j+=inc, j1++, nv++ )
144  for ( cc=0, jc=j*c; cc<c; cc++, jc++ )
145  s[cc] += (*this)[jc];
146 // cout << "---" << endl;
147  // Main sums
148  for ( j=ii, j1=0; j1<nl; j+=inc, j1++ ) {
149  if ( j1 < nl-hk ) { // Add the leading values
150  j2 = j + (long) step;
151  for ( cc=0, jc=j2*c; cc<c; cc++, jc++ )
152  s[cc] += (*this)[jc];
153  nv++;
154  }
155  for ( cc=0, jc=j1*c; cc<c; cc++, jc++ )
156  v[jc] = s[cc]/nv;
157  if ( j1 >= hk ) { // Subtract the trailing values
158  j2 = j - (long) step;
159  for ( cc=0, jc=j2*c; cc<c; cc++, jc++ )
160  s[cc] -= (*this)[jc];
161  nv--;
162  }
163  }
164 
165  for ( j=ii, j1=0; j1<ll; j+=inc )
166  for ( cc=0, jc=j*c; cc<c; cc++, jc++, j1++ )
167  set(jc, v[j1]);
168 
169  return 0;
170 }
171 
172 int Bimage::average_line_sums(long nn, long ik, long nk)
173 {
174  if ( size()[ik] < 2 ) return 0;
175 
176  long i, nls(1);
177 
178  // Count the number of lines
179  for ( i=0; i<3; i++ ) if ( i != ik ) nls *= size()[i];
180 
181  if ( verbose & VERB_PROCESS )
182  cout << tab << "Axis " << ik << " of size " << nls << endl;
183 
184  // Do each line
185 #ifdef HAVE_GCD
186  dispatch_apply(nls, dispatch_get_global_queue(0, 0), ^(size_t i){
187  average_line_sum(nn, i, ik, nk);
188  });
189 #else
190 #pragma omp parallel for
191  for ( i=0; i<nls; i++ )
192  average_line_sum(nn, i, ik, nk);
193 #endif
194 
195  return 0;
196 }
197 
207 int Bimage::filter_average(long kernel_size)
208 {
209  Vector3<long> ksize(kernel_size, kernel_size, kernel_size);
210  return filter_average(ksize);
211 }
212 
214 {
216 
217  if ( k[0]%2 == 0 ) k[0] += 1;
218  if ( k[1]%2 == 0 ) k[1] += 1;
219  if ( k[2]%2 == 0 ) k[2] += 1;
220 
221  k = k.min(size());
222 
223  if ( verbose & VERB_LABEL ) {
224  cout << "Applying an averaging filter:" << endl;
225  cout << "Kernel size: " << k << " (" << k.volume() << ")" << endl << endl;
226  }
227 
228  long i, nn;
229 
230  for ( nn=0; nn<n; nn++ ) {
231  if ( verbose & VERB_PROCESS )
232  cout << "Image " << nn << endl;
233  for ( i=0; i<3; i++ )
234  average_line_sums(nn, i, k[i]);
235  }
236 
237  if ( verbose & VERB_PROCESS ) cout << endl;
238 
239  statistics();
240 
241  return 0;
242 }
243 
244 int Bimage::gaussian_line_sum(long nn, long i, long ik, Bimage* pk)
245 {
246  if ( size()[ik] < 2 ) return 0;
247 
248  long ii(i*x), j, j1, j2, jm, jc, ji, cc, inc(1), nl(size()[ik]), ll(nl*c), hk(pk->x/2);
249  double w;
250 
251  if ( ik == 1 ) {
252  ii = (i%x) + (i/x) * x * y;
253  inc = x;
254  } else if ( ik == 2 ) {
255  ii = i;
256  inc = x * y;
257  }
258 
259  ii += nn * x * y * z;
260  jm = ii + nl*inc;
261 
262  vector<double> v(ll);
263 
264  // Main sums
265  for ( j1=0; j1<nl; j1++ ) {
266  j2 = ( j1 < hk )? hk - j1: 0;
267  j = ii;
268  if ( j2 < 1 ) j += (j1 - hk)*inc;
269  for ( cc=0, jc=j1*c; cc<c; cc++, jc++ )
270  v[jc] = 0;
271  for ( w=0; j2<pk->x && j<jm; j+=inc, j2++ ) {
272  for ( cc=0, jc=j1*c, ji=j*c; cc<c; cc++, jc++, ji++ )
273  v[jc] += (*pk)[j2] * (*this)[ji];
274  w += (*pk)[j2];
275  }
276  for ( cc=0, jc=j1*c; cc<c; cc++, jc++ )
277  v[jc] /= w;
278  }
279 
280  // Transfer back
281  for ( j=ii, j1=0; j1<ll; j+=inc )
282  for ( cc=0, jc=j*c; cc<c; cc++, jc++, j1++ )
283  set(jc, v[j1]);
284 
285  return 0;
286 }
287 
288 int Bimage::gaussian_line_sums(long nn, long ik, Bimage* pk)
289 {
290  if ( size()[ik] < 2 ) return 0;
291 
292  long i, nls(1);
293 
294  // Count the number of lines
295  for ( i=0; i<3; i++ ) if ( i != ik ) nls *= size()[i];
296 
297  if ( verbose & VERB_PROCESS )
298  cout << tab << "Axis " << ik << " of size " << nls << endl;
299 
300  // Do each line
301 #ifdef HAVE_GCD
302  dispatch_apply(nls, dispatch_get_global_queue(0, 0), ^(size_t i){
303  gaussian_line_sum(nn, i, ik, pk);
304  });
305 #else
306 #pragma omp parallel for
307  for ( i=0; i<nls; i++ )
308  gaussian_line_sum(nn, i, ik, pk);
309 #endif
310 
311  return 0;
312 }
313 
324 int Bimage::filter_gaussian(long kernel_size, double sigma)
325 {
327 
328  if ( kernel_size%2 == 0 ) kernel_size += 1;
329 
330  Bimage* pk = new Bimage(Float, TSimple, kernel_size, 1, 1, 1);
331  pk->kernel_gaussian(sigma, 1);
332 
333  if ( verbose & VERB_PROCESS ) {
334  cout << "Applying a gaussian filter:" << endl;
335  cout << "Kernel size: " << pk->x << endl;
336  cout << "Sigma: " << sigma << endl << endl;
337  }
338 
339  long i, nn;
340 
341  for ( nn=0; nn<n; nn++ ) {
342  if ( verbose & VERB_PROCESS )
343  cout << "Image " << nn << endl;
344  for ( i=0; i<3; i++ )
345  gaussian_line_sums(nn, i, pk);
346  }
347 
348  if ( verbose & VERB_PROCESS ) cout << endl;
349 
350  delete pk;
351 
352  statistics();
353 
354  return 0;
355 }
356 
366 {
367  long i, xx, yy, zz;
368  double r, w;
369  Vector3<double> d;
370 
371  if ( verbose )
372  cout << "Weighing with a sinc function" << endl << endl;
373 
374  for ( i=zz=0; zz<z; zz++ ) {
375  d[2] = (-image->origin()[2] + zz)/z;
376  for ( yy=0; yy<y; yy++ ) {
377  d[1] = (-image->origin()[1] + yy)/y;
378  for ( xx=0; xx<x; xx++, i++ ) {
379  d[0] = (-image->origin()[0] + xx)/x;
380  r = M_PI*d.length();
381  if ( r ) {
382  w = r/sin(r);
383  set(i, w * w * (*this)[i]);
384  }
385  }
386  }
387  }
388 
389  return 0;
390 }
391 
392 int Bimage::convolve_chunk(Bimage* pkernel, float* nudata, long i, long len)
393 {
394  long ic, j, jy, jz, jn, m, nn, xx, yy, zz, cc, kx, ky, kz, ix, iy, iz;
395  Vector3<long> hk(pkernel->image->origin());
396  long xmin, ymin, zmin, xmax, ymax, zmax;
397  double v, w;
398 
399  coordinates(i, cc, xx, yy, zz, nn);
400 
401  if ( cc != 0 )
402  cerr << "Warning: chunk not on channel boundary! (" << i << ")" << endl;
403 
404  if ( verbose & VERB_DEBUG )
405  cout << "DEBUG Bimage::convolve_chunk: " << i << tab << cc << tab << xx
406  << tab << yy << tab << zz << tab << nn << endl;
407 
408  xmin = kmin(xx, hk[0]);
409  xmax = kmax(xx, pkernel->x, x);
410  ymin = kmin(yy, hk[1]);
411  ymax = kmax(yy, pkernel->y, y);
412  zmin = kmin(zz, hk[2]);
413  zmax = kmax(zz, pkernel->z, z);
414 
415  m = i+len;
416  if ( m > datasize ) m = datasize;
417 
418  for ( ; i<m; xx++, i+=c ) {
419  if ( xx >= x ) {
420  xx = 0;
421  yy++;
422  if ( yy >= y ) {
423  yy = 0;
424  zz++;
425  if ( zz >= z ) {
426  zz = 0;
427  nn++;
428  }
429  zmin = kmin(zz, hk[2]);
430  zmax = kmax(zz, pkernel->z, z);
431  }
432  ymin = kmin(yy, hk[1]);
433  ymax = kmax(yy, pkernel->y, y);
434  }
435  xmin = kmin(xx, hk[0]);
436  xmax = kmax(xx, pkernel->x, x);
437  for ( cc=0, ic=i; cc<c; cc++, ic++ ) nudata[ic] = 0;
438  w = 0;
439  jn = nn*z;
440  for ( kz=zmin; kz<zmax; kz++ ) {
441  jz = (jn + zz + kz - hk[2])*y;
442  iz = kz*pkernel->y;
443  for ( ky=ymin; ky<ymax; ky++ ) {
444  jy = (jz + yy + ky - hk[1])*x;
445  iy = (iz + ky)*pkernel->x;
446  for ( kx=xmin; kx<xmax; kx++ ) {
447  j = (jy + xx + kx - hk[0])*c;
448  ix = iy + kx;
449  v = (*pkernel)[ix];
450  for ( cc=0, ic=i; cc<c; cc++, ic++, j++ )
451  nudata[ic] += (*this)[j]*v;
452  w += v;
453  }
454  }
455  }
456  if ( w ) for ( cc=0, ic=i; cc<c; cc++, ic++ )
457  nudata[ic] /= w;
458  }
459 
460  return 0;
461 }
462 
474 {
475  if ( !pkernel || !pkernel->data_pointer() ) {
476  cerr << "Error in Bimage::convolve: No kernel specified!" << endl << endl;
477  return -1;
478  }
479 
480  if ( pkernel->sizeX() > x || pkernel->sizeY() > y || pkernel->sizeZ() > z ) {
481  error_show("Error in Bimage::convolve", __FILE__, __LINE__);
482  cerr << "Error: Kernel size does not fit the image size:" << endl;
483  cerr << " Kernel size = " << pkernel->size() << endl;
484  cerr << " Image size = " << size() << endl;
485  return -1;
486  }
487 
489  pkernel->change_type(Float);
490 
491  if ( verbose & VERB_PROCESS ) {
492  cout << "Convolving with a kernel: " << pkernel->file_name() << endl;
493  cout << "Kernel size: " << pkernel->size() << endl << endl;
494  }
495 
496  long i, xx, yy, chunk_size(get_chunk_size(datasize, c));
497 
498  float* nudata = new float[datasize];
499 
500 // if ( verbose & VERB_FULL ) {
501  cout << "Kernel:" << endl;
502  for ( i=yy=0; yy<pkernel->sizeY()*pkernel->sizeZ(); yy++ ) {
503  for ( xx=0; xx<pkernel->sizeX(); xx++, i++ )
504  cout << tab << (*pkernel)[i];
505  cout << endl;
506  }
507  cout << endl;
508 // }
509 
510 #ifdef HAVE_GCD
511  dispatch_apply((datasize - 1)/chunk_size + 1, dispatch_get_global_queue(0, 0), ^(size_t i){
512  convolve_chunk(pkernel, nudata, i*chunk_size, chunk_size);
513  });
514 #else
515 #pragma omp parallel for
516  for ( long i=0; i<datasize; i+=chunk_size )
517  convolve_chunk(pkernel, nudata, i, chunk_size);
518 #endif
519 
520  data_assign((unsigned char *) nudata);
521 
522  statistics();
523 
524  return 0;
525 }
526 
544 int Bimage::filter_ortho(int type)
545 {
547 
548  long i, xx, yy, zz, nn;
549  double value;
550  long slicesize(x*y*c);
551  float* nudata = new float[datasize];
552  long ksize(1);
553  if ( x > 1 ) ksize += 2;
554  if ( y > 1 ) ksize += 2;
555  if ( z > 1 ) ksize += 2;
556  vector<double> kernel(ksize);
557 
558  if ( type == 0 ) {
559  kernel[0] = 0;
560  for ( i=1; i<7; i+=2 )
561  if ( ksize > i ) {
562  kernel[i] = -1;
563  kernel[i+1] = 1;
564  }
565  } else if ( type == 1 ) {
566  kernel[0] = 0;
567  for ( i=1; i<7; i+=2 )
568  if ( ksize > i ) {
569  kernel[0] -= 2;
570  kernel[i] = 1;
571  kernel[i+1] = 1;
572  }
573  }
574 
575  if ( verbose & VERB_PROCESS ) {
576  cout << "Convolving with an orthogonal kernel:" << endl;
577  cout << "Kernel type: ";
578  switch ( type ) {
579  case 0: cout << "gradient"; break;
580  case 1: cout << "laplacian"; break;
581  default: break;
582  }
583  cout << endl << endl;
584  }
585 
586  for ( i=nn=0; nn<n; nn++ ) {
587  for ( zz=0; zz<z; zz++ ) {
588  for ( yy=0; yy<y; yy++ ) {
589  for ( xx=0; xx<x; xx++, i++ ) {
590  value = kernel[0]*(*this)[i];
591  if ( x > 1 ) {
592  if ( xx > 0 ) value += kernel[1]*(*this)[i-1];
593  else value += kernel[1]*(*this)[i+x-1];
594  if ( xx < x - 1 ) value += kernel[2]*(*this)[i+1];
595  else value += kernel[2]*(*this)[i+1-x];
596  if ( type == 0 ) {
597  nudata[i] = value*value;
598  value = 0;
599  }
600  }
601  if ( y > 1 ) {
602  if ( yy > 0 ) value += kernel[3]*(*this)[i-x];
603  else value += kernel[3]*(*this)[i+slicesize-x];
604  if ( yy < y - 1 ) value += kernel[4]*(*this)[i+x];
605  else value += kernel[4]*(*this)[i+x-slicesize];
606  if ( type == 0 ) {
607  nudata[i] += value*value;
608  value = 0;
609  }
610  }
611  if ( z > 1 ) {
612  if ( zz > 0 ) value += kernel[5]*(*this)[i-slicesize];
613  else value += kernel[5]*(*this)[i+datasize-slicesize];
614  if ( zz < z - 1 ) value += kernel[6]*(*this)[i+slicesize];
615  else value += kernel[6]*(*this)[i+slicesize-datasize];
616  if ( type == 0 ) {
617  nudata[i] += value*value;
618  value = 0;
619  }
620  }
621  if ( type == 1 ) nudata[i] = value;
622  else nudata[i] = sqrt(nudata[i]);
623  }
624  }
625  }
626  }
627 
628  data_assign((unsigned char *) nudata);
629 
630  statistics();
631 
632  return 0;
633 }
634 
641 int Bimage::filter_dog(double sigma1, double sigma2)
642 {
643  if ( sigma1 > sigma2 ) swap(sigma1, sigma2);
644 
645  long k(long(6*sigma2+1));
646  Vector3<long> ksize(k, k, k);
647  ksize = ksize.min(size());
648 
649  double m1(1/(sqrt(TWOPI)*sigma1));
650  double m2(1/(sqrt(TWOPI)*sigma2));
651 
652  Bimage* pk = new Bimage(Float, TSimple, ksize, 1);
653  Bimage* pk2 = new Bimage(Float, TSimple, ksize, 1);
654 
655  pk->kernel_gaussian(sigma1, m1);
656  pk2->kernel_gaussian(sigma2, m2);
657  pk->subtract(pk2);
658 
659  delete pk2;
660 
661  convolve(pk);
662 
663  delete pk;
664 
665  statistics();
666 
667  return 0;
668 }
669 
670 int Bimage::filter_bilateral_chunk(Bimage* pkernel, double sigma2,
671  int kernel_type, float* nudata, long i, long len, int first)
672 {
673  long nn, xx, yy, zz, cc;
674  long ic, j, jy, jz, jn, m, kx, ky, kz, ix, iy, iz, cnt(0);
675  Vector3<long> hk(pkernel->image->origin());
676  long xmin, ymin, zmin, xmax, ymax, zmax;
677 
678  double diff2, f, ref, acc, acc_num, acc_denom;
679  double gauss_neginv2sig2(0), lorentz_invsig2(0), tukey_invsig2(0);
680  if ( kernel_type == 1) gauss_neginv2sig2 = -0.5/(sigma2*sigma2);
681  else if ( kernel_type == 2) lorentz_invsig2 = 1./(sigma2*sigma2);
682  else if ( kernel_type == 3) tukey_invsig2 = 0.2/(sigma2*sigma2);
683 
684  coordinates(i, cc, xx, yy, zz, nn);
685 
686  if ( cc != 0 )
687  cerr << "Warning: chunk not on channel boundary! (" << i << ")" << endl;
688 
689  if ( verbose & VERB_DEBUG )
690  cout << "DEBUG Bimage::filter_bilateral_chunk: " << i << tab << cc << tab << xx
691  << tab << yy << tab << zz << tab << nn << endl;
692 
693  xmin = kmin(xx, hk[0]);
694  xmax = kmax(xx, pkernel->x, x);
695  ymin = kmin(yy, hk[1]);
696  ymax = kmax(yy, pkernel->y, y);
697  zmin = kmin(zz, hk[2]);
698  zmax = kmax(zz, pkernel->z, z);
699 
700  m = i+len;
701  if ( m > datasize ) m = datasize;
702 
703  for ( ; i<m; xx++, i+=c ) {
704  if ( xx >= x ) {
705  xx = 0;
706  yy++;
707  if ( yy >= y ) {
708  yy = 0;
709  zz++;
710  if ( zz >= z ) {
711  zz = 0;
712  nn++;
713  }
714  zmin = kmin(zz, hk[2]);
715  zmax = kmax(zz, pkernel->z, z);
716  }
717  ymin = kmin(yy, hk[1]);
718  ymax = kmax(yy, pkernel->y, y);
719  }
720  xmin = kmin(xx, hk[0]);
721  xmax = kmax(xx, pkernel->x, x);
722  for ( cc=0, ic=i; cc<c; cc++, ic++ ) nudata[ic] = 0;
723  ref = (*this)[i];
724  acc_num = acc_denom = 0.0;
725  jn = nn*z;
726  for ( kz=zmin; kz<zmax; kz++ ) {
727  jz = (jn + zz + kz - hk[2])*y;
728  iz = kz*pkernel->y;
729  for ( ky=ymin; ky<ymax; ky++ ) {
730  jy = (jz + yy + ky - hk[1])*x;
731  iy = (iz + ky)*pkernel->x;
732  for ( kx=xmin; kx<xmax; kx++ ) {
733  j = (jy + xx + kx - hk[0])*c;
734  ix = iy + kx;
735  diff2 = ref - (*this)[j];
736  diff2 *= diff2;
737  acc = 0;
738  switch (kernel_type) {
739  case 1: // gaussian
740  acc = exp(gauss_neginv2sig2*diff2);
741  break;
742  case 2: // lorentz
743  acc = 1/(1+lorentz_invsig2*diff2);
744  break;
745  case 3: // tukey
746  if (diff2 <= tukey_invsig2) {
747  f = 1 - tukey_invsig2*diff2;
748  acc = 0.5*f*f;
749  };
750  break;
751  }
752  acc *= (*pkernel)[ix];
753  acc_num += acc*(*this)[j];
754  acc_denom += acc;
755  }
756  }
757  }
758  for ( cc=0, ic=i; cc<c; cc++, ic++ )
759  if (acc_denom > 0) nudata[ic] = acc_num/acc_denom;
760  if ( first && ( verbose & ( VERB_TIME | VERB_PROCESS | VERB_RESULT ) ) )
761  cerr << "Complete: " << setprecision(3)
762  << (++cnt)*100.0/len << " % \r" << flush;
763  }
764 
765  return 0;
766 }
767 
783 int Bimage::filter_bilateral(double sigma1, double sigma2,
784  int kernel_type, long kernel_radius)
785 {
786  // Initialize parameters
787  if ( sigma1 <= 0 ) sigma1 = 1;
788  if ( kernel_radius <= 0 ) kernel_radius = (long) (sigma1*3);
789  if ( sigma2 <= 0 ) sigma2 = std;
790 
791  Vector3<long> ksize(kernel_radius*2+1, kernel_radius*2+1, kernel_radius*2+1);
792  ksize = ksize.min(size());
793 
794  Bimage* pkernel = new Bimage(Double, TSimple, ksize, 1);
795  pkernel->kernel_gaussian(sigma1, 1);
796 
798 
799  if ( verbose & VERB_LABEL ) {
800  cout << "Denoising with a bilateral kernel:" << endl;
801  cout << "Sigma values: " << sigma1 << " " << sigma2 << endl;
802  cout << "Kernel type: " << kernel_type << endl;
803  cout << "Kernel size: " << ksize << endl << endl;
804  }
805 
806  long chunk_size(get_chunk_size(datasize, c));
807 
808  float* nudata = new float[datasize];
809 
810 #ifdef HAVE_GCD
811  dispatch_apply((datasize - 1)/chunk_size + 1, dispatch_get_global_queue(0, 0), ^(size_t i){
812  filter_bilateral_chunk(pkernel, sigma2, kernel_type, nudata, i*chunk_size, chunk_size, i==0);
813  });
814 #else
815 #pragma omp parallel for
816  for ( long i=0; i<datasize; i+=chunk_size )
817  filter_bilateral_chunk(pkernel, sigma2, kernel_type, nudata, i, chunk_size, i==0);
818 #endif
819 
820  delete pkernel;
821 
822  data_assign((unsigned char *) nudata);
823 
824  statistics();
825 
826  return 0;
827 }
828 
835 int Bimage::filter_rolling_ball(long radius, double scale)
836 {
837  if ( radius < 1 ) radius = 1;
838  if ( fabs(scale) < 1e-30 ) scale = 1;
839 
840  long i, xx, yy, zz, nn, kx, ky, kz;
841  long ix, iy, iz, jx, jy, jz;
842  double r2(radius*radius);
843  double dx2, dy2, dz2, diff, bcenter;
844 
845  Vector3<long> ksize(radius*2+1, radius*2+1, radius*2+1);
846  ksize = ksize.min(size());
847  Vector3<long> hk(ksize/2);
848  Vector3<long> lo, hi;
849 
850  long ball_size = (long) ksize.volume();
851  vector<double> ball(ball_size);
852 
853  for ( i=zz=0; zz<ksize[2]; zz++ ) {
854  dz2 = (double)zz - hk[2];
855  dz2 *= dz2;
856  for ( yy=0; yy<ksize[1]; yy++ ) {
857  dy2 = (double)yy - hk[1];
858  dy2 *= dy2;
859  for ( xx=0; xx<ksize[0]; xx++, i++ ) {
860  dx2 = (double)xx - hk[0];
861  dx2 *= dx2;
862  diff = r2 - dx2 - dy2 - dz2;
863  if ( diff > 0 ) ball[i] = sqrt(diff)/scale;
864  else ball[i] = 0;
865  }
866  }
867  }
868 
870 
871  if ( verbose & VERB_PROCESS ) {
872  cout << "Rolling ball filter:" << endl;
873  cout << "Radius: " << radius << endl;
874  cout << "Scaling: " << scale << endl << endl;
875  }
876 
877  vector<float> back(datasize);
878  for ( i=0; i<datasize; i++ ) back[i] = (*this)[i];
879 
880  for ( i=nn=0; nn<n; nn++ ) {
881  for ( zz=0; zz<z; zz++ ) {
882  lo[2] = kmin(zz, hk[2]);
883  hi[2] = kmax(zz, ksize[2], z);
884  for ( yy=0; yy<y; yy++ ) {
885  lo[1] = kmin(yy, hk[1]);
886  hi[1] = kmax(yy, ksize[1], y);
887  for ( xx=0; xx<x; xx++, i++ ) {
888  lo[0] = kmin(xx, hk[0]);
889  hi[0] = kmax(xx, ksize[0], x);
890  if ( scale > 0 ) bcenter = max;
891  else bcenter = min;
892  for ( kz=lo[2]; kz<hi[2]; kz++ ) {
893  iz = (nn*z + zz + kz - hk[2])*y;
894  jz = kz*ksize[1];
895  for ( ky=lo[1]; ky<hi[1]; ky++ ) {
896  iy = (iz + yy + ky - hk[1])*x;
897  jy = (jz + ky)*ksize[0];
898  for ( kx=lo[0]; kx<hi[0]; kx++ ) {
899  ix = iy + xx + kx - hk[0];
900  jx = jy + kx;
901  diff = (*this)[ix] - (bcenter + ball[jx]);
902  if ( ball[jx] > 0 ) {
903  if ( diff < 0 ) bcenter += diff; // Moving the ball down
904  } else if ( ball[jx] < 0 ) {
905  if ( diff > 0 ) bcenter += diff; // Moving the ball up
906  }
907  }
908  }
909  }
910  for ( kz=lo[2]; kz<hi[2]; kz++ ) {
911  iz = (nn*z + zz + kz - hk[2])*y;
912  jz = kz*ksize[1];
913  for ( ky=lo[1]; ky<hi[1]; ky++ ) {
914  iy = (iz + yy + ky - hk[1])*x;
915  jy = (jz + ky)*ksize[0];
916  for ( kx=lo[0]; kx<hi[0]; kx++ ) {
917  ix = iy + xx + kx - hk[0];
918  jx = jy + kx;
919  diff = bcenter + ball[jx];
920  if ( ball[jx] > 0 ) {
921  if ( back[ix] > diff ) back[ix] = diff; // Assigning the background
922  } else if ( ball[jx] < 0 ) {
923  if ( back[ix] < diff ) back[ix] = diff; // Assigning the background
924  }
925  }
926  }
927  }
928  }
929  }
930  }
931  }
932 
933  for ( i=0; i<datasize; i++ ) set(i, (*this)[i] - back[i]);
934 
935  statistics();
936 
937  return 0;
938 }
939 
940 int Bimage::filter_rank_chunk(long kernel_size, double rank, float* nudata, long i, long len)
941 {
942  Vector3<long> blocksize(kernel_size, kernel_size, kernel_size);
943  blocksize = blocksize.min(size());
944  Vector3<long> hk(blocksize/2);
945  long blocktotalsize = (long) blocksize.volume();
946  vector<double> block(blocktotalsize);
947 
948  long nn, xx, yy, zz, cc;
949  long j, jy, jz, jn, m, kx, ky, kz;
950  long xmin, ymin, zmin, xmax, ymax, zmax;
951  long nblock, imed;
952 
953  coordinates(i, cc, xx, yy, zz, nn);
954 
955  if ( cc != 0 )
956  cerr << "Warning: chunk not on channel boundary! (" << i << ")" << endl;
957 
958  if ( verbose & VERB_DEBUG )
959  cout << "DEBUG Bimage::filter_rank_chunk: " << i << tab << cc << tab << xx
960  << tab << yy << tab << zz << tab << nn << endl;
961 
962  xmin = kmin(xx, hk[0]);
963  xmax = kmax(xx, blocksize[0], x);
964  ymin = kmin(yy, hk[1]);
965  ymax = kmax(yy, blocksize[1], y);
966  zmin = kmin(zz, hk[2]);
967  zmax = kmax(zz, blocksize[2], z);
968 
969  m = i+len;
970  if ( m > datasize ) m = datasize;
971 
972  for ( ; i<m; xx++, i+=c ) {
973  if ( xx >= x ) {
974  xx = 0;
975  yy++;
976  if ( yy >= y ) {
977  yy = 0;
978  zz++;
979  if ( zz >= z ) {
980  zz = 0;
981  nn++;
982  }
983  zmin = kmin(zz, hk[2]);
984  zmax = kmax(zz, blocksize[2], z);
985  }
986  ymin = kmin(yy, hk[1]);
987  ymax = kmax(yy, blocksize[1], y);
988  }
989  xmin = kmin(xx, hk[0]);
990  xmax = kmax(xx, blocksize[0], x);
991  nblock = 0;
992  jn = nn*z;
993  for ( kz=zmin; kz<zmax; kz++ ) {
994  jz = (jn + zz + kz - hk[2])*y;
995  for ( ky=ymin; ky<ymax; ky++ ) {
996  jy = (jz + yy + ky - hk[1])*x;
997  for ( kx=xmin; kx<xmax; kx++ ) {
998  j = (jy + xx + kx - hk[0])*c;
999  block[nblock++] = (*this)[j];
1000  }
1001  }
1002  }
1003  imed = nblock*rank;
1004  partition(block, nblock, imed);
1005  nudata[i] = block[imed];
1006  }
1007 
1008  return 0;
1009 }
1010 
1021 int Bimage::filter_rank(long kernel_size, double rank)
1022 {
1023  if ( compoundtype != TSimple ) {
1024  cerr << "Error: The rank filter only operates on single value images!" << endl << endl;
1025  return -1;
1026  }
1027 
1028  if ( kernel_size%2 == 0 ) kernel_size += 1;
1029 
1030 // long i, j, jz, jy, nn, xx, yy, zz, kx, ky, kz;
1031  Vector3<long> blocksize(kernel_size, kernel_size, kernel_size);
1032  blocksize = blocksize.min(size());
1033  long blocktotalsize = (long) blocksize.volume();
1034 
1035  if ( verbose & VERB_PROCESS ) {
1036  cout << "Applying a rank filter:" << endl;
1037  cout << "Rank: " << (long)(rank*blocktotalsize) << endl;
1038  cout << "Kernel size: " << blocksize << " (" << blocksize.volume() << ")" << endl << endl;
1039  }
1040 
1041  long i, chunk_size(get_chunk_size(datasize, c));
1042  float* nudata = new float[datasize];
1043 
1044 #ifdef HAVE_GCD
1045  dispatch_apply((datasize - 1)/chunk_size + 1, dispatch_get_global_queue(0, 0), ^(size_t i){
1046  filter_rank_chunk(kernel_size, rank, nudata, i*chunk_size, chunk_size);
1047  });
1048 #else
1049 #pragma omp parallel for
1050  for ( long i=0; i<datasize; i+=chunk_size )
1051  filter_rank_chunk(kernel_size, rank, nudata, i, chunk_size);
1052 #endif
1053 
1054  for ( i=0; i<datasize; i++ ) set(i, nudata[i]);
1055 
1056  if ( verbose & VERB_DEBUG )
1057  cout << "DEBUG Bimage::filter_rank: Done" << endl;
1058 
1059  delete[] nudata;
1060 
1061  statistics();
1062 
1063  return 0;
1064 }
1065 
1075 Bimage* Bimage::filter_peak(long kernel_size)
1076 {
1077  if ( compoundtype != TSimple ) {
1078  cerr << "Error: The average filter only operates on single value images!" << endl << endl;
1079  return NULL;
1080  }
1081 
1082  if ( kernel_size%2 == 0 ) kernel_size += 1;
1083  if ( kernel_size < 3 ) kernel_size = 3;
1084 
1085  long i, j, nn, xx, yy, zz, kx, ky, kz;
1086  long zlo, zhi, ylo, yhi, xlo, xhi;
1087  Vector3<long> blocksize(kernel_size, kernel_size, kernel_size);
1088  blocksize = blocksize.min(size());
1089  Vector3<long> h(blocksize/2);
1090  long blocktotalsize = (long) blocksize.volume();
1091  double bkg, v, v2;
1092 
1093  Bimage* ppeak = copy();
1094 
1095  if ( verbose & VERB_PROCESS ) {
1096  cout << "Applying a peak filter:" << endl;
1097  cout << "Block size: " << blocksize << " (" << blocktotalsize << ")" << endl << endl;
1098  }
1099 
1100  // Do the peak filtering
1101  for ( i=nn=0; nn<n; nn++ ) {
1102  bkg = background(nn);
1103  for ( zz=0; zz<z; zz++ ) {
1104  zlo = (zz > h[2])? zz - h[2]: 0;
1105  zhi = zz + h[2];
1106  if ( zhi >= z ) zhi = z - 1;
1107  for ( yy=0; yy<y; yy++ ) {
1108  ylo = (yy > h[1])? yy - h[1]: 0;
1109  yhi = yy + h[1];
1110  if ( yhi >= y ) yhi = y - 1;
1111  for ( xx=0; xx<x; xx++, i++ ) {
1112  xlo = (xx > h[0])? xx - h[0]: 0;
1113  xhi = xx + h[0];
1114  if ( xhi >= x ) xhi = x - 1;
1115  v = (*this)[i];
1116  for ( kz=zlo; kz<=zhi; kz++ ) {
1117  for ( ky=ylo; ky<=yhi; ky++ ) {
1118  for ( kx=xlo; kx<=xhi; kx++ ) {
1119  j = index(kx, ky, kz, nn);
1120  v2 = (*this)[j];
1121  if ( v < v2 ) v = bkg;
1122  }
1123  }
1124  }
1125  ppeak->set(i, v);
1126  }
1127  }
1128  }
1129  }
1130 
1131  if ( verbose & VERB_DEBUG )
1132  cout << "DEBUG Bimage::filter_peak: Done" << endl;
1133 
1134  ppeak->statistics();
1135 
1136  return ppeak;
1137 }
1138 
1145 {
1146  change_type(Float);
1147 
1148  period = period.min(size());
1149 
1150  Bimage* pnu = copy_header();
1151  pnu->size(period);
1152  pnu->page_size(period);
1153  pnu->data_alloc_and_clear();
1154  vector<float> w(pnu->image_size());
1155 
1156  long i, j, k, xo, yo, zo, nn, xx, yy, zz, cc;
1157 
1158  if ( verbose ) {
1159  cout << "Calculating a periodic average:" << endl;
1160  cout << "Period: " << period << endl << endl;
1161  }
1162 
1163  for ( i=nn=0; nn<n; nn++ ) {
1164  for ( k=0; k<pnu->image_size(); k++ ) w[k] = 0;
1165  for ( zo=zz=0; zo<z; zo++, zz++ ) {
1166  if ( zz >= pnu->sizeZ() ) zz = 0;
1167  for ( yo=yy=0; yo<y; yo++, yy++ ) {
1168  if ( yy >= pnu->sizeY() ) yy = 0;
1169  for ( xo=xx=0; xo<x; xo++, xx++ ) {
1170  if ( xx >= pnu->sizeX() ) xx = 0;
1171  k = pnu->index(xx, yy, zz);
1172  w[k] += 1;
1173  j = pnu->index(0, xx, yy, zz, nn);
1174  for ( cc=0; cc<c; cc++, j++, i++ )
1175  pnu->add(j, (*this)[i]);
1176  }
1177  }
1178  }
1179  for ( k=0, j=nn*c*pnu->image_size(); k<pnu->image_size(); k++ )
1180  if ( w[k] ) for ( cc=0; cc<c; cc++, j++ )
1181  pnu->set(j, (*pnu)[j]/w[k]);
1182  }
1183 
1184  pnu->origin(Vector3<long>(period/2));
1185 
1186  return pnu;
1187 }
1188 
1189 
1190 
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
verbose
int verbose
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
matrix_linear.h
Header file for matrix functions.
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:324
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
get_chunk_size
size_t get_chunk_size(size_t datasize)
Returns the chunk size per thread.
Definition: utilities.cpp:609
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Bimage::filter_sinc
int filter_sinc()
Weighs an image with a sinc function.
Definition: Bimage_filter.cpp:365
Bimage::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::filter_rank
int filter_rank(long kernel_size, double rank)
Applies a median filter to an image.
Definition: Bimage_filter.cpp:1021
Bimage::convolve
int convolve(Bimage *pkernel)
Convolves an image with an arbitrary size convolution filter.
Definition: Bimage_filter.cpp:473
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::image_size
long image_size()
Definition: Bimage.h:290
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
kmax
long kmax(long i, long km, long m)
Definition: Bimage_filter.cpp:22
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::filter_rank_chunk
int filter_rank_chunk(long kernel_size, double rank, float *nudata, long i, long len)
Definition: Bimage_filter.cpp:940
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage.h
Header file for image class.
Bimage::data_alloc_and_clear
unsigned char * data_alloc_and_clear()
Definition: Bimage.h:272
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
partition
int partition(vector< double > &a, int n, int k)
Determines the index k'th value in the array.
Definition: math_util.cpp:123
Bimage::convolve_chunk
int convolve_chunk(Bimage *pkernel, float *nudata, long i, long len)
Definition: Bimage_filter.cpp:392
Bimage::subtract
void subtract(Bimage *p)
Subtracts another image from an image.
Definition: Bimage.cpp:3406
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bimage::kernel_gaussian
int kernel_gaussian(double sigma, double max)
Generates a gaussian kernel image.
Definition: Bimage_filter.cpp:34
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
Bimage::periodic_averaging
Bimage * periodic_averaging(Vector3< double > period)
Calculates an average within a periodic frame.
Definition: Bimage_filter.cpp:1144
math_util.h
Header file for general utilities.
Bimage::filter_bilateral_chunk
int filter_bilateral_chunk(Bimage *pkernel, double sigma2, int kernel_type, float *nudata, long i, long len, int first)
Definition: Bimage_filter.cpp:670
Bimage::filter_peak
Bimage * filter_peak(long kernel_size)
Finds the peaks in an image.
Definition: Bimage_filter.cpp:1075
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
Vector3::min
Type min()
Definition: Vector3.h:169
Double
@ Double
Definition: utilities.h:162
Vector3::length
double length()
Definition: Vector3.h:222
kmin
long kmin(long i, long hk)
Definition: Bimage_filter.cpp:17
Bimage::kernel_laplacian_of_gaussian
int kernel_laplacian_of_gaussian(double sigma, double max)
Generates a laplacian-of-gaussian kernel.
Definition: Bimage_filter.cpp:77
Bimage::filter_dog
int filter_dog(double sigma1, double sigma2)
Convolves the image with a difference of gausians kernel.
Definition: Bimage_filter.cpp:641
Bimage::filter_ortho
int filter_ortho(int type)
Convolves the image with an orthogonal kernel with wrapping.
Definition: Bimage_filter.cpp:544
Bimage::filter_rolling_ball
int filter_rolling_ball(long radius, double scale)
Apply a rolling ball filter.
Definition: Bimage_filter.cpp:835
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::filter_bilateral
int filter_bilateral(double sigma1, double sigma2, int kernel_type, long kernel_radius)
Denoise an image with combined gaussian distance and density difference kernel.
Definition: Bimage_filter.cpp:783
kernel
Definition: kernlut.h:14
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
TWOPI
#define TWOPI
Definition: utilities.h:72