Bsoft  2.1.0
Bernard's software package
Bimage_bin.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 
15 
16 int Bimage::integer_interpolation(int integer_factor)
17 {
18  return integer_interpolation(integer_factor, 21);
19 }
20 
50 int Bimage::integer_interpolation(int integer_factor, int odd)
51 {
52  if ( integer_factor < 2 ) return 0;
53 
54  // Calculate the new size and kernel weights
55  long i, j, ix, iy, iz, cc;
56  long nn, wx, wy, wz, xx, yy, zz;
57  int dimensions(0);
58  Vector3<long> nusize(1,1,1);
59  Vector3<long> oldnomori(x/2, y/2, z/2);
60  Vector3<long> nunomori;
61  Vector3<long> kernelsize(1,1,1);
62  Vector3<double> nusam(image->sampling());
63 
64  if ( x > 1 ) {
65  kernelsize[0] = 2*integer_factor - 1;
66  nusize[0] = (x + 1)/integer_factor;
67  if ( (odd & 1) && nusize[0]%2 == 0 ) nusize[0]--; // Size must be odd
68  if ( (odd & 2) && nusize[0]%2 == 1 ) nusize[0]--; // Size must be even
69  nunomori[0] = nusize[0]/2;
70  nusam[0] *= integer_factor;
71  dimensions++;
72  }
73  if ( y > 1 ) {
74  kernelsize[1] = 2*integer_factor - 1;
75  nusize[1] = (y + 1)/integer_factor;
76  if ( (odd & 4) && nusize[1]%2 == 0 ) nusize[1]--; // Size must be odd
77  if ( (odd & 8) && nusize[1]%2 == 1 ) nusize[1]--; // Size must be even
78  nunomori[1] = nusize[1]/2;
79  nusam[1] *= integer_factor;
80  dimensions++;
81  }
82  if ( z > 1 ) {
83  kernelsize[2] = 2*integer_factor - 1;
84  nusize[2] = (z + 1)/integer_factor;
85  if ( (odd & 16) && nusize[2]%2 == 0 ) nusize[2]--; // Size must be odd
86  if ( (odd & 32) && nusize[2]%2 == 1 ) nusize[2]--; // Size must be even
87  nunomori[2] = nusize[2]/2;
88  nusam[2] *= integer_factor;
89  dimensions++;
90  }
91 
92  double kernel_scale = 1.0/pow(1.0*integer_factor, 2.0*dimensions);
93  vector<double> kernel(kernelsize[0]*kernelsize[1]*kernelsize[2]);
94 
95  for ( i=zz=0; zz<kernelsize[2]; zz++ ) {
96  wz = (int) (integer_factor - fabs(integer_factor - 1.0 - zz));
97  for ( yy=0; yy<kernelsize[1]; yy++ ) {
98  wy = (int) (integer_factor - fabs(integer_factor - 1.0 - yy));
99  for ( xx=0; xx<kernelsize[0]; xx++, i++ ) {
100  wx = (int) (integer_factor - fabs(integer_factor - 1.0 - xx));
101  kernel[i] = wx*wy*wz*kernel_scale;
102  }
103  }
104  }
105 
106  long datasize = n*nusize[0]*nusize[1]*nusize[2]*c;
107  double* nudata = new double[datasize];
108 
109  if ( verbose & VERB_PROCESS ) {
110  cout << "Special integer interpolation:" << endl;
111  cout << "Integer factor: " << integer_factor << endl;
112  cout << "Interpolation kernel size: " << kernelsize << endl;
113  cout << "New image size: " << nusize << endl;
114  } else if ( verbose & VERB_LABEL )
115  cout << "Special integer interpolation" << endl << endl;
116 
117  if ( verbose & VERB_FULL ) {
118  cout << "Kernel interpolation: Kernel" << endl;
119  for ( i=zz=0; zz<kernelsize[2]; zz++ ) {
120  for ( yy=0; yy<kernelsize[1]; yy++ ) {
121  for ( xx=0; xx<kernelsize[0]; xx++, i++ ) {
122  cout << " " << kernel[i];
123  }
124  cout << endl;
125  }
126  }
127  cout << endl;
128  }
129 
130  // Do the kernel interpolation
131  long oldx, oldy, oldz, xkernel, ykernel, zkernel;
132  long zlo, zhi, ylo, yhi, xlo, xhi;
133  vector<double> sum(c);
134 
135  for ( nn=0; nn<n; nn++ ) {
136  for ( iz=0; iz<nusize[2]; iz++ ) {
137  oldz = (iz - nunomori[2])*integer_factor + oldnomori[2] - kernelsize[2]/2;
138  zlo = oldz;
139  if ( zlo < 0 ) zlo = 0;
140  zhi = oldz + kernelsize[2];
141  if ( zhi > z ) zhi = z;
142  for ( iy=0; iy<nusize[1]; iy++ ) {
143  oldy = (iy - nunomori[1])*integer_factor + oldnomori[1] - kernelsize[1]/2;
144  ylo = oldy;
145  if ( ylo < 0 ) ylo = 0;
146  yhi = oldy + kernelsize[1];
147  if ( yhi > y ) yhi = y;
148  for ( ix=0; ix<nusize[0]; ix++ ) {
149  oldx = (ix - nunomori[0])*integer_factor + oldnomori[0] - kernelsize[0]/2;
150  xlo = oldx;
151  if ( xlo < 0 ) xlo = 0;
152  xhi = oldx + kernelsize[0];
153  if ( xhi > x ) xhi = x;
154  for ( cc=0; cc<c; cc++ ) sum[cc] = 0;
155  for ( zz=zlo; zz<zhi; zz++ ) {
156  zkernel = zz - oldz;
157  for ( yy=ylo; yy<yhi; yy++ ) {
158  ykernel = yy - oldy;
159  for ( xx=xlo; xx<xhi; xx++ ) {
160  xkernel = xx - oldx;
161  i = index(0, xx, yy, zz, nn);
162  j = (zkernel*kernelsize[1] + ykernel)*kernelsize[0] + xkernel;
163  for ( cc=0; cc<c; cc++ ) sum[cc] += (*this)[i++] * kernel[j];
164 
165  }
166  }
167  }
168  i = (((nn*nusize[2] + iz)*nusize[1] + iy)*nusize[0] + ix)*c;
169  for ( cc=0; cc<c; cc++ ) nudata[i++] = sum[cc];
170  }
171  }
172  }
173  image[nn].origin((image[nn].origin() - oldnomori)/integer_factor + nunomori);
174  if ( verbose & VERB_STATS )
175  cout << "Image " << nn+1 << " origin: " << image[nn].origin() << endl;
176  }
177 
178  if ( verbose & VERB_DEBUG )
179  cout << "Kernel interpolation: Sums done" << endl;
180 
181  // Set new image parameters
182  size(nusize);
183  page_size(nusize);
184  sampling(nusam);
185 
186  // Transfer the data to a new block and free the old
187  data_alloc();
188  for ( i=0; i<datasize; i++ ) set(i, nudata[i]);
189 
190  delete[] nudata;
191 
192  statistics();
193 
194  return 0;
195 }
196 
197 int Bimage::bin(long i, Vector3<long> bk, Bimage* pb)
198 {
199  long j, cc, xx, yy, zz;
200  long nn(i/(pb->image_size()*c));
201  Vector3<long> coor = pb->coordinates(i);
202  Vector3<long> bs(bk * coor);
203  Vector3<long> bf(bs + bk);
204  long binsize(0);
205  vector<double> binsum(c);
206  for ( cc=0; cc<c; cc++ ) binsum[cc] = 0;
207 
208  for ( zz=bs[2]; zz<bf[2] && zz<z; zz++ ) {
209  for ( yy=bs[1]; yy<bf[1] && yy<y; yy++ ) {
210  for ( xx=bs[0]; xx<bf[0] && xx<x; xx++ ) {
211  binsize++;
212  j = index(0, xx, yy, zz, nn);
213  for ( cc=0; cc<c; cc++ ) binsum[cc] += (*this)[j++];
214  }
215  }
216  }
217 
218  for ( cc=0; cc<c; cc++ ) pb->set(i++, binsum[cc]/binsize);
219 
220  return 0;
221 }
222 
231 int Bimage::bin(Vector3<long> bk)
232 {
233  if ( verbose & VERB_DEBUG )
234  cout << "DEBUG Bimage::bin: bk=" << bk << endl;
235 
236  Bimage* pb = bin_copy(bk);
237 
238  // Set new image parameters
239  size(pb->size());
240  page_size(size());
241 
242  for ( long i=0; i<n; i++ ) {
243  image[i].origin(pb->image[i].origin());
244  image[i].sampling(pb->image[i].sampling());
245  }
246 
247 // cout << "alloc_size=" << alloc_size() << endl;
248  // Transfer the data to a new block and free the old
249  data_alloc();
250 // cout << "datasize=" << datasize << endl;
251  for ( long i=0; i<datasize; i++ ) set(i, (*pb)[i]);
252  delete pb;
253 
254  statistics();
255 
256  return 0;
257 }
258 
268 {
269  if ( bk[0] < 1 ) bk[0] = 1;
270  if ( bk[1] < 1 ) bk[1] = 1;
271  if ( bk[2] < 1 ) bk[2] = 1;
272  if ( x < bk[0] ) bk[0] = x;
273  if ( y < bk[1] ) bk[1] = y;
274  if ( z < bk[2] ) bk[2] = z;
275 
276  if ( bk[0]*bk[1]*bk[2] < 2 ) return copy();
277 
278  Vector3<long> nusize;
279  nusize[0] = (x + bk[0] - 1)/bk[0];
280  nusize[1] = (y + bk[1] - 1)/bk[1];
281  nusize[2] = (z + bk[2] - 1)/bk[2];
282 
283  Bimage* pb = copy_header();
284 
285  pb->data_type(Float);
286  pb->size(nusize);
287 
288  for ( long i=0; i<pb->images(); i++ ) {
289  pb->image[i].origin(pb->image[i].origin()/bk);
290  pb->image[i].sampling(image[i].sampling()*bk);
291  }
292 
293  pb->data_alloc();
294 
295  if ( verbose & VERB_PROCESS ) {
296  cout << "Binning:" << endl;
297  cout << "Bin size: " << bk << endl;
298  cout << "New image size: " << nusize << endl << endl;
299  } else if ( verbose & VERB_LABEL )
300  cout << "Binning" << endl << endl;
301 
302 #ifdef HAVE_GCD
303  dispatch_apply(pb->data_size()/c, dispatch_get_global_queue(0, 0), ^(size_t i){
304  bin(i*c, bk, pb);
305  });
306 #else
307 #pragma omp parallel for
308  for ( long i=0; i<pb->data_size(); i+=c )
309  bin(i, bk, pb);
310 #endif
311 
312  if ( verbose & VERB_DEBUG )
313  cout << "Binning: Sums done" << endl;
314 
315  pb->statistics();
316 
317 // pb->information();
318 // delete pb;
319 
320  return pb;
321 }
322 
332 {
333  if ( bin%2 == 0 ) bin++; // bin must be odd
334 
335  Bimage* pt = copy();
336 
337  pt->filter_average(bin);
338 
339  long i, j, xx, yy, zz, xo, yo, zo, xj, yj, zj, nn;
340  Vector3<long> nusize(x/bin + 1, y/bin + 1, z/bin + 1);
342 
343  if ( verbose & VERB_PROCESS ) {
344  cout << "Binning: " << bin << endl;
345  cout << "New binned size: " << nusize << endl << endl;
346  }
347 
348  Bimage* pb = new Bimage(Float, compoundtype, nusize, n);
349  pb->sampling(image->sampling()*bin);
350 
351  for ( i=nn=0; nn<n; ++nn ) {
352  pb->image[nn].origin(image[nn].origin()/bin);
353  shift = image[nn].origin() - pb->image[nn].origin() * bin;
354  if ( verbose )
355  cout << nn << tab << shift << endl;
356  zo = shift[2];
357  for ( zz=0; zz<pb->sizeZ(); ++zz, zo+=bin ) {
358  zj = zo;
359  if ( zj < 0 ) zj = 0;
360  if ( zj >= z ) zj = z - 1;
361  yo = shift[1];
362  for ( yy=0; yy<pb->sizeY(); ++yy, yo+=bin ) {
363  yj = yo;
364  if ( yj < 0 ) yj = 0;
365  if ( yj >= y ) yj = y - 1;
366  xo = shift[0];
367  for ( xx=0; xx<pb->sizeX(); ++xx, xo+=bin, ++i ) {
368  xj = xo;
369  if ( xj < 0 ) xj = 0;
370  if ( xj >= x ) xj = x - 1;
371  j = index(0, xj, yj, zj, nn);
372  pb->set(i, (*pt)[j]);
373  }
374  }
375  }
376  }
377 
378  delete pt;
379 
380  return pb;
381 }
382 
383 
392 int Bimage::median_bin(int binning)
393 {
394  if ( binning < 2 ) return -1;
395 
396  if ( compound_type() > TSimple ) {
397  error_show("Bimage::median_bin", __FILE__, __LINE__);
398  cerr << "Error: Interpolation of compound data sets not supported!" << endl << endl;
399  return -1;
400  }
401 
402  // Calculate the new size and set up
403  long i, j, k;
404  long nn, ix, iy, iz, xx, yy, zz, dimensions(0);
405  Vector3<long> nusize(1,1,1);
407  Vector3<long> blocksize(1,1,1);
408  Vector3<double> nusam(image->sampling());
409 
410  if ( x > 1 ) {
411  blocksize[0] = 2*binning - 1;
412  nusize[0] = (x + 1)/binning;
413  if ( 2*(nusize[0]/2) == nusize[0] ) nusize[0]--; // Size must be odd
414  shift[0] = nusize[0] - x/binning;
415  nusam[0] *= binning;
416  dimensions++;
417  }
418  if ( y > 1 ) {
419  blocksize[1] = 2*binning - 1;
420  nusize[1] = (y + 1)/binning;
421  if ( 2*(nusize[1]/2) == nusize[1] ) nusize[1]--; // Size must be odd
422  shift[1] = nusize[1] - y/binning;
423  nusam[1] *= binning;
424  dimensions++;
425  }
426  if ( z > 1 ) {
427  blocksize[2] = 2*binning - 1;
428  nusize[2] = (z + 1)/binning;
429  if ( 2*(nusize[2]/2) == nusize[2] ) nusize[2]--; // Size must be odd
430  shift[2] = nusize[2] - z/binning;
431  nusam[2] *= binning;
432  dimensions++;
433  }
434 
435  long blocktotalsize = blocksize[0]*blocksize[1]*blocksize[2];
436  vector<double> block(blocktotalsize);
437 
438  long datasize = n*nusize[0]*nusize[1]*nusize[2]*c;
439  double* nudata = new double[datasize];
440 
441  if ( verbose & VERB_PROCESS ) {
442  cout << "Binning using a median filter:" << endl;
443  cout << "Block size: " << blocksize << endl;
444  cout << "New image size: " << nusize << endl << endl;
445  } else if ( verbose & VERB_LABEL )
446  cout << "Binning using a median filter" << endl << endl;
447 
448  // Do the median binning interpolation
449  long oldx, oldy, oldz, xblock, yblock, zblock, nblock, imedian;
450  long zlo, zhi, ylo, yhi, xlo, xhi;
451  for ( i=nn=0; nn<n; nn++ ) {
452  for ( iz=0; iz<nusize[2]; iz++ ) {
453  oldz = iz*binning - shift[2]; // Origin of the block
454  zlo = oldz;
455  if ( zlo < 0 ) zlo = 0;
456  zhi = oldz + blocksize[2];
457  if ( zhi > z ) zhi = z;
458  for ( iy=0; iy<nusize[1]; iy++ ) {
459  oldy = iy*binning - shift[1];
460  ylo = oldy;
461  if ( ylo < 0 ) ylo = 0;
462  yhi = oldy + blocksize[1];
463  if ( yhi > y ) yhi = y;
464  for ( ix=0; ix<nusize[0]; ix++, i++ ) {
465  oldx = ix*binning - shift[0];
466  xlo = oldx;
467  if ( xlo < 0 ) xlo = 0;
468  xhi = oldx + blocksize[0];
469  if ( xhi > x ) xhi = x;
470  nblock = 0;
471  for ( zz=zlo; zz<zhi; zz++ ) {
472  zblock = zz - oldz;
473  for ( yy=ylo; yy<yhi; yy++ ) {
474  yblock = yy - oldy;
475  for ( xx=xlo; xx<xhi; xx++ ) {
476  xblock = xx - oldx;
477  k = ((nn*z + zz)*y + yy)*x + xx;
478  j = (zblock*blocksize[1] + yblock)*blocksize[0] + xblock;
479  block[j] = (*this)[k];
480  nblock++;
481  }
482  }
483  }
484  imedian = nblock/2;
485  partition(block, nblock, imedian);
486  nudata[i] = block[imedian];
487  }
488  }
489  }
490  image[nn].origin(image[nn].origin() * (1.0/binning));
491  if ( verbose & VERB_STATS )
492  cout << "Image " << nn+1 << " origin: " << image[nn].origin() << endl;
493  }
494 
495  if ( verbose & VERB_DEBUG )
496  cout << "Median binning interpolation done" << endl;
497 
498  // Set new image parameters
499  size(nusize);
500  page_size(nusize);
501  sampling(nusam);
502 
503  // Transfer the data to a new block and free the old
504  data_alloc();
505  for ( i=0; i<datasize; i++ ) set(i, nudata[i]);
506 
507  delete[] nudata;
508 
509  statistics();
510 
511  return 0;
512 }
513 
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
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
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::page_size
Vector3< long > page_size()
Definition: Bimage.h:493
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::sum
void sum(long m, Bimage **p)
Sums an array of images with their FOM blocks.
Definition: Bimage_combine.cpp:23
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
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
verbose
int verbose
Bimage::bin_around_origin
Bimage * bin_around_origin(int bin)
Bins by an integer size making sure the origin falls on a binned voxel, and returns a new image.
Definition: Bimage_bin.cpp:331
Bimage::integer_interpolation
int integer_interpolation(int integer_factor)
Definition: Bimage_bin.cpp:16
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Bimage::bin_copy
Bimage * bin_copy(long b)
Definition: Bimage.h:1298
Bimage::data_size
long data_size()
Definition: Bimage.h:270
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage.h
Header file for image class.
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::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
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
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
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
VERB_STATS
#define VERB_STATS
Definition: utilities.h:112
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::median_bin
int median_bin(int binning)
Bins by an integer size, selecting the kernel median.
Definition: Bimage_bin.cpp:392
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
kernel
Definition: kernlut.h:14
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22