Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_rescale.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "img_rescale.h"
11 #include "Complex.h"
12 #include "Matrix.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
31 int img_rescale(Bimage* p, double scale, double shift)
32 {
33  int err(-1);
34 
35  if ( !p->data_pointer() ) return err;
36 
37  unsigned long i, datasize = p->data_size();
38 
39  double tdata;
40  double min = p->data_type_min();
41  double max = p->data_type_max();
42 
43  if ( verbose & VERB_PROCESS )
44  cout << "Scale and shift: " << scale << " " << shift << endl << endl;
45 
46  // Adjust the background values
47  for ( i=0; i<p->images(); i++ )
48  p->image[i].background(p->image[i].background()*scale + shift);
49 
50  for ( i=0; i<datasize; i++ ) {
51  tdata = (*p)[i]*scale + shift;
52  if ( tdata < min ) tdata = min;
53  else if ( tdata > max ) tdata = max;
54  p->set(i, tdata);
55  }
56 
57  p->statistics();
58 
59  if ( p->standard_deviation() > 0 ) err = 0;
60 
61  return err;
62 }
63 
79 int img_rescale_to_min_max(Bimage* p, double min, double max)
80 {
81  if ( !p->data_pointer() ) return -1;
82 
83  if ( min >= max ) {
84  cerr << "Error in rescaling: minimum (" << min << ") is greater than maximum ("
85  << max << ") for " << p->file_name() << endl;
86  return -1;
87  }
88 
89  double scale = (max - min)/(p->maximum() - p->minimum());
90  double shift = min - p->minimum()*scale;
91 
92  if ( verbose & VERB_LABEL )
93  cout << "Rescaling to: " << min << " " << max << endl;
94 
95  return img_rescale(p, scale, shift);
96 }
97 
113 int img_rescale_to_avg_std(Bimage* p, double avg, double std)
114 {
115  if ( !p->data_pointer() ) return -1;
116 
117  if ( std < 0 ) {
118  cerr << "Warning: Cannot use a negative standard deviation to scale to! (" << std << ")" << endl;
119  return -1;
120  }
121 
122  if ( p->standard_deviation() < 1e-30 )
123  if ( p->statistics() )
124  cerr << tab << "in img_rescale_to_avg_std" << endl;
125 
126 // if ( p->standard_deviation() < 1e-30 ) if ( p->statistics() < 0 ) {
127 // cerr << "Error in img_rescale_to_avg_std: Statistics not recalculated for "
128 // << p->file_name() << "!" << endl;
129 // return -1;
130 // }
131 
132 // if ( p->standard_deviation() == 0 ) {
133 // cerr << "Error in img_rescale_to_avg_std: Standard deviation is zero for "
134 // << p->file_name() << "!" << endl;
135 // return -1;
136 // }
137 
138  double scale = std/p->standard_deviation();
139  double shift = avg - p->average()*scale;
140 
141  if ( verbose & VERB_LABEL )
142  cout << "Rescaling to average and stdev: " << avg << " " << std << endl;
143 
144  return img_rescale(p, scale, shift);
145 }
146 
147 
165 int img_truncate(Bimage* p, double min, double max, double setmin, double setmax)
166 {
167  if ( !p->data_pointer() ) return -1;
168 
169  unsigned long i, datasize = p->data_size();
170  double tdata;
171  double tmin = p->data_type_min();
172  double tmax = p->data_type_max();
173 
174  // Make sure the extremes don't exceed the data
175  if ( setmin < p->minimum() ) setmin = p->minimum();
176  if ( setmax > p->maximum() ) setmax = p->maximum();
177  if ( min < tmin ) min = tmin;
178  if ( max > tmax ) max = tmax;
179  if ( setmin < tmin ) setmin = tmin;
180  if ( setmax > tmax ) setmax = tmax;
181 
182  // Adjust the background values
183  for ( i=0; i<p->images(); i++ ) {
184  if ( p->image[i].background() < min ) p->image[i].background(setmin);
185  if ( p->image[i].background() > max ) p->image[i].background(setmax);
186  }
187 
188  if ( verbose & VERB_PROCESS ) {
189  cout << "Truncating to: " << min << " " << max << endl;
190  cout << "Min and max replacement values: " << setmin << " " << setmax << endl << endl;
191  }
192 
193  for ( i=0; i<datasize; i++ ) {
194  tdata = (*p)[i];
195  if ( tdata < min ) tdata = setmin;
196  if ( tdata > max ) tdata = setmax;
197  p->set(i, tdata);
198  }
199 
200  p->statistics();
201 
202  return 0;
203 }
204 
220 int img_truncate_to_min_max(Bimage* p, double min, double max)
221 {
222  return img_truncate(p, min, max, min, max);
223 }
224 
239 int img_truncate_to_avg(Bimage* p, double min, double max)
240 {
241  return img_truncate(p, min, max, p->average(), p->average());
242 }
243 
258 int img_truncate_to_background(Bimage* p, double min, double max)
259 {
260  if ( !p->data_pointer() ) return -1;
261 
262  unsigned long i, n, n1, imgsize = p->sizeX()*p->sizeY()*p->sizeZ()*p->channels();
263  double tdata, bkg;
264  double tmin = p->data_type_min();
265  double tmax = p->data_type_max();
266 
267  // Make sure the extremes don't exceed the data
268  if ( min < tmin ) min = tmin;
269  if ( max > tmax ) max = tmax;
270 
271  if ( verbose & VERB_PROCESS ) {
272  cout << "Truncating to background:" << endl;
273  cout << "Range: " << min << " " << max << endl;
274  cout << "Image\tBackground" << endl;
275  }
276 
277  // Adjust the background values
278  for ( n=0; n<p->images(); n++ ) {
279  if ( p->image[n].background() < min ) p->image[n].background(min);
280  if ( p->image[n].background() > max ) p->image[n].background(max);
281  if ( verbose & VERB_PROCESS )
282  cout << n+1 << tab << p->image[n].background() << endl;
283  }
284  if ( verbose & VERB_PROCESS )
285  cout << endl;
286 
287  for ( n=0; n<p->images(); n++ ) {
288  bkg = p->image[n].background();
289  for ( i=n*imgsize, n1=n+imgsize; i<n1; i++ ) {
290  tdata = (*p)[i];
291  if ( tdata < min ) tdata = bkg;
292  if ( tdata > max ) tdata = bkg;
293  p->set(i, tdata);
294  }
295  }
296 
297  p->statistics();
298 
299  return 0;
300 }
301 
314 int img_limit_levels(Bimage* p, int nlevels)
315 {
316  if ( !p->data_pointer() ) return -1;
317 
318  if ( nlevels > 255 ) {
319  cerr << "Error: There should be less than 255 gray levels (" << nlevels << ")" << endl;
320  return -1;
321  }
322 
323  if ( verbose & VERB_PROCESS )
324  cout << "Limiting levels to: " << nlevels << endl << endl;
325 
326  unsigned long i, datasize = p->data_size();
327  double unit = 255.0/(nlevels - 1);
328  double scale = 0.999*nlevels/(p->maximum() - p->minimum());
329 
330  unsigned char* nudata = new unsigned char[datasize];
331 
332  if ( verbose & VERB_LABEL )
333  cout << "Restricting to " << nlevels << " levels." << endl << endl;
334 
335  for ( i=0; i<datasize; i++ )
336  nudata[i] = (unsigned char) (unit*floor(((*p)[i] - p->minimum())*scale));
337 
338  p->data_type(UCharacter);
339 
340  p->data_assign((unsigned char *) nudata);
341 
342  p->statistics();
343 
344  return 0;
345 }
unsigned long images()
Definition: Bimage.h:402
void sizeX(unsigned long nx)
Definition: Bimage.h:332
int img_rescale_to_min_max(Bimage *p, double min, double max)
Rescales the image data to a given minimum and maximum.
Definition: img_rescale.cpp:79
int img_truncate_to_avg(Bimage *p, double min, double max)
Sets voxels in image data exceeding a given minimum and maximum to the average.
Header file for general utilities.
int img_truncate_to_background(Bimage *p, double min, double max)
Sets voxels in image data exceeding a given minimum and maximum to the image background.
double background()
Definition: Bimage.h:123
Header file for image utilities.
Class for complex numbers.
Generalized matrix class.
#define VERB_PROCESS
Definition: utilities.h:99
double average(unsigned long cc, double xf, double yf, double zf, unsigned long nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1119
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.cpp:2403
unsigned long channels()
Definition: Bimage.h:327
double data_type_max()
Get the maximum of a datatype.
Definition: Bimage.cpp:1331
General image parameter class.
Definition: Bimage.h:149
double maximum()
Definition: Bimage.h:420
void set(unsigned long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:885
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:545
int img_truncate(Bimage *p, double min, double max, double setmin, double setmax)
Truncates image data to a given minimum and maximum.
void file_name(Bstring s)
Definition: Bimage.h:209
double standard_deviation()
Definition: Bimage.h:422
unsigned char * data_pointer()
Definition: Bimage.h:225
unsigned long data_size()
Definition: Bimage.h:219
int img_rescale_to_avg_std(Bimage *p, double avg, double std)
Rescales the image data to a given average and standard deviation.
void sizeZ(unsigned long nz)
Definition: Bimage.h:334
int verbose
Header file for 2D and 3D image I/O.
int img_limit_levels(Bimage *p, int nlevels)
Converts a full gray scale image to a limited level image.
int img_truncate_to_min_max(Bimage *p, double min, double max)
Truncates image data to a given minimum and maximum.
int img_rescale(Bimage *p, double scale, double shift)
Rescales the image data to a given minimum and maximum.
Definition: img_rescale.cpp:31
void sizeY(unsigned long ny)
Definition: Bimage.h:333
#define VERB_LABEL
Definition: utilities.h:98
DataType data_type()
Definition: Bimage.h:290
double minimum()
Definition: Bimage.h:419
double data_type_min()
Get the minimum of a datatype.
Definition: Bimage.cpp:1301
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bsub_image * image
Definition: Bimage.h:180