Bsoft  2.1.0
Bernard's software package
bop.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: bop [options] file1.img file2.img outfile.img",
21 "----------------------------------------------------",
22 "Binary operations for 2D and 3D images.",
23 "Only one operation can be done at a time.",
24 " ",
25 "Actions:",
26 "-slices Interpret multiple 2D images as z-slices of a single 3D image",
27 "-images Interpret slices of a single 3D image as 2D images",
28 "-add 1.4,3.6 Add two images, scaling and shifting the second.",
29 "-multiply -5.1,0.5 Multiply two images, scaling and shifting the second.",
30 "-divide -1.8,-2.4 Divide the first image by the second, scaling and shifting the second.",
31 "-largest 3.1,0.5 Maximum of two images, scaling and shifting the second.",
32 "-smallest 3.1,0.5 Minimum of two images, scaling and shifting the second.",
33 "-truncate -0.3,10 Truncate result to min and max.",
34 "-variance 0.15,-0.5 Scale based on variance ratio, optionally shifting the second.",
35 "-fit 6 Linear fit, excluding a percentage of voxels as outliers, output difference.",
36 "-coefficient Calculates correlation coefficients between two sets of images.",
37 "-correlate corr.img Correlate two images and generate a correlation image.",
38 "-Histomatch 20 Match histogram of image 2 to image 1, output modified image 2.",
39 "-blend 7 Blend from image 1 to image 2, output a new multi-image file.",
40 " ",
41 "Parameters:",
42 "-verbose 7 Verbosity of output.",
43 "-datatype u Force writing of a new data type.",
44 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
45 "-radius minr,maxr Correlate or fit only between min. and max. radius (pixel).",
46 " ",
47 "Input:",
48 "-Mask mask.tif Mask file to use for limiting comparisons to a defined region.",
49 " ",
50 NULL
51 };
52 
53 int main(int argc, char* argv[])
54 {
55  // Initialize all settings
56  DataType nudatatype(Unknown_Type); // Conversion to new type
57  Vector3<double> sampling; // Units for the three axes (A/pixel)
58  int znswitch(0); // 0=not, 1=n2z, 2=z2n
59  int setadd(0);
60  int setmultiply(0);
61  int setdivide(0);
62  int setlargest(0);
63  int setsmallest(0);
64  int setvar(0);
65  int setlinear(0);
66  double excl_voxels(0); // Percentage voxels to excl_voxels from linear fit
67  int setcc(0);
68  int radiusflag(0);
69  int setminmax(0);
70  int sethisto(0), bins(0);
71  double scale(1);
72  double shift(0);
73 // double threshold(0);
74 // double fill(0);
75  double min(0), max(0);
76  double minr(0), maxr(0);
77  int blend_number(0);
78  Bstring corrfile;
79  Bstring maskfile; // Mask file name
80 
81  int optind;
82  Boption* option = get_option_list(use, argc, argv, optind);
83  Boption* curropt;
84  for ( curropt = option; curropt; curropt = curropt->next ) {
85  if ( curropt->tag == "datatype" )
86  nudatatype = curropt->datatype();
87  if ( curropt->tag == "sampling" )
88  sampling = curropt->scale();
89  if ( curropt->tag == "slices" )
90  znswitch = 1;
91  if ( curropt->tag == "images" )
92  znswitch = 2;
93  if ( curropt->tag == "add" ) {
94  if ( curropt->values(scale, shift) < 2 )
95  cerr << "-add: Both scale and shift must be specified!" << endl;
96  else setadd = 1;
97  }
98  if ( curropt->tag == "multiply" ) {
99  if ( curropt->values(scale, shift) < 2 )
100  cerr << "-multiply: Both scale and shift must be specified!" << endl;
101  else setmultiply = 1;
102  }
103  if ( curropt->tag == "divide" ) {
104  if ( curropt->values(scale, shift) < 2 )
105  cerr << "-divide: Both scale and shift must be specified!" << endl;
106  else setdivide = 1;
107  }
108  if ( curropt->tag == "largest" ) {
109  if ( curropt->values(scale, shift) < 2 )
110  cerr << "-largest: Both scale and shift must be specified!" << endl;
111  else setlargest = 1;
112  }
113  if ( curropt->tag == "smallest" ) {
114  if ( curropt->values(scale, shift) < 2 )
115  cerr << "-smallest: Both scale and shift must be specified!" << endl;
116  else setsmallest = 1;
117  }
118  if ( curropt->tag == "truncate" ) {
119  if ( curropt->values(min, max) < 2 )
120  cerr << "-truncate: Both min and max must be specified!" << endl;
121  else setminmax = 1;
122  }
123  if ( curropt->tag == "variance" ) {
124  if ( curropt->values(scale, shift) < 1 )
125  cerr << "-variance: A variance ratio be specified!" << endl;
126  else setvar = setadd = 1;
127  }
128  if ( curropt->tag == "fit" ) {
129  if ( ( excl_voxels = curropt->value.real() ) < 0 )
130  cerr << "-fit: A percentage of voxels to exclude must be specified!" << endl;
131  else
132  setlinear = 1;
133  }
134  if ( curropt->tag == "coefficient" )
135  setcc = 1;
136  if ( curropt->tag == "radius" ) {
137  if ( curropt->values(minr, maxr) < 2 )
138  cerr << "-radius: Both radii must be specified!" << endl;
139  else radiusflag = 1;
140  }
141  if ( curropt->tag == "Histomatch" ) {
142  if ( ( bins = curropt->value.integer() ) < 1 )
143  cerr << "-Histomatch: The number of bins must be specified!" << endl;
144  else
145  sethisto = 1;
146  }
147  if ( curropt->tag == "blend" )
148  if ( ( blend_number = curropt->value.integer() ) < 1 )
149  cerr << "-blend: A number of images must be specified!" << endl;
150  if ( curropt->tag == "correlate" )
151  corrfile = curropt->filename();
152  if ( curropt->tag == "Mask" )
153  maskfile = curropt->filename();
154  }
155  option_kill(option);
156 
157  double ti = timer_start();
158 
159  // Read the input files
160  int dataflag = 0;
161  if ( optind < argc - 1 ) dataflag = 1;
162  Bimage* p1 = read_img(argv[optind++], dataflag, -1);
163  if ( p1 == NULL ) {
164  cerr << "Error: No first input file read!" << endl;
165  bexit(-1);
166  }
167 
168  if ( znswitch == 1 )
169  if ( p1->images_to_slices() < 0 ) bexit(-1);
170 
171  if ( znswitch == 2 )
172  if ( p1->slices_to_images() < 0 ) bexit(-1);
173 
174  p1->change_type(Float);
175  if ( p1->standard_deviation() <= 0 ) p1->statistics();
176  if ( sampling.volume() > 0 ) p1->sampling(sampling);
177 
178  Bimage* p2 = read_img(argv[optind++], dataflag, -1);
179  if ( p2 == NULL ) {
180  cerr << "Error: No second input file read!" << endl;
181  bexit(-1);
182  }
183 
184  if ( znswitch == 1 )
185  if ( p2->images_to_slices() < 0 ) bexit(-1);
186 
187  if ( znswitch == 2 )
188  if ( p2->slices_to_images() < 0 ) bexit(-1);
189 
190  p2->change_type(Float);
191  if ( p2->standard_deviation() <= 0 ) p2->statistics();
192  if ( sampling.volume() > 0 ) p2->sampling(sampling);
193 
194  if ( !p1->data_pointer() ) {
195  cerr << "Error: Data missing from image " << p1->file_name() << endl;
196  bexit(-1);
197  }
198  if ( !p2->data_pointer() ) {
199  cerr << "Error: Data missing from image " << p2->file_name() << endl;
200  bexit(-1);
201  }
202 
203  Bimage* pmask = NULL;
204  if ( maskfile.length() )
205  pmask = read_img(maskfile, 1, -1);
206 
207  Bimage* pnu = NULL;
208  if ( blend_number ) {
209  pnu = p1->blend(p2, blend_number);
210  delete p1;
211  p1 = pnu;
212  }
213 
214  if ( setvar )
215  scale = p1->standard_deviation()/(p2->standard_deviation() * sqrt(scale));
216 
217  if ( fabs(scale - 1) > 1e-10 ) p2->multiply(scale);
218 
219  if ( shift ) p2->add(shift);
220 
221  if ( setadd ) // Add two images
222  p1->add(p2);
223 
224  if ( setmultiply ) // Multiply two images
225  p1->multiply(p2);
226 
227  if ( setdivide ) { // Divide the first image by the second image
228  if ( p2->images() == 1 ) p1->divide_one(p2);
229  else p1->divide(p2);
230  }
231 
232  if ( setlargest ) // Largest of two images
233  p1->largest(p2);
234 
235  if ( setsmallest ) // Smallest of two images
236  p1->smallest(p2);
237 
238  int n;
239  double cc(0);
240  Vector3<double> origin(p1->image->origin());
241  if ( setlinear ) { // Do a linear fit
242  if ( !pmask && radiusflag ) {
243  pmask = new Bimage(UCharacter, p1->compound_type(), p1->size(), p1->images());
244  pmask->origin(origin);
245  pmask->mask_shell(origin, minr, maxr);
246 // write_img("mask.pif", pmask);
247  }
248  cc = p1->linear_fit(p2, pmask, excl_voxels);
249  if ( verbose & VERB_RESULT ) {
250  cout << "Image\tR-factor" << endl;
251  for ( n=0; n<p1->images(); n++ )
252  cout << n+1 << tab << p1->image[n].FOM() << endl;
253  cout << endl;
254  }
255  }
256 
257  if ( corrfile.length() ) { // Calculate a correlation image
258  cc = p1->correlate(p2, minr, maxr, pmask, 1);
259  p2->change_type(nudatatype);
260  write_img(corrfile, p2, 0);
261  } else if ( setcc ) {
262  cc = p1->correlate(p2, minr, maxr, pmask, 0);
263  }
264 
265  if ( sethisto ) {
266  if ( bins > 10 )
267  p1->histomatch(p2, bins);
268  else
269  cerr << "Too few bins!" << endl;
270  }
271 
272  if ( setminmax )
273  p1->truncate_to_min_max(min, max);
274 
275  // Write an output file if a file name is given
276  if ( argc > optind && strspn(argv[optind],"-") != 1 ) {
277  p1->change_type(nudatatype);
278  write_img(argv[optind], p1, 0);
279  }
280 
281  delete p1;
282  delete p2;
283  delete pmask;
284 
285  if ( verbose & VERB_TIME )
286  timer_report(ti);
287 
288  bexit((int)(1000*cc));
289 }
290 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
UCharacter
@ UCharacter
Definition: utilities.h:153
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
Bimage::images
long images()
Definition: Bimage.h:504
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bimage::correlate
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Definition: Bimage_correlate.cpp:36
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Bimage::mask_shell
long mask_shell(Vector3< double > origin, double rad_min, double rad_max)
Definition: Bimage.h:1328
Boption::value
Bstring value
Definition: options.h:30
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
verbose
int verbose
Bimage::smallest
void smallest(Bimage *p)
Selects the smallest of each pixel from two images.
Definition: Bimage.cpp:3733
Bimage::divide_one
void divide_one(Bimage *p, double scale=1, double shift=0)
Divides the first image by the first sub-image of the second.
Definition: Bimage.cpp:3635
Bimage::divide
void divide(Bimage *p, double scale=1, double shift=0)
Divides the first image by the second.
Definition: Bimage.cpp:3582
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::truncate_to_min_max
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:331
use
const char * use[]
Definition: bop.cpp:18
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
main
int main(int argc, char *argv[])
Definition: bop.cpp:53
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::linear_fit
double linear_fit(Bimage *p, Bimage *pmask, double max_exclude)
Linear least squares fit of two images.
Definition: Bimage_combine.cpp:477
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::blend
Bimage * blend(Bimage *p, long number)
Blends the two images, creating a new set of sub-images.
Definition: Bimage_combine.cpp:106
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
Bimage::largest
void largest(Bimage *p)
Selects the largest of each pixel from two images.
Definition: Bimage.cpp:3709
Bimage::histomatch
int histomatch(Bimage *p, long bins)
Fits two images by matching the histogram of the second to the first.
Definition: Bimage_combine.cpp:615
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22