| Bsoft 2.0.5 Bernard's software package

brandom.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "random_numbers.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: brandom [options] [input.img] output.img",
22 "-----------------------------------------------",
23 "Generates random content images.",
24 "If an input image is not found a random output image is generated.",
25 "The default output data type is floating point.",
26 " ",
27 "Actions:",
28 "-type gauss Distribution type (default: uniform, ",
29 " others: gaussian, poisson, logistical, spectral).",
30 "-rescale -0.1,5.2 Rescale output data to average and standard deviation.",
31 " ",
32 "Parameters:",
33 "-verbose 7 Verbosity of output.",
34 "-datatype u Data type (default: byte).",
35 "-images 12 Number of images (default: 1).",
36 "-size 10,50,8 Size (default: 256,256,1).",
37 "-sampling 1.5,1.5,1.5 Sampling (default: 1,1,1; a single value can be given).",
38 "-origin 0,-10,30 Set the origin (default image origin).",
39 "-minmax -1.2,5.6 Set minimum and maximum of input and uniform random image (default: -1.732,1.732).",
40 "-avgstd 0.5,2.3 Set average and standard deviation of input and non-uniform random image (default: 0,1).",
41 "-alpha 2 Spectral noise decay (default: white=0, pink=1, red/brown=2, blue=-1, violet=-2).",
42 "-snr 1.4,63 Signal-to-noise ratio and foreground radius (default: 1).",
43 " ",
44 NULL
45 };
46
47 int main(int argc, char **argv)
48 {
49 // Initialize variables
50 DataType nudatatype(Unknown_Type); // Data type to be set
51 Vector3<long> size(256,256,1);
52 int set_origin(0); // Flag to set origin
53 Vector3<double> origin; // Origin
54 int set_sampling(0);
55 Vector3<double> sampling(1,1,1); // Units for the three axes (A/pixel)
56 long nimg(1); // Number of images
57 double min(-sqrt(3.0)), max(sqrt(3.0));
58 double avg(0), std(1); // Rescaling input or random image to average and stdev
59 int set_avgstd(0); // Flag to set average and standard deviation
60 double alpha(0); // Spectral noise decay
61 double finalavg(0), finalstd(0); // Rescaling output to average and stdev
62 double snr(1); // Signal-to-noise ratio
63 double foreground_radius(0); // Foreground radius
64 int rand_type(0); // Default uniform distribution
65
66 int optind;
67 Boption* option = get_option_list(use, argc, argv, optind);
68 Boption* curropt;
69 for ( curropt = option; curropt; curropt = curropt->next ) {
70 if ( curropt->tag == "datatype" )
71 nudatatype = curropt->datatype();
72 if ( curropt->tag == "origin" ) {
73 if ( curropt->value[0] == 'c' ) {
74 set_origin = 2;
75 } else {
76 origin = curropt->origin();
77 set_origin = 1;
78 }
79 }
80 if ( curropt->tag == "sampling" ) {
81 sampling = curropt->sampling();
82 set_sampling = 1;
83 }
84 if ( curropt->tag == "type" ) {
85 if ( curropt->value[0] == 'u' ) rand_type = 0;
86 else if ( curropt->value[0] == 'g' ) rand_type = 1;
87 else if ( curropt->value[0] == 'p' ) rand_type = 2;
88 else if ( curropt->value[0] == 'l' ) rand_type = 3;
89 else if ( curropt->value[0] == 's' ) rand_type = 4;
90 else
91 cerr << "-type: Type " << curropt->value << " not supported! Default to uniform distribution" << endl;
92 }
93 if ( curropt->tag == "minmax" ) {
94 if ( curropt->values(min, max) < 2 )
95 cerr << "-minmax: Both min and max must be specified!" << endl;
96 if ( min > max ) swap(min, max);
97 if ( min == max ) {
98 min -= 5;
99 max += 5;
100 }
101 }
102 if ( curropt->tag == "avgstd" ) {
103 if ( curropt->values(avg, std) < 1 )
104 cerr << "-avgstd: At least the average must be specified!" << endl;
105 if ( std < 0 ) std = -std;
106 if ( std == 0 ) std = 1;
107 set_avgstd = 1;
108 }
109 if ( curropt->tag == "alpha" )
110 if ( ( alpha = curropt->value.real() ) < 0 )
111 cerr << "-alpha: A positive spectral decay must be specified!" << endl;
112 if ( curropt->tag == "rescale" )
113 if ( curropt->values(finalavg, finalstd) < 2 )
114 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
115 if ( curropt->tag == "images" )
116 if ( ( nimg = curropt->value.integer() ) < 1 )
117 cerr << "-images: The number of images must be specified!" << endl;
118 if ( curropt->tag == "size" )
119 size = curropt->size();
120 if ( curropt->tag == "snr" ) {
121 if ( curropt->values(snr, foreground_radius) < 1 )
122 cerr << "-snr: A value must be specified!" << endl;
123 else
124 if ( snr <= 0 ) snr = 1;
125 }
126 }
127 option_kill(option);
128
129 if ( verbose & VERB_FULL )
130 cout << "Random number range: " << get_rand_max() << endl;
131
132 double ti = timer_start();
133
134 double scale, fg = 1e30;
135 Bimage* p = NULL;
136 Bimage* pran = NULL;
137
138 if ( optind < argc - 1 ) {
139 p = read_img(argv[optind++], 1, -1);
140 p->change_type(Float);
141 if ( rand_type > 0 ) {
142 if ( set_avgstd ) p->rescale_to_avg_std(avg, std);
143 } else
144 p->rescale_to_min_max(min, max);
145 }
146
147 if ( nudatatype == Unknown_Type ) nudatatype = Float;
148
149 if ( p ) {
150 p->change_type(Float);
151 nimg = p->images();
152 size = p->size();
153 if ( !set_avgstd ) {
154 avg = p->average();
155 std = p->standard_deviation();
156 }
157 } else {
158 if ( size[0] < 1 ) size[0] = 256;
159 if ( size[1] < 1 ) size[1] = 256;
160 if ( size[2] < 1 ) size[2] = 1;
161 }
162
163 pran = new Bimage(Float, TSimple, size, nimg);
164 switch ( rand_type ) {
165 case 1:
166 pran->noise_gaussian(avg, std);
167 break;
168 case 2:
169 pran->noise_poisson(avg);
170 break;
171 case 3:
172 pran->noise_logistical(avg, std);
173 break;
174 case 4:
175 pran->noise_spectral(alpha);
176 break;
177 default:
178 pran->noise_uniform(min, max);
179 break;
180 }
181
182 if ( p ) {
183 if ( foreground_radius ) {
184 if ( p->sizeZ() < 2 ) fg = M_PI*foreground_radius*foreground_radius;
185 else fg = (M_PI*4.0/3.0)*foreground_radius*foreground_radius*foreground_radius;
186 }
187 if ( fg > size.volume() ) fg = size.volume();
188 scale = sqrt(size.volume()/(snr*fg));
189 p->add(pran, scale, -avg);
190 delete pran;
191 pran = p;
192 pran->statistics();
193 if ( finalstd ) pran->rescale_to_avg_std(finalavg, finalstd);
194 }
195
196 pran->label(get_cmd_line(argc, argv));
197 pran->change_type(nudatatype);
198
199 if ( set_sampling ) pran->sampling(sampling);
200
201 if ( set_origin ) {
202 if ( set_origin == 2 ) pran->origin(pran->size()/2);
203 else pran->origin(origin);
204 }
205
206 if ( optind < argc )
207 write_img(argv[optind], pran, 0);
208 else
209 write_img("temp.pif", pran, 0);
210
211 delete pran;
212
213 if ( verbose & VERB_TIME )
214 timer_report(ti);
215
216 bexit(0);
217 }
218
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::rescale_to_min_max
int rescale_to_min_max(double numin, double numax)
Rescales the image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:100
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::add
void add(long j, double v)
Definition: Bimage.h:308
Bimage::noise_logistical
int noise_logistical(double ravg, double rstd)
Generates an image with a gaussian random distribution of densities.
Definition: Bimage_noise.cpp:156
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Bimage::noise_gaussian
int noise_gaussian(double ravg=0, double rstd=1)
Generates an image with a gaussian random distribution of densities.
Definition: Bimage_noise.cpp:72
TSimple
Definition: Bimage.h:58
timer.h
Utilities for timing functions.
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: brandom.cpp:19
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
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
main
int main(int argc, char **argv)
Definition: brandom.cpp:47
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::noise_uniform
int noise_uniform(double rmin, double rmax)
Generates an image with a uniform random distribution of densities.
Definition: Bimage_noise.cpp:34
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bimage::noise_poisson
int noise_poisson(double ravg)
Generates an image with a poisson random distribution of densities.
Definition: Bimage_noise.cpp:115
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
random_numbers.h
Header file for functions for creating random images.
Bimage::noise_spectral
int noise_spectral(double alpha)
Generates a noise map with a defined spectral decay.
Definition: Bimage_noise.cpp:184
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< long >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
verbose
int verbose
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
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
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
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
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563