Bsoft  2.1.0
Bernard's software package
img_combine.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "img_combine.h"
11 #include "linked_list.h"
12 #include "utilities.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
32 Bimage* img_add(Bstring* file_list, int flags)
33 {
34  if ( !file_list ) return NULL;
35 
36  int calcavg = flags & 1;
37  int calcfom = (flags & 2) >> 1;
38  long nfiles = count_list((char *) file_list);
39 
40  long i, j, n(0), nf, nimg(0);
41  double v, va, w;
42  Bstring* filename;
43  Bimage* p = NULL;
44 
45  Bimage* psum = read_img(*file_list, 1, 0);
46  psum->clear();
47  psum->change_type(Float);
48 
49  Bimage* pfom = psum->next = psum->copy();
50 
51  long imgsize = psum->sizeX()*psum->sizeY()*psum->sizeZ()*psum->channels();
52 
53  if ( verbose & ( VERB_PROCESS | VERB_LABEL ) )
54  cout << endl << "Adding " << nfiles << " files together:" << endl;
55 
56  for ( nf=0, filename = file_list; filename && nf<nfiles; filename = filename->next, nf++ ) {
57  p = read_img(*filename, 1, -1);
58  if ( p != NULL ) {
59  if ( verbose & VERB_LABEL )
60  cout << "Adding file " << nf << " with images " << p->images() << endl;
61  for ( n=j=0; n<p->images(); n++ ) {
62  for ( i=n*imgsize; i<(n+1)*imgsize; i++, j++ ) {
63  v = (*p)[j];
64  psum->add(j, v);
65  if ( calcfom ) pfom->add(j, v*v);
66  }
67  }
68  nimg += p->images();
69  delete p;
70  }
71  }
72 
73  w = 1.0/nimg;
74 
75  for ( i=n=0; n<psum->images(); n++ ) {
76  for ( j=0; j<imgsize; j++, i++ ) {
77  va = (*psum)[i] * w;
78  if ( calcavg ) psum->set(i, va);
79  else psum->set(i, (*psum)[i]);
80  v = (*pfom)[i];
81  if ( calcfom > 0 ) {
82  v = v * w - va*va;
83  if ( v < 0 ) v = 0;
84  }
85  if ( calcfom > 1 ) v = sqrt(v);
86  pfom->set(i, v);
87  }
88  }
89 
90  psum->statistics();
91  pfom->statistics();
92 
93  return psum;
94 }
95 
96 
107 Bimage* img_setup_combined(Bstring* file_list, long& nimg, int cat)
108 {
109  if ( !file_list ) return NULL;
110 
111  long nf(0), nn(0);
112  Vector3<double> sam(1,1,1);
113  Bstring* filename;
114  Bimage* p;
115  Bimage* pc = new Bimage;
116 
117  nimg = 0;
118 
119  for ( filename = file_list; filename; filename = filename->next, nf++ ) {
120  p = read_img(*filename, 0, -1);
121  if ( p != NULL ) {
122  if ( pc->channels() < p->channels() ) pc->channels(p->channels());
123  if ( pc->data_type() < p->data_type() ) pc->data_type(p->data_type());
124  if ( pc->compound_type() < p->compound_type() ) pc->compound_type(p->compound_type());
125  pc->size(pc->size().max(p->size()));
126  nimg += p->images();
127  if ( nn < p->images() ) nn = p->images();
128  if ( nf == 0 ) sam = p->image->sampling();
129  delete p;
130  }
131  }
132 
133  if ( cat ) pc->images(nimg);
134  else pc->images(nn);
135  pc->origin(pc->default_origin());
136  pc->sampling(sam);
137 
138  pc->data_alloc_and_clear();
139 
140  pc->information();
141 
142  return pc;
143 }
144 
145 
146 
163 Bimage* img_catenate(Bstring* file_list, Bstring& rawstring, DataType nudatatype,
164  Vector3<long> nusize, int setZslices, int fill_type, double fill,
165  double newavg, double newstd)
166 {
167 
168  // Set up image structures
169  Bimage* p = NULL;
170  Bimage* pcat = NULL;
171  Bstring filename, *name;
172  int err(0);
173  long i, j, k;
174 
175  if ( rawstring.length() > 0 && rawstring[0] != '#' ) rawstring = "#" + rawstring;
176 
177  // First read the file headers to identify problems
178  for ( i=j=0, name = file_list; name; name = name->next ) {
179  filename = *name + rawstring;
180  p = read_img(filename, 0, -1);
181  if ( p ) {
182  if ( verbose & VERB_DEBUG )
183  cout << "DEBUG img_catenate: " << p->file_name() << " " <<
184  p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << " " << p->images() << " " << p->channels() << endl;
185  if ( i == 0 ) {
186  pcat = p;
187  if ( fill_type == FILL_AVERAGE ) fill = p->average();
188  if ( fill_type == FILL_BACKGROUND ) fill = p->background(long(0));
189  if ( p->fourier_type() ) p->fourier_type(Standard);
190  } else {
191  if ( setZslices && p->images() > 1 ) {
192  cerr << p->file_name() << " must be a single image file to pack slices" << endl;
193  err += -1;
194  }
195 // err += img_compatibility(pcat, p);
196  if ( !p->compatible(pcat) ) err += -1;
197  if ( pcat->sizeX() < p->sizeX() ) pcat->sizeX(p->sizeX());
198  if ( pcat->sizeY() < p->sizeY() ) pcat->sizeY(p->sizeY());
199  if ( pcat->sizeZ() < p->sizeZ() ) pcat->sizeZ(p->sizeZ());
200  if ( fill_type == FILL_AVERAGE ) fill += p->average();
201  if ( fill_type == FILL_BACKGROUND ) fill += p->background(long(0));
202  delete p;
203  }
204  if ( setZslices ) i += p->sizeZ();
205  else i += p->images();
206  j++;
207  } else err += -1;
208  }
209 
210  if ( err < 0 || j == 0 ) {
211  cerr << "Files not concatenated! Number of errors: " << -err << endl;
212  bexit(err);
213  }
214 
215  // Set up for catenation
216  if ( nusize.volume() > 0 ) pcat->size(nusize);
217  nusize = {pcat->sizeX(), pcat->sizeY(), pcat->sizeZ()};
218  if ( fill_type != FILL_USER ) fill /= j;
219  if ( setZslices ) {
220  pcat->sizeZ(i);
221  pcat->images(1);
222  } else {
223  pcat->images(i);
224  }
225  if ( nudatatype > Unknown_Type ) pcat->data_type(nudatatype);
226  pcat->data_alloc();
227 
228  if ( verbose & VERB_FULL )
229  cout << "Catenated size: n=" << pcat->images() << " size=" << pcat->size()
230  << " c=" << pcat->channels() << endl << endl;
231 
232  // Read the images including their data
233  long imgsize = (long) pcat->size().volume()*pcat->channels();
234  Vector3<long> translate;
235 
236  if ( setZslices ) imgsize = (long) pcat->sizeX()*pcat->sizeY()*pcat->channels();
237 
238  if ( verbose )
239  cout << "File\tImages\tStart" << endl;
240  for ( i=0, name = file_list; name; name = name->next ) {
241  filename = *name + rawstring;
242  p = read_img(filename, 1, -1);
243  if ( p != NULL ) {
244  if ( verbose )
245  cout << p->file_name() << tab << p->images()
246  << tab << i << endl;
247  if ( setZslices ) nusize[2] = p->sizeZ();
248  if ( newstd ) p->rescale_to_avg_std(newavg, newstd);
249  translate = (nusize - p->size())/2;
250  p->resize(nusize, translate, fill_type, fill);
251  p->change_type(nudatatype);
252  for ( j=i*imgsize, k=0; k<imgsize*p->images(); j++, k++ ) pcat->set(j, (*p)[k]);
253  if ( setZslices ) i += p->sizeZ();
254  else for ( j=0; j<p->images(); j++, i++ ) pcat->image[i] = p->image[j];
255  delete p;
256  }
257  }
258 
259  if ( verbose )
260  cout << "Number of images: " << pcat->images() << endl;
261 
262  if ( verbose & VERB_DEBUG )
263  cout << "DEBUG img_catenate: final_size=" << i*imgsize << endl;
264 
265  return pcat;
266 }
267 
291 Bimage* img_add_weighed(Bstring* file_list, vector<double> weight,
292  double newavg, double newstd, int flags)
293 {
294  if ( !file_list ) return NULL;
295 
296  int calcavg = flags & 1;
297  int calcfom = (flags & 2) >> 1;
298  int center = flags & 8;
299 
300  long i, j, n(0), nf, nimg(0), c(0);
301  double v;
302  Bstring* filename;
303  Bimage* p = NULL;
304 
305  Bimage* psum = img_setup_combined(file_list, nimg, 0);
306  long nfiles = count_list((char *) file_list);
307 
308  double weightsum(0);
309  if ( weight.size() < nfiles )
310  for ( i=weight.size(); i<nfiles; i++ ) weight.push_back(1);
311 
312  for ( i=0; i<nfiles; i++ ) weightsum += weight[i];;
313 
314  double degrees_of_freedom = weightsum*(1 - 1.0L/nfiles);
315 
316  if ( verbose & VERB_PROCESS ) {
317  cout << endl << "Adding " << nimg << " images together:" << endl;
318  cout << "New image size: " << psum->size() << endl;
319  cout << "Degrees of freedom: " << degrees_of_freedom << endl << endl;
320  } else if ( verbose & VERB_LABEL )
321  cout << endl << "Adding " << nimg << " images together" << endl << endl;
322 
323  long imgsize = psum->sizeX()*psum->sizeY()*psum->sizeZ();
324  long datasize = imgsize*psum->images();
325 
326  float* fom = NULL;
327  if ( calcfom ) {
328  psum->next = new Bimage(Float, TSimple, psum->size(), psum->images());
329  fom = (float *) psum->next->data_pointer();
330  }
331 
332  vector<double> imgweight(psum->images());
333 
334  for ( nf=0, filename = file_list; filename && nf<nfiles; filename = filename->next, nf++ ) {
335  p = read_img(*filename, 1, -1);
336  if ( p != NULL ) {
337  if ( newstd > 0 ) p->rescale_to_avg_std(newavg, newstd);
338  if ( center ) p->center_wrap();
339  if ( verbose & VERB_LABEL )
340  cout << "Adding image " << nf << " with weight " << weight[nf] << endl << endl;
341  for ( n=j=0; n<p->images(); n++ ) {
342  imgweight[n] += weight[nf];
343  for ( i=n*imgsize; i<(n+1)*imgsize; i++ ) {
344  for ( c=0; c<psum->channels(); c++, j++ ) {
345  v = (*p)[j];
346  psum->add(j, weight[nf]*v);
347  if ( calcfom ) fom[i] += weight[nf]*v*v;
348  }
349  }
350  }
351  delete p;
352  }
353  }
354 
355  if ( calcfom ) {
356  for ( i=n=0; n<psum->images(); n++ )
357  for ( j=0; j<imgsize; j++, i++ )
358  fom[i] = (fom[i] - (*psum)[i]*(*psum)[i]/imgweight[n])/degrees_of_freedom;
359  if ( calcfom > 1 ) {
360  for ( i=0; i<datasize; i++ ) {
361  if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
362  else fom[i] = 0;
363  }
364  }
365  }
366 
367  if ( calcavg )
368  for ( i=n=0; n<psum->images(); n++ )
369  for ( j=0; j<imgsize; j++, i++ )
370  psum->set(i, (*psum)[i] / imgweight[n]);
371 
372  psum->statistics();
373 
374  return psum;
375 }
376 
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
img_add
Bimage * img_add(Bstring *file_list, int flags)
Adds multiple images together with optional variance or standard deviation.
Definition: img_combine.cpp:32
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
Bimage::information
int information()
Prints out header information for an image.
Definition: Bimage.cpp:2508
Bimage::background
double background(long nn)
Definition: Bimage.h:549
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
img_catenate
Bimage * img_catenate(Bstring *file_list, Bstring &rawstring, DataType nudatatype, Vector3< long > nusize, int setZslices, int fill_type, double fill, double newavg, double newstd)
Catenates a list of images into a multi-image structure.
Definition: img_combine.cpp:163
Standard
@ Standard
Definition: Bimage.h:42
Bimage::images
long images()
Definition: Bimage.h:504
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
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::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::clear
void clear()
Definition: Bimage.h:705
Bimage.h
Header file for image class.
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:708
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
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
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
verbose
int verbose
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bimage::compatible
bool compatible(Bimage *p)
Check if this image has the same number of channels and data type as another.
Definition: Bimage.cpp:653
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
img_setup_combined
Bimage * img_setup_combined(Bstring *file_list, long &nimg, int cat)
Sets up a list of images for concatenation or summation.
Definition: img_combine.cpp:107
img_add_weighed
Bimage * img_add_weighed(Bstring *file_list, vector< double > weight, double newavg, double newstd, int flags)
Adds multiple images together with given weights.
Definition: img_combine.cpp:291
img_combine.h
Header file for combining two images in various ways.
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
Vector3::max
Type max()
Definition: Vector3.h:175