| Bsoft 2.0.5 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();
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 for ( i=0, name = file_list; name; name = name->next ) {
239 filename = *name + rawstring;
240 p = read_img(filename, 1, -1);
241 if ( p != NULL ) {
242 if ( verbose )
243 cout << p->file_name() << tab << p->images() << endl;
244 if ( setZslices ) nusize[2] = p->sizeZ();
245 if ( newstd ) p->rescale_to_avg_std(newavg, newstd);
246 translate = (nusize - p->size())/2;
247 p->resize(nusize, translate, fill_type, fill);
248 p->change_type(nudatatype);
249 for ( j=i*imgsize, k=0; k<imgsize*p->images(); j++, k++ ) pcat->set(j, (*p)[k]);
250 if ( setZslices ) i += p->sizeZ();
251 else for ( j=0; j<p->images(); j++, i++ ) pcat->image[i] = p->image[j];
252 delete p;
253 }
254 }
255
256 if ( verbose )
257 cout << "Number of images: " << pcat->images() << endl;
258
259 if ( verbose & VERB_DEBUG )
260 cout << "DEBUG img_catenate: final_size=" << i*imgsize << endl;
261
262 return pcat;
263 }
264
288 Bimage* img_add_weighed(Bstring* file_list, vector<double> weight,
289 double newavg, double newstd, int flags)
290 {
291 if ( !file_list ) return NULL;
292
293 int calcavg = flags & 1;
294 int calcfom = (flags & 2) >> 1;
295 int center = flags & 8;
296
297 long i, j, n(0), nf, nimg(0), c(0);
298 double v;
299 Bstring* filename;
300 Bimage* p = NULL;
301
302 Bimage* psum = img_setup_combined(file_list, nimg, 0);
303 long nfiles = count_list((char *) file_list);
304
305 double weightsum(0);
306 if ( weight.size() < nfiles )
307 for ( i=weight.size(); i<nfiles; i++ ) weight.push_back(1);
308
309 for ( i=0; i<nfiles; i++ ) weightsum += weight[i];;
310
311 double degrees_of_freedom = weightsum*(1 - 1.0L/nfiles);
312
313 if ( verbose & VERB_PROCESS ) {
314 cout << endl << "Adding " << nimg << " images together:" << endl;
315 cout << "New image size: " << psum->size() << endl;
316 cout << "Degrees of freedom: " << degrees_of_freedom << endl << endl;
317 } else if ( verbose & VERB_LABEL )
318 cout << endl << "Adding " << nimg << " images together" << endl << endl;
319
320 long imgsize = psum->sizeX()*psum->sizeY()*psum->sizeZ();
321 long datasize = imgsize*psum->images();
322
323 float* fom = NULL;
324 if ( calcfom ) {
325 psum->next = new Bimage(Float, TSimple, psum->size(), psum->images());
326 fom = (float *) psum->next->data_pointer();
327 }
328
329 vector<double> imgweight(psum->images());
330
331 for ( nf=0, filename = file_list; filename && nf<nfiles; filename = filename->next, nf++ ) {
332 p = read_img(*filename, 1, -1);
333 if ( p != NULL ) {
334 if ( newstd > 0 ) p->rescale_to_avg_std(newavg, newstd);
335 if ( center ) p->center_wrap();
336 if ( verbose & VERB_LABEL )
337 cout << "Adding image " << nf << " with weight " << weight[nf] << endl << endl;
338 for ( n=j=0; n<p->images(); n++ ) {
339 imgweight[n] += weight[nf];
340 for ( i=n*imgsize; i<(n+1)*imgsize; i++ ) {
341 for ( c=0; c<psum->channels(); c++, j++ ) {
342 v = (*p)[j];
343 psum->add(j, weight[nf]*v);
344 if ( calcfom ) fom[i] += weight[nf]*v*v;
345 }
346 }
347 }
348 delete p;
349 }
350 }
351
352 if ( calcfom ) {
353 for ( i=n=0; n<psum->images(); n++ )
354 for ( j=0; j<imgsize; j++, i++ )
355 fom[i] = (fom[i] - (*psum)[i]*(*psum)[i]/imgweight[n])/degrees_of_freedom;
356 if ( calcfom > 1 ) {
357 for ( i=0; i<datasize; i++ ) {
358 if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
359 else fom[i] = 0;
360 }
361 }
362 }
363
364 if ( calcavg )
365 for ( i=n=0; n<psum->images(); n++ )
366 for ( j=0; j<imgsize; j++, i++ )
367 psum->set(i, (*psum)[i] / imgweight[n]);
368
369 psum->statistics();
370
371 return psum;
372 }
373
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
verbose
int verbose
Bimage::compatible
bool compatible(Bimage *p)
Check if this image has the same number of channels and data type as another.
Definition: Bimage.cpp:623
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bimage.h
Header file for image class.
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::add
void add(long j, double v)
Definition: Bimage.h:308
Bimage::information
int information()
Prints out header information for an image.
Definition: Bimage.cpp:2489
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
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:173
Bstring::length
long length() const
Definition: Bstring.cpp:263
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:713
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Vector3::max
Type max()
Definition: Vector3.h:175
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
linked_list.h
Header file for generalized linked list functions.
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
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bstring::next
Bstring * next
Definition: Bstring.h:31
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
Standard
Definition: Bimage.h:41
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
img_combine.h
Header file for combining two images in various ways.
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
FILL_USER
#define FILL_USER
Definition: utilities.h:172
Bimage::channels
long channels()
Definition: Bimage.h:371
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:123
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
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Bimage::next
Bimage * next
Definition: Bimage.h:165
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Bstring
Definition: Bstring.h:29
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2695
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::clear
void clear()
Definition: Bimage.h:697
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
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:1349
Unknown_Type
Definition: utilities.h:147
Bimage::images
long images()
Definition: Bimage.h:491
Vector3::volume
double volume()
Definition: Vector3.h:235
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
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:288
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563