| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_tile_patch.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include <fstream>
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
16
35 int img_tile_with_overlap(Bimage* p, Bstring outputfile, Vector3<int> size,
36 Vector3<int> overlap, int digits, int first_number, DataType nudatatype)
37 {
38 Bstring filename;
39 char format[16];
40 Bimage* pone = NULL;
41 Vector3<float> start;
42
43 if ( digits < 2 ) digits = 2;
44
45 snprintf(format, 16, "%%0%dd.", digits);
46
47 if ( size[0] < 1 ) size[0] = 512;
48 if ( size[1] < 1 ) size[1] = 512;
49 if ( size[2] < 1 ) size[2] = 512;
50 if ( size[0] > p->sizeX() ) size[0] = p->sizeX();
51 if ( size[1] > p->sizeY() ) size[1] = p->sizeY();
52 if ( size[2] > p->sizeZ() ) size[2] = p->sizeZ();
53 if ( overlap[0] < 0 ) overlap[0] = 0;
54 if ( overlap[1] < 0 ) overlap[1] = 0;
55 if ( overlap[2] < 0 ) overlap[2] = 0;
56 if ( overlap[0] > p->sizeX() ) overlap[0] = p->sizeX()/2;
57 if ( overlap[1] > p->sizeY() ) overlap[1] = p->sizeY()/2;
58 if ( overlap[2] > p->sizeZ() ) overlap[2] = p->sizeZ()/2;
59
60 int n, i = first_number;
61 Vector3<int> step = size - overlap;
62 Vector3<int> xsize;
63
64 filename = outputfile.pre_rev('.') + ".tiles";
65 n = (p->sizeX()/step.x() + 1)*(p->sizeY()/step.y() + 1)*(p->sizeZ()/step.z() + 1);
66
67 if ( verbose ) {
68 cout << "Splitting image " << p->file_name() << " into tiles:" << endl;
69 cout << "Tile size: " << size << endl;
70 cout << "Overlap: " << overlap << endl;
71 cout << "Number of tiles: " << n << endl << endl;
72 }
73
74 ofstream fd(filename.c_str());
75 if ( fd.fail() ) return 0;
76
77 fd << p->sizeX() << " " << p->sizeY() << " " << p->sizeZ() << endl;
78 fd << overlap[0] << " " << overlap[1] << " " << overlap[2] << endl;
79
80 for ( n=0, start[2]=0; start[2]<p->sizeZ(); start[2]+=step[2] ) {
81 xsize[2] = p->sizeZ() - (long)start[2];
82 if ( xsize[2] > size[2] ) xsize[2] = size[2];
83 for ( start[1]=0; start[1]<p->sizeY(); start[1]+=step[1] ) {
84 xsize[1] = p->sizeY() - (long)start[1];
85 if ( xsize[1] > size[1] ) xsize[1] = size[1];
86 for ( start[0]=0; start[0]<p->sizeX(); start[0]+=step[0], i++ ) {
87 xsize[0] = p->sizeX() - (long)start[0];
88 if ( xsize[0] > size[0] ) xsize[0] = size[0];
89 pone = p->extract(0, start, xsize);
90 pone->label(p->label());
91 pone->change_type(nudatatype);
92 filename = outputfile.pre_rev('.') + Bstring(i, format) + outputfile.post_rev('.');
93 if ( verbose & VERB_LABEL )
94 cout << "File " << i << ": " << filename << endl;
95 write_img(filename, pone);
96 delete pone;
97 fd << start[0] << " " << start[1] << " " << start[2] << endl;
98 n++;
99 }
100 }
101 }
102
103 fd.close();
104
105 if ( verbose & VERB_LABEL )
106 cout << "Number of tiles: " << n << endl << endl;
107
108 return n;
109 }
110
126 Bimage* img_patch(Bstring* file_list, Bstring tile_file, DataType nudatatype,
127 double cutmin, double cutmax, double nuavg, double nustd)
128 {
129 size_t i, n(1);
130 Bstring* file_ptr = NULL;
131 Vector3<int> size; // Overall size after patching
132 Vector3<int> overlap; // Tile overlap
133
134 // Get the number of tiles and find the last tile
135 for ( file_ptr = file_list; file_ptr->next; file_ptr = file_ptr->next ) n++;
136
137 // Read the tile size, overlap and locations
138 ifstream fd(tile_file.c_str());
139 if ( fd.fail() ) {
140 cerr << "Error: A text file specifying the size and tile origins must be given!" << endl;
141 bexit(-1);
142 }
143
144 fd >> size[0] >> size[1] >> size[2];
145 fd >> overlap[0] >> overlap[1] >> overlap[2];
146
147 Vector3<int>* tile_origin = new Vector3<int>[n];
148
149 for ( i=0; i<n; i++ )
150 fd >> tile_origin[i][0] >> tile_origin[i][1] >> tile_origin[i][2];
151
152 fd.close();
153
154 // Get the overall size
155 Bimage* p = read_img(*file_list, 0, 0);
156
157 Vector3<int> tile_size(p->size()); // Tile size from first image
158
159 if ( verbose & VERB_DEBUG ) {
160 cout << "DEBUG img_patch: tilesize=" << tile_size << " overlap=" << overlap << endl;
161 cout << "DEBUG img_patch: tiles=" << n << endl;
162 }
163
164 delete p;
165
166 // Get the overall size
167 p = read_img(*file_ptr, 0, 0);
168
169 if ( size != tile_origin[n-1] + p->size() ) {
170 cerr << "Error: The expected size does not match the size given in file " << tile_file << endl;
171 cerr << " Expected size: " << tile_origin[n-1] + p->size() << endl;
172 cerr << " Given size: " << size << endl;
173 cerr << " Make sure all the tiles are present." << endl;
174 bexit(-1);
175 }
176
177 // Set up the receiving image
178 if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
179 Bimage* ppatch = new Bimage(nudatatype, p->compound_type(), size.x(), size.y(), size.z(), 1);
180 ppatch->sampling(p->sampling());
181 ppatch->image[0] = p->image[0];
182 ppatch->image->origin(ppatch->size()/2);
183 delete p;
184
185 if ( verbose ) {
186 cout << "Patching tiles:" << endl;
187 cout << "Size: " << size << endl;
188 cout << "Overlap: " << overlap << endl;
189 cout << "Number of tiles: " << n << endl << endl;
190 }
191
192 for ( i=0, file_ptr = file_list; i<n && file_ptr; i++, file_ptr = file_ptr->next ) {
193 if ( ( p = read_img(*file_ptr, 1, 0) ) ) {
194 if ( verbose & VERB_DEBUG )
195 cout << p->file_name() << ": " << p->size() << " " << p->channels() << " " << p->images() << endl;
196 if ( nustd ) p->rescale_to_avg_std(nuavg, nustd);
197 if ( cutmin || cutmax )
198 p->truncate_to_min_max(cutmin, cutmax);
199 p->change_type(nudatatype);
200 ppatch->place_with_overlap(p, i, tile_size, n, tile_origin);
201 delete p;
202 }
203 }
204
205 ppatch->calculate_background();
206
207 delete[] tile_origin;
208
209 return ppatch;
210 }
211
212
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1544
Vector3::z
Type z()
Definition: Vector3.h:139
verbose
int verbose
Bimage::sizeZ
void sizeZ(size_t nz)
Definition: Bimage.h:332
DataType
DataType
Base data type specifier.
Definition: utilities.h:139
Bimage::calculate_background
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:26
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:105
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:307
Vector3< int >
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:232
Bimage::size
Vector3< size_t > size() const
Definition: Bimage.h:327
Bimage
General image parameter class.
Definition: Bimage.h:152
Bimage::place_with_overlap
int place_with_overlap(Bimage *p, size_t nn, Vector3< int > tile_size, size_t ntile, Vector3< int > *tile_origin)
Packs a tile with overlap into a new composite image.
Definition: Bimage_combine.cpp:243
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg=0, double nustd=1)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:100
read_img
Bimage * read_img(const char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
Bimage::channels
size_t channels()
Definition: Bimage.h:325
Bstring::next
Bstring * next
Definition: Bstring.h:30
Bimage::sampling
void sampling(double s)
Definition: Bimage.h:440
Vector3::x
Type x()
Definition: Vector3.h:137
Bimage::file_name
void file_name(Bstring s)
Definition: Bimage.h:213
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:132
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bimage::extract
Bimage * extract(size_t nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:434
write_img
int write_img(const char *filename, Bimage *p)
General driver function to write multiple image formats This is the only image writing function that ...
Definition: rwimg.cpp:255
Bimage::images
size_t images()
Definition: Bimage.h:438
Bstring
Definition: Bstring.h:28
Bimage::sizeX
void sizeX(size_t nx)
Definition: Bimage.h:330
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: img_tile_patch.cpp:35
Bimage::sizeY
void sizeY(size_t ny)
Definition: Bimage.h:331
Unknown_Type
Definition: utilities.h:140
rwimg.h
Header file for 2D and 3D image I/O.
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:419
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: img_tile_patch.cpp:126
Bimage::label
void label(Bstring s)
Definition: Bimage.h:217
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:99
Bimage::data_type
DataType data_type()
Definition: Bimage.h:305
Vector3::y
Type y()
Definition: Vector3.h:138
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:287
Bimage::image
Bsub_image * image
Definition: Bimage.h:183