| Bsoft 2.0.5 Bernard's software package

Bimage_powerspectrum.cpp
Go to the documentation of this file.
1
9 #include "Bimage.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
15 int set_tile_size(Vector3<long>& tile_size, Vector3<long> img_size)
16 {
17 // Minimum and maximum tile sizes allowed
18 long min_size(64);
19 long max_size(4096);
20
21 tile_size = tile_size.max(min_size);
22 tile_size = tile_size.min(img_size);
23 tile_size = tile_size.min(max_size);
24
25 if ( tile_size[0] != tile_size[1] ) {
26 if ( tile_size[0] > tile_size[1] ) tile_size[0] = tile_size[1];
27 else tile_size[1] = tile_size[0];
28 if ( img_size[2] > 1 ) tile_size[2] = tile_size[0];
29 }
30
31 return 0;
32 }
33
34
48 int Bimage::power_spectrum(int flags)
49 {
50 if ( verbose & VERB_DEBUG ) {
51 cout << "DEBUG Bimage::power_spectrum: flags=" << flags << endl;
52 cout << "DEBUG Bimage::power_spectrum: tiles=" << n << endl;
53 }
54
55 color_to_simple();
56 change_type(Float);
57
58 if ( flags & 1 ) rescale_to_avg_std(0, 1);
59
60 if ( fft() )
61 return error_show("Bimage::power_spectrum", __FILE__, __LINE__);
62
63 complex_to_intensities();
64
65 fourier_type(NoTransform);
66
67 if ( flags & 2 ) average_images();
68 // cout << "number of images = " << images() << endl;
69
70 statistics();
71
72 origin(0,0,0);
73
74 show_maximum((complex(1)).power());
75
76 if ( flags & 4 ) center_wrap();
77
78 if ( flags & 8 ) logarithm(); // Get logarithm of intensities
79
80 return 0;
81 }
82
97 Bimage* Bimage::powerspectrum_tiled(long img_num, Vector3<long> tile_size, int flags)
98 {
99 // If the image is square it is assumed to be a CCD image or extracted to be square
100 // and no padding around the edges is necessary
101 int pad(0);
102 long max_size(4096);
103
104 // If the image is not square, it is assumed to be a micrograph with severe edge
105 // features that will affect the power spectrum
106 if ( x != y && x > max_size && y > max_size) {
107 pad = x/10;
108 if ( pad < y/10 ) pad = y/10;
109 }
110
111 Vector3<long> start, ext_size, step_size;
112
113 set_tile_size(tile_size, size());
114
115 ext_size[0] = tile_size[0]*((x - 2*pad)/tile_size[0]);
116 ext_size[1] = tile_size[1]*((y - 2*pad)/tile_size[1]);
117 ext_size[2] = tile_size[2]*((z - 2*pad)/tile_size[2]);
118 ext_size = ext_size.max(1);
119
120 start[0] = (x - ext_size[0])/2;
121 start[1] = (y - ext_size[1])/2;
122 start[2] = (z - ext_size[2])/2;
123
124 if ( verbose & VERB_DEBUG ) {
125 cout << "DEBUG Bimage::powerspectrum_tiled: img_num=" << img_num << endl;
126 cout << "DEBUG Bimage::powerspectrum_tiled: start=" << start << endl;
127 cout << "DEBUG Bimage::powerspectrum_tiled: tile_size=" << tile_size << endl;
128 }
129
130 // If a single image assume a micrograph that needs to be tiled
131 Bimage* pex = extract_tiles(img_num, start, ext_size, tile_size, step_size, 0);
132
133 pex->power_spectrum(flags);
134
135 return pex;
136 }
137
154 Bimage* Bimage::powerspectrum_tilt_axis(long img_num, Vector3<long> tile_size,
155 double tilt_axis, double tilt_offset, int flags)
156 {
157 long i, xx, yy, ntiles(0);
158 double tan_a(tan(tilt_axis));
159 double inv_tan_a(1.0/tan_a);
160 double ratang(atan2(y, x));
161 Vector3<long> origin(size()/2);
162 vector<Vector3<long>> tile;
163
164 set_tile_size(tile_size, size());
165
166 if ( fabs(tilt_axis) < ratang || fabs(tilt_axis) > M_PI - ratang ) {
167 origin[1] += (long)tilt_offset;
168 ntiles = x/tile_size[0];
169 tile.resize(ntiles);
170 for ( i=0, xx = origin[0] - (ntiles * tile_size[0])/2; i<ntiles; i++, xx += tile_size[0] ) {
171 tile[i][0] = xx;
172 tile[i][1] = (long) (origin[1] + (xx - origin[0])*tan_a);
173 }
174 } else {
175 origin[0] += (long)tilt_offset;
176 ntiles = y/tile_size[1];
177 tile.resize(ntiles);
178 for ( i=0, yy = origin[1] - (ntiles * tile_size[1])/2; i<ntiles; i++, yy += tile_size[1] ) {
179 tile[i][0] = (long) (origin[0] + (yy - origin[1])*inv_tan_a);
180 tile[i][1] = yy;
181 }
182 }
183
184 if ( verbose & VERB_DEBUG ) {
185 cout << "DEBUG Bimage::powerspectrum_tilted: img_num=" << img_num << endl;
186 cout << "DEBUG Bimage::powerspectrum_tilted: origin=" << origin << endl;
187 cout << "DEBUG Bimage::powerspectrum_tilted: tile_size=" << tile_size << endl;
188 cout << "DEBUG Bimage::powerspectrum_tilted: ntiles=" << ntiles << endl;
189 // for ( i=0; i<ntiles; i++ )
190 // cout << "DEBUG Bimage::powerspectrum_tilted: " << tile[i][0] << " " << tile[i][1] << endl;
191 }
192
193 // If a single image assume a micrograph that needs to be tiled
194 Bimage* pex = extract_tiles(img_num, tile, tile_size);
195
196 Vector3<long> translate;
197 Vector3<long> nusize(1,1,1);
198 if ( pex->sizeX() != pex->sizeY() ) {
199 nusize[0] = pex->sizeX();
200 if ( pex->sizeY() > 1 && nusize[0] > pex->sizeY() ) nusize[0] = pex->sizeY();
201 if ( pex->sizeZ() > 1 && nusize[0] > pex->sizeZ() ) nusize[0] = pex->sizeZ();
202 if ( pex->sizeY() > 1 ) nusize[1] = nusize[0];
203 if ( pex->sizeZ() > 1 ) nusize[2] = nusize[0];
204 pex->resize(nusize, translate, FILL_USER, 0);
205 }
206
207 // write_img("pex.pif", pex);
208
209 pex->power_spectrum(flags);
210
211 // write_img("pex.pif", pex);
212
213 return pex;
214 }
215
216 Bimage* Bimage::defocus_scale(long nn, double df, double df2, double iCL2, int fill_type)
217 {
218 Bimage* ps = new Bimage(Float, TSimple, size(), 1);
219
220 long i, j, xx, yy;
221 double interval(1.0/(image[nn].sampling()[0]*x));
222 double s1, s2, f1(iCL2*df), f2(2*iCL2*df2), len, fill(image[nn].background());
223 vector<double> vs(x,0);
224
225 for ( size_t i=0; i<vs.size(); ++i ) {
226 s2 = interval*i;
227 s2 *= s2;
228 s1 = f1 - sqrt(f1*f1 + s2*s2 - f2*s2);
229 vs[i] = sqrt(s1)/interval;
230 }
231
232 Vector3<double> dis, coor, ori(size()/2);
233
234 for ( i=0, yy=0; yy<y; ++yy ) {
235 dis[1] = double(yy) - ori[1];
236 for ( xx=0; xx<x; ++xx, ++i ) {
237 dis[0] = double(xx) - ori[0];
238 len = dis.length();
239 coor = ori;
240 if ( len ) {
241 j = long(len);
242 f2 = len - j;
243 f1 = 1 - f2;
244 coor += dis * (f1*vs[j] + f2*vs[j+1])/len;
245 }
246 ps->set(i, interpolate(coor, nn, fill));
247 }
248 }
249
250 return ps;
251 }
252
272 Bimage* Bimage::powerspectrum_tilted(long img_num, Vector3<long> tile_size,
273 double tilt_axis, double tilt_angle, double defocus, double iCL2, int flags)
274 {
275 if ( image->origin()[0] <= 0 || image->origin()[1] <= 0 )
276 image->origin(size()/2);
277
278 set_tile_size(tile_size, size());
279
280 long nn;
281 Vector3<long> start, region(size()), step_size(tile_size/2);
282 Vector3<long> edge_rect(tile_size/1.5), edge_start((tile_size-edge_rect)/2);
283 edge_rect = edge_rect.max(1);
284
285 // cout << tile_size << tab << edge_rect << tab << edge_start << endl;
286
287 // The coordinates are the lower-left corners of the tiles
288 vector<Vector3<long>> coor = tile_coordinates(start, region, tile_size, step_size, 0);
289
290 Bimage* pex = extract_tiles(img_num, coor, tile_size);
291
292 pex->edge(1, edge_rect, edge_start, edge_start[0]/2.0, FILL_AVERAGE, 0);
293
294 pex->power_spectrum(5);
295 pex->calculate_background();
296
297 double ddef, ca(cos(tilt_axis)), sa(sin(tilt_axis)), tt(tan(tilt_angle));
298 Bimage* pt;
299 Vector3<double> vec, origin(pex->size()/2), scale(1,1,1), translate;
300 Matrix3 mat(1);
301
302 if ( verbose & VERB_DEBUG ) {
303 cout << "DEBUG Bimage::powerspectrum_tilted: origin=" << image->origin() << endl;
304 cout << "DEBUG Bimage::powerspectrum_tilted: ca=" << ca << " sa=" << sa << " tt=" << tt << endl;
305 }
306
307 // The scale is calculated so that it compensates for the tile defocus
308 // So the defocus difference is subtracted from the reference defocus
309 // This is equivalent to negating the pixel vector or negating the tilt axis
310 // if ( verbose )
311 // cout << "Tile\tx\ty\t∆f" << endl;
312 for ( nn=0; nn<pex->images(); nn++ ) {
313 vec = (origin - image->origin() + coor[nn])*image->sampling();
314 ddef = (vec[0]*sa - vec[1]*ca)*tt;
315 // if ( iCL2 > 0.002 ) {
316 scale[0] = scale[1] = sqrt(defocus/(defocus + ddef));
317 pt = pex->transform(nn, pex->size(), scale, origin, translate, mat, FILL_BACKGROUND);
318 // } else {
319 // pt = pex->defocus_scale(nn, defocus, defocus + ddef, iCL2, FILL_BACKGROUND);
320 // }
321 pex->replace(nn, pt);
322 // if ( verbose )
323 // cout << nn << tab << vec[0] << tab << vec[1] << tab << ddef << endl;
324 delete pt;
325 }
326
327 if ( flags & 2 )
328 pex->average_images();
329
330 pex->origin(pex->size()/2);
331
332 return pex;
333 }
334
354 Bimage* Bimage::powerspectrum_tiled_and_tilted(Vector3<long> tile_size,
355 double tilt_axis, double tilt_angle, double tilt_offset,
356 double defocus, double iCL2, int flags)
357 {
358 long nn, nimg(n);
359 Bimage* ps1 = NULL;
360 if ( flags & 16 ) nimg = 1;
361
362 set_tile_size(tile_size, size());
363
364 Bimage* ps = new Bimage(Float, TSimple, tile_size, nimg);
365 ps->sampling(sampling());
366
367 for ( nn = 0; nn<n; nn++ ) {
368 if ( fabs(tilt_angle) > 1e-6 ) {
369 if ( defocus > 0 )
370 ps1 = powerspectrum_tilted(nn, tile_size, tilt_axis, tilt_angle, defocus, iCL2, flags);
371 else
372 ps1 = powerspectrum_tilt_axis(nn, tile_size, tilt_axis, tilt_offset, flags);
373 } else
374 ps1 = powerspectrum_tiled(nn, tile_size, flags);
375 if ( flags & 16 ) {
376 ps->add(ps1);
377 } else {
378 ps->replace(nn, ps1);
379 }
380 delete ps1;
381 }
382
383 if ( flags & 16 ) ps->multiply(1.0/nn);
384
385 ps->sampling(sampling());
386 ps->statistics();
387 ps->file_name(file_name());
388 ps->origin(ps->size()/2);
389
390 return ps;
391 }
392
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
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2828
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bimage.h
Header file for image class.
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Vector3::length
double length()
Definition: Vector3.h:222
Bimage::powerspectrum_tiled_and_tilted
Bimage * powerspectrum_tiled_and_tilted(Vector3< long > tile_size, double tilt_axis, double tilt_angle, double tilt_offset, double defocus, double iCL2, int flags=0)
Prepares a tiled powerspectrum from a tilted image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:354
Bimage::add
void add(long j, double v)
Definition: Bimage.h:308
Bimage::file_name
string & file_name()
Definition: Bimage.h:231
Bimage::fft
int fft()
Definition: Bimage.h:822
utilities.h
Header file for general utilities.
Bimage::powerspectrum_tiled
Bimage * powerspectrum_tiled(long img_num, Vector3< long > tile_size, int flags=0)
Prepares a tiled power spectrum from an image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:97
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bimage::power_spectrum
int power_spectrum(int flags=0)
Calculates a power spectrum.
Definition: Bimage_powerspectrum.cpp:48
Bimage::edge
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Definition: Bimage_edit.cpp:319
Matrix3
Definition: Matrix3.h:17
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:173
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1005
Bimage::logarithm
void logarithm()
Calculates the logarithm of the image data.
Definition: Bimage_rescale.cpp:728
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:312
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:713
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:44
Bimage::powerspectrum_tilt_axis
Bimage * powerspectrum_tilt_axis(long img_num, Vector3< long > tile_size, double tilt_axis, double tilt_offset, int flags=0)
Prepares a tiled powerspectrum from a tilted image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:154
Bimage::interpolate
double interpolate(long cc, double xx, double yy=0, double zz=0, long nn=0, double fill=0) const
Interpolates using a given location.
Definition: Bimage.cpp:1392
Bimage::average_images
void average_images()
Definition: Bimage.h:716
Bimage::tile_coordinates
vector< Vector3< long > > tile_coordinates(Vector3< long > &start, Vector3< long > &region, Vector3< long > &tile_size, Vector3< long > &step_size, int exceed)
Generates a set of tile coordinates for an image.
Definition: Bimage_extract.cpp:263
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Bimage::defocus_scale
Bimage * defocus_scale(long nn, double df, double df2, double iCL2, int fill_type)
Definition: Bimage_powerspectrum.cpp:216
Vector3::max
Type max()
Definition: Vector3.h:175
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3288
set_tile_size
int set_tile_size(Vector3< long > &tile_size, Vector3< long > img_size)
Definition: Bimage_powerspectrum.cpp:15
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28
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
Bimage::complex_to_intensities
void complex_to_intensities()
The intensities from a complex image is written to a simple image.
Definition: Bimage_complex.cpp:92
Bimage::show_maximum
double show_maximum()
Definition: Bimage.h:559
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
FILL_USER
#define FILL_USER
Definition: utilities.h:172
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:123
verbose
int verbose
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
Bimage::pad
int pad(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:351
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< long >
Vector3::min
Type min()
Definition: Vector3.h:169
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage::powerspectrum_tilted
Bimage * powerspectrum_tilted(long img_num, Vector3< long > tile_size, double tilt_axis, double tilt_angle, double defocus, double iCL2, int flags=0)
Prepares a tiled powerspectrum from a tilted image for determining CTF parameters.
Definition: Bimage_powerspectrum.cpp:272
Bimage::images
long images()
Definition: Bimage.h:491
NoTransform
Definition: Bimage.h:40
Bimage::fill
void fill(double v)
Definition: Bimage.h:698
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563