| Bsoft 2.0.5 Bernard's software package

beditimg.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 /* Usage assistance */
18 const char* use[] = {
19 " ",
20 "Usage: beditimg [options] [input.img] output.img",
21 "------------------------------------------------",
22 "Creates a new image or modifies an input image by inserting or deleting regions.",
23 " ",
24 "Actions:",
25 "-create 20,60,30 Create a new image of this size (pixels/voxels).",
26 " Note: No input image is read and the first file name is taken as the output image.",
27 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
28 "-box 0,3,44,50,30,44 Start (lower-left-back voxel) and size of box (voxels).",
29 "-sphere 22,5,39,120 Center and radius from center of sphere (voxels).",
30 "-shell 32,45,23,20,120 Center and inner and outer radii of shell (voxels).",
31 "-cylinder 41,76,9,12,23 Center, radius and height of cylinder (voxels).",
32 "-gaussian 45,55,12,14.5 Center and sigma of gaussian sphere (voxels).",
33 "-quadratic 1,2,1,2,1,2,1 Create a quadratic image (7 parameters).",
34 "-gradient quad Correct for the gradient in an image: linear or quadratic.",
35 "-tetrahedron 9,-3,4,... Extract a tetrahedron defined by four sets of 3-valued vectors.",
36 " ",
37 "Parameters:",
38 "-verbose 7 Verbosity of output.",
39 "-datatype u Force writing of a new data type.",
40 "-origin 60.4,-32.2,44 Origin of image (voxels).",
41 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
42 "-images 12 Number of images (default: 1).",
43 "-fill 0.02 Fill value: average (default), background, or value.",
44 "-background -1.2 Fill a newly created image with a background value.",
45 "-edgewidth 10 Gaussian width of edge of region to delete (voxels, <0 inverts selection).",
46 "-wrap Turn wrapping on (default off).",
47 " ",
48 "Parameters for placing an image (use with -place):",
49 "-location 33,12,56 Location to place an input image.",
50 "-rotate 0,0.4,0.7,52 Rotation applied before placing an input image.",
51 " ",
52 "Input:",
53 "-place img.pif An image to place within a larger image.",
54 " ",
55 NULL
56 };
57
58 int main(int argc, char* argv[])
59 {
60 // Initialize variables
61 DataType nudatatype(Unknown_Type); // Conversion to new type
62 Vector3<double> sam; // Units for the three axes (A/pixel)
63 int set_origin(0);
64 Vector3<double> origin; // Origin in file
65 Vector3<long> nusize; // Size of new file
66 long nimg(1); // Number of images to create
67 double nuavg(0), nustd(0); // For rescaling
68 int fill_type(FILL_AVERAGE); // Use average
69 double fill(0); // Default fill is average
70 double background(0); // Default background is zero
71 Vector3<double> boxstart; // Start of box to delete
72 Vector3<long> boxsize; // Size of box to delete
73 Vector3<double> spherecenter; // Sphere center
74 double sphereradius(0); // Sphere radius
75 Vector3<double> shellcenter; // Shell center
76 double shellminrad(0); // Shell minimum radius
77 double shellmaxrad(0); // Shell maximum radius
78 Vector3<double> cylcenter; // Cylinder center
79 double cylradius(0); // Cylinder radius
80 double cylheight(0); // Cylinder height
81 double sigma(0); // Gaussian sphere width
82 double quad[7] = {0,0,0,0,0,0,0}; // Quadratic parameters
83 double width(0); // Gaussian width minimum = sharp edge
84 int correct_gradient(0); // Linear gradient correction flag
85 int tetrahedron(0); // Flag to extract a tetrahedron
86 Vector3<double> tet[4]; // Tetrahedron
87 int setwrap(0); // Wrapping flag
88 Vector3<double> loc; // Location for image to place
89 double angle(0); // Rotation angle
90 Vector3<double> axis(0.0,0.0,1.0); // Rotation axis
91 Bstring imgfile; // Image to place
92
93 int optind;
94 Boption* option = get_option_list(use, argc, argv, optind);
95 Boption* curropt;
96 for ( curropt = option; curropt; curropt = curropt->next ) {
97 long i, j;
98 vector<double> d = curropt->value.split_into_doubles(",");
99 if ( curropt->tag == "create" )
100 nusize = curropt->size();
101 if ( curropt->tag == "datatype" )
102 nudatatype = curropt->datatype();
103 if ( curropt->tag == "sampling" )
104 sam = curropt->sampling();
105 if ( curropt->tag == "images" )
106 if ( ( nimg = curropt->value.integer() ) < 1 )
107 cerr << "-images: The number of images must be specified!" << endl;
108 if ( curropt->tag == "rescale" )
109 if ( curropt->values(nuavg, nustd) < 2 )
110 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
111 if ( curropt->tag == "fill" )
112 fill = curropt->fill(fill_type);
113 if ( curropt->tag == "background" )
114 if ( ( background = curropt->value.real() ) == 0 )
115 cerr << "-background: A background value must be specified!" << endl;
116 if ( curropt->tag == "box" )
117 if ( curropt->box(boxstart, boxsize) < 6 )
118 cerr << "-box: A position and size must be specified!" << endl;
119 if ( curropt->tag == "edgewidth" ) {
120 width = curropt->value.real();
121 if ( fabs(width) < 0.001 )
122 cerr << "-edgewidth: An edge width must be specified!" << endl;
123 }
124 if ( curropt->tag == "sphere" )
125 if ( curropt->values(spherecenter[0],
126 spherecenter[1], spherecenter[2], sphereradius) < 4 )
127 cerr << "-sphere: A position and radius must be specified!" << endl;
128 if ( curropt->tag == "shell" ) {
129 if ( d.size() < 5 )
130 cerr << "-shell: A position and two radii must be specified!" << endl;
131 else {
132 for ( i=0; i<3; i++ ) shellcenter[i] = d[i];
133 shellminrad = d[3];
134 shellmaxrad = d[4];
135 }
136 }
137 if ( curropt->tag == "cylinder" ) {
138 if ( d.size() < 5 )
139 cerr << "-cylinder: A position, radius and height must be specified!" << endl;
140 else {
141 for ( i=0; i<3; i++ ) cylcenter[i] = d[i];
142 cylradius = d[3];
143 cylheight = d[4];
144 }
145 }
146 if ( curropt->tag == "gaussian" ) {
147 if ( d.size() < 4 )
148 cerr << "-gaussian: 4 parameters must be specified!" << endl;
149 else {
150 for ( i=0; i<3; i++ ) spherecenter[i] = d[i];
151 sigma = d[3];
152 }
153 }
154 if ( curropt->tag == "quadratic" ) {
155 if ( d.size() < 7 )
156 cerr << "-quadratic: 7 parameters must be specified!" << endl;
157 else
158 for ( size_t i=0; i<d.size(); i++ ) quad[i] = d[i];
159 }
160 if ( curropt->tag == "gradient" ) {
161 if ( curropt->value[0] == 'q' ) correct_gradient = 2;
162 else correct_gradient = 1;
163 }
164 if ( curropt->tag == "tetrahedron" ) {
165 if ( d.size() < 12 )
166 cerr << "-tetrahedron: Twelve values must be specified!" << endl;
167 else {
168 for ( i=0; i<4; i++ )
169 for ( j=0; j<3; j++ )
170 tet[i][j] = d[i];
171 tetrahedron = 1;
172 }
173 }
174 if ( curropt->tag == "origin" ) {
175 if ( curropt->value[0] == 'c' ) {
176 set_origin = 2;
177 } else {
178 origin = curropt->origin();
179 set_origin = 1;
180 }
181 }
182 if ( curropt->tag == "wrap" )
183 setwrap = 1;
184 if ( curropt->tag == "location" )
185 if ( curropt->values(loc[0], loc[1], loc[2]) < 3 )
186 cerr << "-location: All three location coordinates must be specified!" << endl;
187 if ( curropt->tag == "rotate" ) {
188 if ( curropt->values(axis[0], axis[1], axis[2], angle) < 4 )
189 cerr << "-rotate: All three vector elements and an angle must be specified!" << endl;
190 else
191 angle *= M_PI/180.0;
192 }
193 if ( curropt->tag == "place" )
194 imgfile = curropt->filename();
195 }
196 option_kill(option);
197
198 double ti = timer_start();
199
200 Bimage* p = NULL;
201 if ( nusize[0] > 0 && nusize[1] > 0 && nusize[2] > 0 ) {
202 if ( nudatatype == Unknown_Type ) nudatatype = Float;
203 p = new Bimage(nudatatype, TSimple, nusize, 1);
204 if ( background ) p->fill(background);
205 } else {
206 p = read_img(argv[optind++], 1, -1);
207 if ( !p ) {
208 cerr << "Error: No input file read or output size specified!" << endl;
209 bexit(-1);
210 }
211 if ( nudatatype == Unknown_Type ) nudatatype = p->data_type(); // Preserve the old type
212 }
213
214 if ( fill_type == FILL_AVERAGE ) fill = p->average();
215 if ( fill_type == FILL_BACKGROUND ) fill = p->background(long(0));
216
217 if ( set_origin ) {
218 if ( set_origin == 2 ) p->origin(p->size()/2);
219 else p->origin(origin);
220 }
221
222 if ( sam.volume() ) p->sampling(sam);
223
224 if ( boxsize.volume() > 0 )
225 p->shape(0, boxsize, boxstart, width, fill_type, fill);
226
227 if ( sphereradius )
228 p->sphere(spherecenter, sphereradius, width, fill_type, fill);
229
230 if ( sigma > 0 )
231 p->gaussian_sphere(0, spherecenter, sigma, fill);
232
233 if ( shellmaxrad > 0 ) {
234 if ( setwrap )
235 p->shell_wrap(0, shellcenter, shellminrad, shellmaxrad, width, fill_type, fill);
236 else
237 p->shell(0, shellcenter, shellminrad, shellmaxrad, width, fill_type, fill);
238 }
239
240 if ( cylradius && cylheight )
241 p->cylinder(cylcenter, cylradius, cylheight, width, fill_type, fill);
242
243 if ( quad[6] )
244 p->quadric(quad);
245
246 if ( imgfile.length() ) {
247 Bimage* pp = read_img(imgfile, 1, 0);
248 if ( angle ) pp->rotate(axis, angle);
249 p->place(0, pp, loc, 1e30, 1, 0, 0);
250 delete pp;
251 }
252
253 if ( tetrahedron ) {
254 Bimage* pnu = p->extract_tetrahedron(tet, fill_type, fill);
255 delete p;
256 p = pnu;
257 }
258
259 if ( correct_gradient == 1 ) p->gradient_correction();
260 else if ( correct_gradient == 2 ) p->quadric_correct(p->quadric_fit());
261
262 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
263
264 if ( nimg > 1 ) {
265 Bimage* pm = p->copy(nimg);
266 for ( long n=1; n<nimg; ++n ) pm->replace(n, p);
267 delete p;
268 p = pm;
269 }
270
271 // Write an output file if a file name is given
272 if ( optind < argc ) {
273 p->change_type(nudatatype);
274 p->label(get_cmd_line(argc, argv));
275 write_img(argv[optind], p, 0);
276 }
277
278 delete p;
279
280 if ( verbose & VERB_TIME )
281 timer_report(ti);
282
283 bexit(0);
284 }
285
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::shell_wrap
int shell_wrap(long nn, Vector3< double > center, double minrad, double maxrad, double width, int fill_type=0, double fill=0)
Fills a shell within an image with a uniform value with wrapping.
Definition: Bimage_edit.cpp:654
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2828
Bstring::real
double real()
Definition: Bstring.cpp:545
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bimage::quadric_fit
vector< double > quadric_fit()
Fits the whole image to a quadric surface.
Definition: Bimage_rescale.cpp:862
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
main
int main(int argc, char *argv[])
Definition: beditimg.cpp:58
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
Bimage::shape
int shape(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Creates a shape in an image and fills it with a constant value.
Definition: Bimage_edit.cpp:103
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:173
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::place
int place(long nn, Bimage *p, Vector3< double > loc, double radius=0, double scale=1, double shift=0, int operation=0)
Places a small image into a large image.
Definition: Bimage_combine.cpp:178
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
Boption::filename
Bstring filename()
Definition: options.h:85
TSimple
Definition: Bimage.h:58
timer.h
Utilities for timing functions.
Float
Definition: utilities.h:157
Bimage::cylinder
int cylinder(Vector3< double > center, double radius, double height, double width, int fill_type=0, double fill=0)
Fills a cylinder within an image with a uniform value.
Definition: Bimage_edit.cpp:465
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
Boption::box
long box(Vector3< T1 > &st, Vector3< T2 > &sz)
Definition: options.h:214
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:438
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
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
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:665
Bimage::extract_tetrahedron
Bimage * extract_tetrahedron(Vector3< double > *tet, int fill_type=0, double fill=0)
Extracts a tetrahedral part of the image.
Definition: Bimage_extract.cpp:546
Bimage::quadric
int quadric(double *param)
Generates a quadric surface over the whole image.
Definition: Bimage_edit.cpp:819
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::shell
int shell(Vector3< double > center, double minrad, double maxrad, double width, int fill_type=0, double fill=0)
Fills a shell within an image with a uniform value.
Definition: Bimage_edit.cpp:544
Boption::next
Boption * next
Definition: options.h:28
verbose
int verbose
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::gradient_correction
int gradient_correction()
Calculates and corrects for a linear gradient across an image.
Definition: Bimage_rescale.cpp:752
Vector3< double >
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
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:253
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::gaussian_sphere
int gaussian_sphere(long nn, Vector3< double > center, double sigma, double amp)
Fills a gaussian sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:492
Bimage::fill
void fill(double v)
Definition: Bimage.h:698
Bimage::quadric_correct
int quadric_correct(vector< double > param)
Corrects for a quadric surface.
Definition: Bimage_rescale.cpp:826
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
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
use
const char * use[]
Definition: beditimg.cpp:18
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