| Bsoft 2.0.5 Bernard's software package

btile.cpp
Go to the documentation of this file.
1
9 #include "rwmg.h"
10 #include "mg_processing.h"
11 #include "rwimg.h"
12 #include "file_util.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19
20 int img_tile_with_overlap(Bimage* p, Bstring outputfile, Vector3<int> size,
21 Vector3<int> overlap, int digits, int first_number, DataType nudatatype);
22
23 // Usage assistance
24 const char* use[] = {
25 " ",
26 "Usage: btile [options] input.img/input.star output.img",
27 "------------------------------------------------------",
28 "Splits an image into overlapping tiles.",
29 "During tiling, the original size and tile origins are written into a text",
30 " file with the base of the output file name and the extension \".tiles\".",
31 "The program bpatch can reassemble the tiles using this text file.",
32 " ",
33 "Action:",
34 "-split Split into multiple images.",
35 " ",
36 "Parameters:",
37 "-verbose 7 Verbosity of output.",
38 "-datatype u Force writing of a new data type.",
39 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
40 "-size 120,150,70 Size of tiles (default 512,512,512).",
41 "-overlap 13,15,8 Overlap of tiles (default 0,0,0).",
42 "-first 5 Number given to the first file (default 0).",
43 "-digits 3 Number of digits inserted before the last period in the output file name (default 3).",
44 " ",
45 "Parameters for reconstructions:",
46 "-recid rec_1 Reconstruction ID to use.",
47 " ",
48 "Output:",
49 "-output output.star Output parameter file.",
50 " ",
51 NULL
52 };
53
54 int main(int argc, char **argv)
55 {
56 // Initialize variables
57 int split(0); // Flag to split into individual images
58 DataType nudatatype(Unknown_Type); // Conversion to new type
59 Vector3<double> sampling; // Units for the three axes (A/pixel)
60 Vector3<long> size(512,512,512); // Tile size
61 Vector3<long> overlap; // Tile overlap
62 int first_number(0); // Number given to first file
63 int digits(3); // File number size
64 Bstring recid; // Reconstruction ID to tile
65 Bstring outfile; // Output micrograph parameter file
66
67 int optind;
68 Boption* option = get_option_list(use, argc, argv, optind);
69 Boption* curropt;
70 for ( curropt = option; curropt; curropt = curropt->next ) {
71 if ( curropt->tag == "split" ) split = 1;
72 if ( curropt->tag == "datatype" )
73 nudatatype = curropt->datatype();
74 if ( curropt->tag == "sampling" )
75 sampling = curropt->sampling();
76 if ( curropt->tag == "size" )
77 size = curropt->size();
78 if ( curropt->tag == "overlap" )
79 overlap = curropt->size();
80 if ( curropt->tag == "first" ) {
81 if ( ( first_number = curropt->value.integer() ) < 0 )
82 cerr << "-first: A number must be specified!" << endl;
83 if ( first_number < 0 ) first_number = 0;
84 }
85 if ( curropt->tag == "digits" )
86 if ( ( digits = curropt->value.integer() ) < 1 )
87 cerr << "-digits: A number of digits must be specified!" << endl;
88 if ( curropt->tag == "recid" ) {
89 recid = curropt->value;
90 if ( recid.length() < 1 )
91 cerr << "-recid: A reconstruction ID must be specified!" << endl;
92 }
93 if ( curropt->tag == "output" )
94 outfile = curropt->filename();
95 }
96 option_kill(option);
97
98 double ti = timer_start();
99
100 Bstring filename = argv[optind++];
101 Bproject* project = NULL;
102 Breconstruction* rec;
103
104 if ( file_type(filename) == Micrograph ) {
105 project = read_project(filename);
106 project->select = 1;
107 for ( rec = project->rec; rec && rec->id != recid; rec = rec->next ) ;
108 if ( !rec ) {
109 cerr << "Error: Reconstruction " << recid << " not found!" << endl;
110 bexit(-1);
111 }
112 filename = rec->frec;
113 if ( sampling[0] > 0 ) rec->voxel_size = sampling[0];
114 }
115
116 Bimage* p = read_img(filename, 1, -1);
117 if ( p == NULL ) bexit(-1);
118
119 if ( optind >= argc ) bexit(0);
120
121 if ( sampling.volume() > 0 ) p->sampling(sampling);
122
123 p->label(get_cmd_line(argc, argv));
124
125 if ( argc > optind ) {
126 if ( split)
127 img_tile_with_overlap(p, argv[optind], size, overlap, digits, first_number, nudatatype);
128 else {
129 Vector3<long> start, step(size-overlap);
130 Bimage* ptile = p->extract_tiles(0, start, p->size(), size, step, 1);
131 ptile->label(get_cmd_line(argc, argv));
132 ptile->change_type(nudatatype);
133 write_img(argv[optind], ptile, 0);
134 delete ptile;
135 }
136 }
137
138 delete p;
139
140 if ( project && outfile.length() ) {
141 project_update_comment(project, argc, argv);
142 write_project(outfile, project, 0, 0);
143 }
144
145 project_kill(project);
146
147 if ( verbose & VERB_TIME )
148 timer_report(ti);
149
150 bexit(0);
151 }
152
171 int img_tile_with_overlap(Bimage* p, Bstring outputfile, Vector3<int> size,
172 Vector3<int> overlap, int digits, int first_number, DataType nudatatype)
173 {
174 Bstring filename;
175 char format[16];
176 Bimage* pone = NULL;
177 Vector3<double> start;
178
179 if ( digits < 2 ) digits = 2;
180
181 snprintf(format, 16, "%%0%dd.", digits);
182
183 if ( size[0] < 1 ) size[0] = 512;
184 if ( size[1] < 1 ) size[1] = 512;
185 if ( size[2] < 1 ) size[2] = 512;
186 if ( size[0] > p->sizeX() ) size[0] = p->sizeX();
187 if ( size[1] > p->sizeY() ) size[1] = p->sizeY();
188 if ( size[2] > p->sizeZ() ) size[2] = p->sizeZ();
189 if ( overlap[0] < 0 ) overlap[0] = 0;
190 if ( overlap[1] < 0 ) overlap[1] = 0;
191 if ( overlap[2] < 0 ) overlap[2] = 0;
192 if ( overlap[0] > p->sizeX() ) overlap[0] = p->sizeX()/2;
193 if ( overlap[1] > p->sizeY() ) overlap[1] = p->sizeY()/2;
194 if ( overlap[2] > p->sizeZ() ) overlap[2] = p->sizeZ()/2;
195
196 int n, i = first_number;
197 Vector3<long> step = size - overlap;
198 Vector3<long> xsize;
199
200 filename = outputfile.pre_rev('.') + ".tiles";
201 n = (p->sizeX()/step[0] + 1)*(p->sizeY()/step[1] + 1)*(p->sizeZ()/step[2] + 1);
202
203 if ( verbose ) {
204 cout << "Splitting image " << p->file_name() << " into tiles:" << endl;
205 cout << "Tile size: " << size << endl;
206 cout << "Overlap: " << overlap << endl;
207 cout << "Number of tiles: " << n << endl << endl;
208 }
209
210 ofstream fd(filename.c_str());
211 if ( fd.fail() ) return 0;
212
213 fd << p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << endl;
214 fd << overlap[0] << " " << overlap[1] << " " << overlap[2] << endl;
215
216 for ( n=0, start[2]=0; start[2]<p->sizeZ(); start[2]+=step[2] ) {
217 xsize[2] = p->sizeZ() - (long)start[2];
218 if ( xsize[2] > size[2] ) xsize[2] = size[2];
219 for ( start[1]=0; start[1]<p->sizeY(); start[1]+=step[1] ) {
220 xsize[1] = p->sizeY() - (long)start[1];
221 if ( xsize[1] > size[1] ) xsize[1] = size[1];
222 for ( start[0]=0; start[0]<p->sizeX(); start[0]+=step[0], i++ ) {
223 xsize[0] = p->sizeX() - (long)start[0];
224 if ( xsize[0] > size[0] ) xsize[0] = size[0];
225 pone = p->extract(0, start, xsize);
226 pone->label(p->label());
227 pone->change_type(nudatatype);
228 filename = outputfile.pre_rev('.') + Bstring(i, format) + outputfile.post_rev('.');
229 if ( verbose & VERB_LABEL )
230 cout << "File " << i << ": " << filename << endl;
231 write_img(filename, pone, 0);
232 delete pone;
233 fd << start[0] << " " << start[1] << " " << start[2] << endl;
234 n++;
235 }
236 }
237 }
238
239 fd.close();
240
241 if ( verbose & VERB_LABEL )
242 cout << "Number of tiles: " << n << endl << endl;
243
244 return n;
245 }
img_tile_with_overlap
int img_tile_with_overlap(Bimage *p, Bstring outputfile, Vector3< int > size, Vector3< int > overlap, int digits, int first_number, DataType nudatatype)
Writes overlapping tiles into new images.
Definition: btile.cpp:171
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::extract_tiles
Bimage * extract_tiles(long nn, vector< Vector3< long >> &coords, Vector3< long > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Definition: Bimage_extract.cpp:335
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.
main
int main(int argc, char **argv)
Definition: btile.cpp:54
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
Breconstruction
Definition: mg_processing.h:280
Boption::tag
Bstring tag
Definition: options.h:29
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
verbose
int verbose
project
Bproject * project
Definition: bshow.cpp:31
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.
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
Micrograph
Definition: file_util.h:23
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
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
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
file_util.h
Header file for file checking functions.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
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
Bproject::select
int select
Definition: mg_processing.h:339
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< int >
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
mg_processing.h
Header file for micrograph processing.
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
Breconstruction::voxel_size
double voxel_size
Definition: mg_processing.h:292
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
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
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
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
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
use
const char * use[]
Definition: btile.cpp:24