| Bsoft 2.0.5 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->sampling();
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);
129 project_kill(project);
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();
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 ppatch->label(get_cmd_line(argc, argv));
157
158 write_img(patchfile, ppatch, 0);
159
160 Bstring id("1");
161 if ( outparam.length() ) {
162 project = read_project(paramfile);
163 if ( !project->rec ) reconstruction_add(&project->rec, id);
164 project->rec->frec = patchfile;
165 project_update_comment(project, argc, argv);
166 write_project(outparam, project, 0, 0);
167 project_kill(project);
168 }
169
170 string_kill(file_list);
171 delete ppatch;
172 patchfile = 0;
173 id = 0;
174
175 if ( verbose & VERB_TIME )
176 timer_report(ti);
177
178 bexit(0);
179 }
180
181 int img_pack_into_image(Bimage* p, Bimage* ppatch, Vector3<int> start, Vector3<int> overlap)
182 {
183 long i, j, c, x, y, z, xx, yy, zz;
184 Vector3<double> w;
185
186 overlap *= 0.5;
187
188 if ( verbose & VERB_PROCESS )
189 cout << "Placing tile of size " << p->size() << " at " << start << endl << endl;
190
191 for ( z=0; z<p->sizeZ(); z++ ) {
192 zz = start[2] + z;
193 w[2] = 0;
194 if ( z >= overlap[2] && z < p->sizeZ() - overlap[2] ) w[2] = 1;
195 if ( zz <= overlap[2] ) w[2] = 1;
196 if ( zz >= ppatch->sizeZ() - overlap[2] ) w[2] = 1;
197 for ( y=0; y<p->sizeY(); y++ ) {
198 yy = start[1] + y;
199 w[1] = 0;
200 if ( y >= overlap[1] && y < p->sizeY() - overlap[1] ) w[1] = 1;
201 if ( yy <= overlap[1] ) w[1] = 1;
202 if ( yy >= ppatch->sizeY() - overlap[1] ) w[1] = 1;
203 for ( x=0; x<p->sizeX(); x++ ) {
204 xx = start[0] + x;
205 w[0] = 0;
206 if ( x >= overlap[0] && x < p->sizeX() - overlap[0] ) w[0] = 1;
207 if ( xx <= overlap[0] ) w[0] = 1;
208 if ( xx >= ppatch->sizeX() - overlap[0] ) w[0] = 1;
209 if ( w.volume() ) {
210 i = ((z*p->sizeY() + y)*p->sizeX() + x)*p->channels();
211 j = ((zz*ppatch->sizeY() + yy)*ppatch->sizeX() + xx)*p->channels();
212 for ( c=0; c<p->channels(); c++, i++, j++ ) ppatch->set(j, (*p)[i]);
213 }
214 }
215 }
216 }
217
218 return(0);
219 }
220
236 Bimage* img_patch(Bstring* file_list, Bstring tile_file, DataType nudatatype,
237 double cutmin, double cutmax, double nuavg, double nustd)
238 {
239 long i, n(1);
240 Bstring* file_ptr = NULL;
241 Vector3<long> size; // Overall size after patching
242 Vector3<long> overlap; // Tile overlap
243
244 // Get the number of tiles and find the last tile
245 for ( file_ptr = file_list; file_ptr->next; file_ptr = file_ptr->next ) n++;
246
247 // Read the tile size, overlap and locations
248 ifstream fd(tile_file.c_str());
249 if ( fd.fail() ) {
250 cerr << "Error: A text file specifying the size and tile origins must be given!" << endl;
251 bexit(-1);
252 }
253
254 fd >> size[0] >> size[1] >> size[2];
255 fd >> overlap[0] >> overlap[1] >> overlap[2];
256
257 Vector3<int>* tile_origin = new Vector3<int>[n];
258
259 for ( i=0; i<n; i++ )
260 fd >> tile_origin[i][0] >> tile_origin[i][1] >> tile_origin[i][2];
261
262 fd.close();
263
264 // Get the overall size
265 Bimage* p = read_img(*file_list, 0, 0);
266
267 Vector3<long> tile_size(p->size()); // Tile size from first image
268
269 if ( verbose & VERB_DEBUG ) {
270 cout << "DEBUG img_patch: tilesize=" << tile_size << " overlap=" << overlap << endl;
271 cout << "DEBUG img_patch: tiles=" << n << endl;
272 }
273
274 delete p;
275
276 // Get the overall size
277 p = read_img(*file_ptr, 0, 0);
278
279 if ( size != tile_origin[n-1] + p->size() ) {
280 cerr << "Error: The expected size does not match the size given in file " << tile_file << endl;
281 cerr << " Expected size: " << tile_origin[n-1] + p->size() << endl;
282 cerr << " Given size: " << size << endl;
283 cerr << " Make sure all the tiles are present." << endl;
284 bexit(-1);
285 }
286
287 // Set up the receiving image
288 if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
289 Bimage* ppatch = new Bimage(nudatatype, p->compound_type(), size, 1);
290 ppatch->sampling(p->sampling());
291 ppatch->image[0] = p->image[0];
292 ppatch->origin(ppatch->default_origin());
293 delete p;
294
295 if ( verbose ) {
296 cout << "Patching tiles:" << endl;
297 cout << "Size: " << size << endl;
298 cout << "Overlap: " << overlap << endl;
299 cout << "Number of tiles: " << n << endl << endl;
300 }
301
302 for ( i=0, file_ptr = file_list; i<n && file_ptr; i++, file_ptr = file_ptr->next ) {
303 if ( ( p = read_img(*file_ptr, 1, 0) ) ) {
304 if ( verbose & VERB_DEBUG )
305 cout << p->file_name() << ": " << p->size() << " " << p->channels() << " " << p->images() << endl;
306 if ( nustd ) p->rescale_to_avg_std(nuavg, nustd);
307 if ( cutmin || cutmax )
308 p->truncate_to_min_max(cutmin, cutmax);
309 p->change_type(nudatatype);
310 // ppatch->place_with_overlap(p, i, tile_size, n, tile_origin);
311 img_pack_into_image(p, ppatch, tile_origin[i], overlap);
312 delete p;
313 }
314 }
315
316 ppatch->calculate_background();
317
318 delete[] tile_origin;
319
320 return ppatch;
321 }
322
323
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
rwmg.h
Header file for reading and writing micrograph parameters.
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:650
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Breconstruction
Definition: mg_processing.h:280
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
Bimage::assemble_tiles
int assemble_tiles(Bimage *pt, int flag=0)
Assembles overlapping tiles into this image.
Definition: Bimage_combine.cpp:432
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
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::file_name
void file_name(string s)
Definition: Bimage.h:230
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
timer.h
Utilities for timing functions.
Vector3::max
Type max()
Definition: Vector3.h:175
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
Micrograph
Definition: file_util.h:23
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:704
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bstring::next
Bstring * next
Definition: Bstring.h:31
Boption
A class for a command line input option.
Definition: options.h:26
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
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
file_util.h
Header file for file checking functions.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
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
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
main
int main(int argc, char **argv)
Definition: bpatch.cpp:53
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
use
const char * use[]
Definition: bpatch.cpp:24
Bimage::images
long images()
Definition: Bimage.h:491
Vector3::volume
double volume()
Definition: Vector3.h:235
project_update_comment
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:468
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
img_pack_into_image
int img_pack_into_image(Bimage *p, Bimage *ppatch, Vector3< int > start, Vector3< int > overlap)
Definition: bpatch.cpp:181
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
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760
verbose
int verbose
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:236