| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_random.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "img_combine.h"
11 #include "img_fourier.h"
12 #include "random_numbers.h"
13 #include "utilities.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
38 Bimage* img_random_uniform(int nimg, Vector3<int> size, double min, double max)
39 {
40 if ( size.volume() < 1 ) return NULL;
41
42 size_t datasize = (size_t) (nimg*size.volume());
43 float* data = random_array_uniform(datasize, min, max);
44
45 // The image structure
46 Bimage* p = new Bimage(Float, TSimple, size, nimg, (unsigned char *) data);
47
48 p->statistics();
49
50 if ( verbose & VERB_PROCESS ) {
51 cout << "Generating a random image with a uniform distribution:" << endl;
52 cout << "Minimum and maximum: " <<
53 p->minimum() << " " << p->maximum() << endl;
54 cout << "Size: " <<
55 p->channels() << " " << p->size() << " " << p->images() << endl << endl;
56 } else if ( verbose & VERB_LABEL )
57 cout << "Generating a random image with a uniform distribution" << endl << endl;
58
59 return p;
60 }
61
80 Bimage* img_random_gaussian(int nimg, Vector3<int> size, double avg, double std)
81 {
82 if ( size.volume() < 1 ) return NULL;
83
84 size_t datasize = (size_t) (nimg*size.volume());
85 float* data = random_array_gaussian(datasize, avg, std);
86
87 // The image structure
88 Bimage* p = new Bimage(Float, TSimple, size, nimg, (unsigned char *) data);
89
90 // Initialize variables
91 p->statistics();
92
93 if ( verbose & VERB_PROCESS ) {
94 cout << "Generating a random image with a gaussian distribution:" << endl;
95 cout << "Avg and std: " <<
96 p->average() << " " << p->standard_deviation() << endl;
97 cout << "Size: " <<
98 p->channels() << " " << p->size() << " " << p->images() << endl << endl;
99 } else if ( verbose & VERB_LABEL )
100 cout << "Generating a random image with a gaussian distribution" << endl << endl;
101
102 return p;
103 }
104
127 Bimage* img_random_poisson(int nimg, Vector3<int> size, double avg)
128 {
129 if ( avg <= 0 ) {
130 error_show("Error in img_random_poisson: The average for a Poisson distribution must be > 0!", __FILE__, __LINE__);
131 return NULL;
132 }
133
134 if ( size.volume() < 1 ) return NULL;
135
136 size_t datasize = (size_t) (nimg*size.volume());
137 float* data = random_array_poisson(datasize, avg);
138
139 // The image structure
140 Bimage* p = new Bimage(Float, TSimple, size, nimg, (unsigned char *) data);
141
142 p->statistics();
143
144 if ( verbose & VERB_PROCESS ) {
145 cout << "Generating a random image with a poisson distribution:" << endl;
146 cout << "Avg and std: " <<
147 p->average() << " " << p->standard_deviation() << endl;
148 cout << "Size: " <<
149 p->channels() << " " << p->size() << " " << p->images() << endl << endl;
150 } else if ( verbose & VERB_LABEL )
151 cout << "Generating a random image with a poisson distribution" << endl << endl;
152
153 return p;
154 }
155
175 Bimage* img_random_logistical(int nimg, Vector3<int> size, double avg, double std)
176 {
177 if ( size.volume() < 1 ) return NULL;
178
179 size_t datasize = (size_t) (nimg*size.volume());
180 float* data = random_array_logistical(datasize, avg, std);
181
182 // The image structure
183 Bimage* p = new Bimage(Float, TSimple, size, nimg, (unsigned char *) data);
184
185 p->statistics();
186
187 if ( verbose & VERB_PROCESS ) {
188 cout << "Generating a random image with a logistical differential distribution:" << endl;
189 cout << "Avg and std: " <<
190 p->average() << " " << p->standard_deviation() << endl;
191 cout << "Size: " <<
192 p->channels() << " " << p->size() << " " << p->images() << endl << endl;
193 } else if ( verbose & VERB_LABEL )
194 cout << "Generating a random image with a logistical differential distribution" << endl << endl;
195
196 return p;
197 }
198
210 Bimage* img_spectral_noise(int nimg, Vector3<int> size, double alpha)
211 {
212 if ( size.volume() < 1 ) return NULL;
213
214 // The image structure
215 Bimage* p = new Bimage(Float, TComplex, size.x(), size.y(), size.z(), nimg);
216
217 unsigned long i, j, n, x, y, z, imgsize = (unsigned long) size.volume();
218 unsigned long datasize = p->data_size();
219 double x2, y2, z2, s2, w, sum;
220 double fac(-alpha/4); // Factor of 2 because we work with s2, and another 2 for amplitude scaling
221
222 if ( verbose & VERB_PROCESS ) {
223 cout << "Generating a noise image:" << endl;
224 cout << "Size: " << p->channels() << " " << p->size() << " " << p->images() << endl;
225 cout << "Alpha: " << alpha << endl << endl;
226 } else if ( verbose & VERB_LABEL )
227 cout << "Generating a noise image" << endl;
228
229 float* phi = random_array_uniform(datasize, -1, 1);
230 if ( verbose & VERB_DEBUG )
231 cout << "DEBUG img_noise: random array size " << datasize << endl;
232
233 for ( i=1; i<datasize; i++ ) p->set(i, phi[i]);
234 delete[] phi;
235
236 for ( i=n=0; n<p->images(); n++ ) {
237 sum = 0;
238 for ( z=0; z<p->sizeZ(); z++ ) {
239 z2 = z*1.0L/p->sizeZ();
240 if ( z2 >= 0.5 ) z2 -= 1;
241 z2 *= z2;
242 for ( y=0; y<p->sizeY(); y++ ) {
243 y2 = y*1.0L/p->sizeY();
244 if ( y2 >= 0.5 ) y2 -= 1;
245 y2 *= y2;
246 for ( x=0; x<p->sizeX(); x++, i++ ) {
247 x2 = x*1.0L/p->sizeX();
248 if ( x2 >= 0.5 ) x2 -= 1;
249 x2 *= x2;
250 s2 = x2 + y2 + z2;
251 if ( s2 ) {
252 w = pow(s2, fac);
253 p->set(i, p->complex(i)*w);
254 sum += w*w;
255 }
256 }
257 }
258 }
259 sum = sqrt(sum/(3*imgsize));
260 for ( j=n*imgsize; j<(n+1)*imgsize; j++ ) p->set(j, p->complex(j)/sum);
261 }
262
263 p->fft_back();
264
265 p->statistics();
266
267 return(p);
268 }
269
284 int img_add_gaussian_noise(Bimage* p, double snr)
285 {
286 if ( snr <= 0 || snr > 1000 ) return 0;
287
288 if ( verbose & (VERB_LABEL | VERB_PROCESS ) )
289 cout << "Adding Gaussian noise to an image with SNR = " << snr << endl << endl;
290
291 p->change_type(Float);
292
293 int nimg = p->images();
294 double avg = 0;
295 double std = p->standard_deviation()/sqrt(snr);
296 Vector3<int> size(p->size());
297
298 Bimage* pnoise = img_random_gaussian(nimg, size, avg, std);
299
300 // img_add(p, pnoise, 1, 0);
301 p->add(pnoise);
302
303 delete pnoise;
304
305 p->statistics();
306
307 return 0;
308 }
309
324 int img_add_poisson_noise(Bimage* p)
325 {
326 if ( verbose & (VERB_LABEL | VERB_PROCESS ) )
327 cout << "Adding Poisson noise to an image" << endl << endl;
328
329 p->change_type(Float);
330
331 unsigned long i, nzero = 0, datasize = p->data_size();
332
333 for ( i=0; i<datasize; i++ ) {
334 if ( (*p)[i] <= 0 ) nzero++;
335 p->set(i, random_poisson((*p)[i]));
336 }
337
338 if ( verbose & VERB_PROCESS )
339 cout << "Number of zero voxels: " << nzero << " (" << nzero*100.0/datasize << " %)" << endl;
340
341 p->statistics();
342
343 return 0;
344 }
345
346
347
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1440
Vector3::z
Type z()
Definition: Vector3.h:141
Bimage::sizeZ
void sizeZ(size_t nz)
Definition: Bimage.h:321
Bimage::fft_back
int fft_back()
Definition: Bimage.h:573
utilities.h
Header file for general utilities.
img_add_poisson_noise
int img_add_poisson_noise(Bimage *p)
Adds poisson random noise to an image.
Definition: img_random.cpp:324
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:104
img_spectral_noise
Bimage * img_spectral_noise(int nimg, Vector3< int > size, double alpha)
Generates a noise map with a defined spectral decay.
Definition: img_random.cpp:210
Float
Definition: Bimage.h:59
Vector3< int >
error_show
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:616
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:99
std
TComplex
Definition: Bimage.h:71
Bimage::average
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1150
Bimage::statistics
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
TSimple
Definition: Bimage.h:70
Bimage::size
Vector3< int > size()
Definition: Bimage.h:316
Bimage
General image parameter class.
Definition: Bimage.h:152
Bimage::maximum
double maximum()
Definition: Bimage.h:419
Bimage::complex
Complex< double > complex(size_t j)
Returns a complex value at the given index.
Definition: Bimage.cpp:824
random_array_poisson
float * random_array_poisson(int n, double avg)
Generates a series with a poisson random distribution of values.
Definition: random_numbers.cpp:297
Bimage::channels
size_t channels()
Definition: Bimage.h:314
random_array_logistical
float * random_array_logistical(long n, double avg, double std)
Generates an array with a logistical random distribution.
Definition: random_numbers.cpp:380
img_fourier.h
General FFT for n-dimensional data.
img_combine.h
Header file for combining two images in various ways.
Vector3::x
Type x()
Definition: Vector3.h:139
Bimage::add
void add(size_t j, double v)
Definition: Bimage.h:264
img_random_uniform
Bimage * img_random_uniform(int nimg, Vector3< int > size, double min, double max)
Generates an image with a uniform random distribution of densities.
Definition: img_random.cpp:38
Bimage::set
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:916
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:421
Bimage::images
size_t images()
Definition: Bimage.h:401
random_numbers.h
verbose
int verbose
Bimage::data_size
size_t data_size()
Definition: Bimage.h:224
Bimage::sizeX
void sizeX(size_t nx)
Definition: Bimage.h:319
Bimage::sizeY
void sizeY(size_t ny)
Definition: Bimage.h:320
img_random_poisson
Bimage * img_random_poisson(int nimg, Vector3< int > size, double avg)
Generates an image with a poisson random distribution of densities.
Definition: img_random.cpp:127
data
JSAMPIMAGE data
Definition: jpeglib.h:945
img_random_gaussian
Bimage * img_random_gaussian(int nimg, Vector3< int > size, double avg, double std)
Generates an image with a gaussian random distribution of densities.
Definition: img_random.cpp:80
img_add_gaussian_noise
int img_add_gaussian_noise(Bimage *p, double snr)
Adds gaussian random noise to an image.
Definition: img_random.cpp:284
rwimg.h
Header file for 2D and 3D image I/O.
random_array_uniform
float * random_array_uniform(long n, double min, double max)
Generates a series with a uniform random distribution.
Definition: random_numbers.cpp:75
random_poisson
double random_poisson(double avg)
Generates a value deviating from the average based on a poisson distribution.
Definition: random_numbers.cpp:220
img_random_logistical
Bimage * img_random_logistical(int nimg, Vector3< int > size, double avg, double std)
Generates an image with a gaussian random distribution of densities.
Definition: img_random.cpp:175
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:98
Vector3::volume
double volume()
Definition: Vector3.h:207
Vector3::y
Type y()
Definition: Vector3.h:140
Bimage::minimum
double minimum()
Definition: Bimage.h:418
random_array_gaussian
float * random_array_gaussian(long n, double avg, double std)
Generates a series with a gaussian random distribution of values.
Definition: random_numbers.cpp:171