Bsoft  2.1.0
Bernard's software package
bpatch.cpp
Go to the documentation of this file.
1 
9 #include "rwmg.h"
10 #include "rwimg.h"
11 #include "file_util.h"
12 #include "utilities.h"
13 #include "options.h"
14 #include "timer.h"
15 #include <fstream>
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 Bimage* img_patch(Bstring* file_list, Bstring tile_file, DataType nudatatype,
21  double cutmin, double cutmax, double nuavg, double nustd);
22 
23 // Usage assistance
24 const char* use[] = {
25 " ",
26 "Usage: bpatch [options] input.images/input.star",
27 "-----------------------------------------------",
28 "Patches a list of tiles together with overlap.",
29 "Requires a text file generated by btile that specifies the size and order of the tiles.",
30 "Any number of input images may be given and may include the wild card \"*\".",
31 "All images must have the same data type.",
32 " ",
33 "Parameters:",
34 "-verbose 3 Verbosity of output.",
35 "-datatype u Force writing of a new data type.",
36 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
37 "-origin 0.8,-10,15.7 Set the origin of the output image (default 0,0,0).",
38 "-size 1024,1024,120 Output image size (default from tile file).",
39 "-mode 1 Overlap handling: 0=average(default), 1=weigh, 2=central.",
40 "-rescale 127,27.5 Rescale all input data to average and standard deviation.",
41 "-truncate -5,5.5 Truncate data to minimum and maximum after rescaling.",
42 " ",
43 "Input:",
44 "-tiles text.tiles Text file with overall size and tile origins.",
45 " ",
46 "Output:",
47 "-output output.img Output image (default patch.pif).",
48 "-parameters file.star Output micrograph parameter file.",
49 " ",
50 NULL
51 };
52 
53 int main(int argc, char **argv)
54 {
55  // Initialize variables
56  DataType nudatatype(Unknown_Type); // Conversion to new type
57  Vector3<double> sampling; // Units for the three axes (A/pixel)
58  Vector3<double> origin; // New image origin
59  int set_origin(0); // Flag to set origin
60  Vector3<long> size; // Output image size
61  int flag(0); // Overlap flag
62  double nuavg(0), nustd(0); // Rescaling to average and stdev
63  double cutmin(0), cutmax(0); // Truncation
64  Bstring patchfile("patch.pif");
65  Bstring tile_file; // Text file with overall size and tile origins
66  Bstring outparam; // Output micrograph parameter file
67 
68  int optind;
69  Boption* option = get_option_list(use, argc, argv, optind);
70  Boption* curropt;
71  for ( curropt = option; curropt; curropt = curropt->next ) {
72  if ( curropt->tag == "datatype" )
73  nudatatype = curropt->datatype();
74  if ( curropt->tag == "sampling" )
75  sampling = curropt->scale();
76  if ( curropt->tag == "origin" ) {
77  if ( curropt->value[0] == 'c' ) {
78  set_origin = 2;
79  } else {
80  origin = curropt->origin();
81  set_origin = 1;
82  }
83  }
84  if ( curropt->tag == "size" )
85  size = curropt->size();
86  if ( curropt->tag == "mode" )
87  flag = curropt->value.integer();
88  if ( curropt->tag == "rescale" )
89  if ( curropt->values(nuavg, nustd) < 2 )
90  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
91  if ( curropt->tag == "truncate" )
92  if ( curropt->values(cutmin, cutmax) < 2 )
93  cerr << "-truncate: Both min and max must be specified!" << endl;
94  if ( curropt->tag == "output" )
95  patchfile = curropt->filename();
96  if ( curropt->tag == "tiles" )
97  tile_file = curropt->filename();
98  if ( curropt->tag == "parameters" )
99  outparam = curropt->filename();
100  }
101  option_kill(option);
102 
103  if ( verbose && argc < 3 ) bexit(-1);
104 
105  double ti = timer_start();
106 /*
107  if ( !tile_file.length() ) {
108  cerr << "Error: A text file specifying the size and tile origins must be given!" << endl;
109  bexit(-1);
110  }
111 */
112  if ( argc - optind < 1 ) {
113  cerr << "Error: No tiles found!" << endl;
114  bexit(-1);
115  }
116 
117  Bstring* file_list = NULL;
118  while ( optind < argc ) string_add(&file_list, argv[optind++]);
119  Bstring paramfile = *file_list;
120  Bproject* project = NULL;
121  Breconstruction* rec;
122 
123  if ( file_type(paramfile) == Micrograph ) {
124  project = read_project(file_list);
125  string_kill(file_list);
126  file_list = NULL;
127  for ( rec = project->rec; rec; rec = rec->next )
128  string_add(&file_list, rec->frec);
130  }
131 
132  Bimage* ppatch = NULL;
133 
134  if ( tile_file.length() )
135  ppatch = img_patch(file_list, tile_file, nudatatype,
136  cutmin, cutmax, nuavg, nustd);
137  else {
138  Bimage* p = read_img(*file_list, 1, -1);
139  if ( size.volume() < 1 )
140  for ( long nn=0; nn<p->images(); ++nn )
141  size = size.max(p->size()+p->image[nn].origin());
142  ppatch = p->copy_header(1);
143  ppatch->size(size);
144  ppatch->data_alloc_and_clear();
145  ppatch->assemble_tiles(p, flag);
146  delete p;
147  }
148 
149  if ( sampling.volume() > 0 ) ppatch->sampling(sampling);
150 
151  if ( set_origin ) {
152  if ( set_origin == 2 ) ppatch->origin(ppatch->size()/2);
153  else ppatch->origin(origin);
154  }
155 
156  write_img(patchfile, ppatch, 0);
157 
158  Bstring id("1");
159  if ( outparam.length() ) {
160  project = read_project(paramfile);
161  if ( !project->rec ) reconstruction_add(&project->rec, id);
162  project->rec->frec = patchfile;
163  write_project(outparam, project, 0, 0);
165  }
166 
167  string_kill(file_list);
168  delete ppatch;
169  patchfile = 0;
170  id = 0;
171 
172  if ( verbose & VERB_TIME )
173  timer_report(ti);
174 
175  bexit(0);
176 }
177 
179 {
180  long i, j, c, x, y, z, xx, yy, zz;
181  Vector3<double> w;
182 
183  overlap *= 0.5;
184 
185  if ( verbose & VERB_PROCESS )
186  cout << "Placing tile of size " << p->size() << " at " << start << endl << endl;
187 
188  for ( z=0; z<p->sizeZ(); z++ ) {
189  zz = start[2] + z;
190  w[2] = 0;
191  if ( z >= overlap[2] && z < p->sizeZ() - overlap[2] ) w[2] = 1;
192  if ( zz <= overlap[2] ) w[2] = 1;
193  if ( zz >= ppatch->sizeZ() - overlap[2] ) w[2] = 1;
194  for ( y=0; y<p->sizeY(); y++ ) {
195  yy = start[1] + y;
196  w[1] = 0;
197  if ( y >= overlap[1] && y < p->sizeY() - overlap[1] ) w[1] = 1;
198  if ( yy <= overlap[1] ) w[1] = 1;
199  if ( yy >= ppatch->sizeY() - overlap[1] ) w[1] = 1;
200  for ( x=0; x<p->sizeX(); x++ ) {
201  xx = start[0] + x;
202  w[0] = 0;
203  if ( x >= overlap[0] && x < p->sizeX() - overlap[0] ) w[0] = 1;
204  if ( xx <= overlap[0] ) w[0] = 1;
205  if ( xx >= ppatch->sizeX() - overlap[0] ) w[0] = 1;
206  if ( w.volume() ) {
207  i = ((z*p->sizeY() + y)*p->sizeX() + x)*p->channels();
208  j = ((zz*ppatch->sizeY() + yy)*ppatch->sizeX() + xx)*p->channels();
209  for ( c=0; c<p->channels(); c++, i++, j++ ) ppatch->set(j, (*p)[i]);
210  }
211  }
212  }
213  }
214 
215  return(0);
216 }
217 
233 Bimage* img_patch(Bstring* file_list, Bstring tile_file, DataType nudatatype,
234  double cutmin, double cutmax, double nuavg, double nustd)
235 {
236  long i, n(1);
237  Bstring* file_ptr = NULL;
238  Vector3<long> size; // Overall size after patching
239  Vector3<long> overlap; // Tile overlap
240 
241  // Get the number of tiles and find the last tile
242  for ( file_ptr = file_list; file_ptr->next; file_ptr = file_ptr->next ) n++;
243 
244  // Read the tile size, overlap and locations
245  ifstream fd(tile_file.c_str());
246  if ( fd.fail() ) {
247  cerr << "Error: A text file specifying the size and tile origins must be given!" << endl;
248  bexit(-1);
249  }
250 
251  fd >> size[0] >> size[1] >> size[2];
252  fd >> overlap[0] >> overlap[1] >> overlap[2];
253 
254  Vector3<int>* tile_origin = new Vector3<int>[n];
255 
256  for ( i=0; i<n; i++ )
257  fd >> tile_origin[i][0] >> tile_origin[i][1] >> tile_origin[i][2];
258 
259  fd.close();
260 
261  // Get the overall size
262  Bimage* p = read_img(*file_list, 0, 0);
263 
264  Vector3<long> tile_size(p->size()); // Tile size from first image
265 
266  if ( verbose & VERB_DEBUG ) {
267  cout << "DEBUG img_patch: tilesize=" << tile_size << " overlap=" << overlap << endl;
268  cout << "DEBUG img_patch: tiles=" << n << endl;
269  }
270 
271  delete p;
272 
273  // Get the overall size
274  p = read_img(*file_ptr, 0, 0);
275 
276  if ( size != tile_origin[n-1] + p->size() ) {
277  cerr << "Error: The expected size does not match the size given in file " << tile_file << endl;
278  cerr << " Expected size: " << tile_origin[n-1] + p->size() << endl;
279  cerr << " Given size: " << size << endl;
280  cerr << " Make sure all the tiles are present." << endl;
281  bexit(-1);
282  }
283 
284  // Set up the receiving image
285  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
286  Bimage* ppatch = new Bimage(nudatatype, p->compound_type(), size, 1);
287  ppatch->sampling(p->sampling(0));
288  ppatch->image[0] = p->image[0];
289  ppatch->origin(ppatch->default_origin());
290  delete p;
291 
292  if ( verbose ) {
293  cout << "Patching tiles:" << endl;
294  cout << "Size: " << size << endl;
295  cout << "Overlap: " << overlap << endl;
296  cout << "Number of tiles: " << n << endl << endl;
297  }
298 
299  for ( i=0, file_ptr = file_list; i<n && file_ptr; i++, file_ptr = file_ptr->next ) {
300  if ( ( p = read_img(*file_ptr, 1, 0) ) ) {
301  if ( verbose & VERB_DEBUG )
302  cout << p->file_name() << ": " << p->size() << " " << p->channels() << " " << p->images() << endl;
303  if ( nustd ) p->rescale_to_avg_std(nuavg, nustd);
304  if ( cutmin || cutmax )
305  p->truncate_to_min_max(cutmin, cutmax);
306  p->change_type(nudatatype);
307 // ppatch->place_with_overlap(p, i, tile_size, n, tile_origin);
308  img_pack_into_image(p, ppatch, tile_origin[i], overlap);
309  delete p;
310  }
311  }
312 
313  ppatch->calculate_background();
314 
315  delete[] tile_origin;
316 
317  return ppatch;
318 }
319 
320 
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
file_util.h
Header file for file checking functions.
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
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 >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
verbose
int verbose
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
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
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
use
const char * use[]
Definition: bpatch.cpp:24
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
img_patch
Bimage * img_patch(Bstring *file_list, Bstring tile_file, DataType nudatatype, double cutmin, double cutmax, double nuavg, double nustd)
Assembles overlapping tiles into a single image.
Definition: bpatch.cpp:233
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Micrograph
@ Micrograph
Definition: file_util.h:23
Boption::value
Bstring value
Definition: options.h:30
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.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::assemble_tiles
int assemble_tiles(Bimage *pt, int flag=0)
Assembles overlapping tiles into this image.
Definition: Bimage_combine.cpp:433
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
Bimage::data_alloc_and_clear
unsigned char * data_alloc_and_clear()
Definition: Bimage.h:272
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
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
Breconstruction
Definition: mg_processing.h:283
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
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.
Bstring::next
Bstring * next
Definition: Bstring.h:31
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
main
int main(int argc, char **argv)
Definition: bpatch.cpp:53
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
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
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
Bimage::channels
long channels()
Definition: Bimage.h:384
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
img_pack_into_image
int img_pack_into_image(Bimage *p, Bimage *ppatch, Vector3< int > start, Vector3< int > overlap)
Definition: bpatch.cpp:178
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
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
Vector3::max
Type max()
Definition: Vector3.h:175