Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_powerspectrum.cpp
Go to the documentation of this file.
1 
9 #include "img_fourier.h"
10 #include "img_complex.h"
11 #include "img_interpolate.h"
12 #include "img_colour.h"
13 #include "img_extract.h"
14 #include "img_combine.h"
15 #include "trigonometry.h"
16 #include "utilities.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
35 int img_power_spectrum(Bimage* p, int flags)
36 {
37  if ( verbose & VERB_DEBUG )
38  cout << "DEBUG img_power_spectrum: flags=" << flags << endl;
39 
40  p->color_to_simple();
41  p->change_type(Float);
42 
43  if ( flags & 1 ) p->rescale_to_avg_std(0, 1);
44 
45  if ( img_fft(FFTW_FORWARD, p) )
46  return error_show("img_power_spectrum", __FILE__, __LINE__);
47 
49 
51 
52  if ( flags & 2 ) img_average(p);
53 
54 // p->set(0, 0);
55 
56  p->statistics();
57 
58  p->origin(0,0,0);
59 
60  p->show_maximum((p->complex(1)).power());
61 
62  if ( flags & 4 ) p->center_wrap();
63 
64  if ( flags & 8 ) p->logarithm(); // Get logarithm of intensities
65 
66  return 0;
67 }
68 
85 {
86  // If the image is square it is assumed to be a CCD image or extracted to be square
87  // and no padding around the edges is necessary
88  int pad(0);
89 
90  // Minimum and maximum tile sizes allowed
91  int min_size = 64;
92  int max_size = 1024;
93 
94  // If the image is not square, it is assumed to be a micrograph with severe edge
95  // features that will affect the power spectrum
96  if ( p->sizeX() != p->sizeY() && p->sizeX() > max_size && p->sizeY() > max_size) {
97  pad = p->sizeX()/10;
98  if ( pad < p->sizeY()/10 ) pad = p->sizeY()/10;
99  }
100 
101  Vector3<int> start;
102  Vector3<int> ext_size;
103 
104  tile_size = tile_size.max(min_size);
105 
106  if ( tile_size.x() > p->sizeX() ) tile_size[0] = p->sizeX();
107  if ( tile_size.y() > p->sizeY() ) tile_size[1] = p->sizeY();
108  if ( tile_size.z() > p->sizeZ() ) tile_size[2] = p->sizeZ();
109 
110  tile_size = tile_size.min(max_size);
111 
112  ext_size[0] = tile_size.x()*((p->sizeX() - 2*pad)/tile_size.x());
113  ext_size[1] = tile_size.y()*((p->sizeY() - 2*pad)/tile_size.y());
114  ext_size[2] = tile_size.z()*((p->sizeZ() - 2*pad)/tile_size.z());
115  ext_size = ext_size.max(1);
116 
117  start[0] = (p->sizeX() - ext_size.x())/2;
118  start[1] = (p->sizeY() - ext_size.y())/2;
119  start[2] = (p->sizeZ() - ext_size.z())/2;
120 
121  if ( verbose & VERB_DEBUG ) {
122  cout << "DEBUG img_tiled_powerspectrum: start=" << start << endl;
123  cout << "DEBUG img_tiled_powerspectrum: tile_size=" << tile_size << endl;
124  }
125 
126  // If a single image assume a micrograph that needs to be tiled
127  Bimage* pex = img_extract_tiles(p, -1, start, ext_size, tile_size, 0);
128 
129  Vector3<int> translate;
130  Vector3<int> size(1,1,1);
131  if ( pex->sizeX() != pex->sizeY() ) {
132  size[0] = pex->sizeX();
133  if ( pex->sizeY() > 1 && size.x() > pex->sizeY() ) size[0] = pex->sizeY();
134  if ( pex->sizeZ() > 1 && size.x() > pex->sizeZ() ) size[0] = pex->sizeZ();
135  if ( pex->sizeY() > 1 ) size[1] = size.x();
136  if ( pex->sizeZ() > 1 ) size[2] = size.x();
137  pex->resize(size, translate, FILL_USER, 0);
138  }
139 
140  img_power_spectrum(pex, flags);
141 
142  return pex;
143 }
144 
162  double tilt_axis, double tilt_offset, int flags)
163 {
164  // Minimum and maximum tile sizes allowed
165  int min_size = 64;
166  int max_size = 1024;
167 
168  int i, x, y, ntiles = 0;
169  double tan_a = btan(tilt_axis);
170  double inv_tan_a = 1.0/tan_a;
171  double ratang = batan2(p->sizeY(), p->sizeX());
172  Vector3<int> origin(p->size()/2);
173  Vector3<int>* tile = NULL;
174 
175  tile_size = tile_size.max(min_size);
176 
177  if ( tile_size.x() > p->sizeX() ) tile_size[0] = p->sizeX();
178  if ( tile_size.y() > p->sizeY() ) tile_size[1] = p->sizeY();
179  if ( tile_size.z() > p->sizeZ() ) tile_size[2] = p->sizeZ();
180 
181  tile_size = tile_size.min(max_size);
182 
183 // Vector3<int> tile_half = tile_size/2;
184 
185  if ( fabs(tilt_axis) < ratang || fabs(tilt_axis) > M_PI - ratang ) {
186  origin[1] += (int)tilt_offset;
187  ntiles = p->sizeX()/tile_size.x();
188  tile = new Vector3<int>[ntiles];
189  for ( i=0, x = origin.x() - (ntiles * tile_size.x())/2; i<ntiles; i++, x += tile_size.x() ) {
190  tile[i][0] = x;
191  tile[i][1] = (int) (origin.y() + (x - origin.x())*tan_a);
192  }
193  } else {
194  origin[0] += (int)tilt_offset;
195  ntiles = p->sizeY()/tile_size.y();
196  tile = new Vector3<int>[ntiles];
197  for ( i=0, y = origin.y() - (ntiles * tile_size.y())/2; i<ntiles; i++, y += tile_size.y() ) {
198  tile[i][0] = (int) (origin.x() + (y - origin.y())*inv_tan_a);
199  tile[i][1] = y;
200  }
201  }
202 
203  if ( verbose & VERB_DEBUG ) {
204  cout << "DEBUG img_tilt_axis_powerspectrum: origin=" << origin << endl;
205  cout << "DEBUG img_tilt_axis_powerspectrum: tile_size=" << tile_size << endl;
206  cout << "DEBUG img_tilt_axis_powerspectrum: ntiles=" << ntiles << endl;
207  for ( i=0; i<ntiles; i++ )
208  cout << "DEBUG img_tilt_axis_powerspectrum: " << tile[i].x() << " " << tile[i].y() << endl;
209  }
210 
211  // If a single image assume a micrograph that needs to be tiled
212  Bimage* pex = img_extract_tiles_at_coords(p, 0, ntiles, tile, tile_size);
213 
214  delete[] tile;
215 
216  Vector3<int> translate;
217  Vector3<int> size(1,1,1);
218  if ( pex->sizeX() != pex->sizeY() ) {
219  size[0] = pex->sizeX();
220  if ( pex->sizeY() > 1 && size.x() > pex->sizeY() ) size[0] = pex->sizeY();
221  if ( pex->sizeZ() > 1 && size.x() > pex->sizeZ() ) size[0] = pex->sizeZ();
222  if ( pex->sizeY() > 1 ) size[1] = size.x();
223  if ( pex->sizeZ() > 1 ) size[2] = size.x();
224  pex->resize(size, translate, FILL_USER, 0);
225  }
226 
227 // write_img("pex.pif", pex);
228 
229  img_power_spectrum(pex, flags);
230 
231 // write_img("pex.pif", pex);
232 
233  return pex;
234 }
235 
256 {
257  if ( p->compound_type() != TComplex ) return NULL;
258 
259  p->change_type(Float);
260 
261  p->statistics();
262 
263  if ( verbose & VERB_PROCESS ) {
264  cout << "Generating a phase-coloured power spectrum" << endl;
265  } else if ( verbose & VERB_LABEL )
266  cout << "Generating a phase-coloured power spectrum" << endl;
267 
268  unsigned long i, datasize = p->sizeX()*p->sizeY()*p->sizeZ()*p->images();
269  double amp_ratio;
270 
271  if ( scale <= 0 ) scale = 1/(p->average() + p->standard_deviation());
272  else scale /= p->average() + p->standard_deviation();
273 
274  scale *= 255;
275 
276  // Shift the phases
278 
279  Bimage* ps = new Bimage(UCharacter, TRGB, p->sizeX(), p->sizeY(), p->sizeZ(), p->images());
280 
281  RGB<unsigned char> rgb;
282 
283  for ( i=0; i<datasize; i++ ) {
284  amp_ratio = (p->complex(i)).amp()*scale; // Weigh with amplitudes
285  if ( amp_ratio > 0 ) {
286  if ( amp_ratio > 255 ) amp_ratio = 255;
287  rgb.phase((p->complex(i)).phi(), amp_ratio);
288  ps->set(i, rgb);
289  }
290  }
291 
292  ps->center_wrap();
293 
295 
296  return ps;
297 }
298 
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1440
Type z()
Definition: Vector3.h:141
void sizeZ(size_t nz)
Definition: Bimage.h:383
int img_write_phase_colour_wheel()
Generates a phase color wheel.
Definition: img_colour.cpp:29
Bimage * img_extract_tiles_at_coords(Bimage *p, int img_number, int ntiles, Vector3< int > *coords, Vector3< int > tile_size)
Extracts a set of tiles at specified positions from an image into a new image.
Header file for general utilities.
#define VERB_DEBUG
Definition: utilities.h:104
CompoundType compound_type()
Definition: Bimage.h:294
Definition: Bimage.h:57
Bimage * img_tilt_axis_powerspectrum(Bimage *p, Vector3< int > tile_size, double tilt_axis, double tilt_offset, int flags)
Prepares an tilted image for determining CTF parameters.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:616
void logarithm()
Calculates the logarithm of the image data.
#define VERB_PROCESS
Definition: utilities.h:99
int center_wrap()
Centers an image with wrapping.
Type max()
Definition: Vector3.h:151
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1150
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.cpp:2199
Functions to handle complex data sets.
Functions to handle RGB images.
Vector3< int > size()
Definition: Bimage.h:378
General image parameter class.
Definition: Bimage.h:150
#define M_PI
Definition: utilities.h:51
Complex< double > complex(size_t j)
Returns a complex value at the given index.
Definition: Bimage.cpp:824
Header file for functions to extract parts of images.
double btan(double x)
Avoid non-zero values for tan(PI) and tan(-PI). ATTENTION: tan function return a bigg value for tan(P...
Library routines for interpolating images.
General FFT for n-dimensional data.
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Header file for combining two images in various ways.
Bimage * img_extract_tiles(Bimage *p, int img_number, Vector3< int > start, Vector3< int > size, Vector3< int > tile_size, int exceed)
Extracts a set of tiles from an image into a new image.
Definition: Color.h:30
int img_fft(Bimage *p)
Fast Fourier transforms an image.
Definition: img_fourier.cpp:42
Type min()
Definition: Vector3.h:145
double batan2(double y, double x)
Sets small values to zero before calling atan2.
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
void complex_to_intensities()
The intensities from a complex image is written to a simple image.
double show_maximum()
Definition: Bimage.h:494
Type x()
Definition: Vector3.h:139
Header file for trigonometry functions.
#define FILL_USER
Definition: utilities.h:139
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:916
int img_average(Bimage *p)
Averages images in a multi-image file.
Definition: img_combine.cpp:30
double standard_deviation()
Definition: Bimage.h:480
Definition: Bimage.h:72
size_t images()
Definition: Bimage.h:460
void sizeX(size_t nx)
Definition: Bimage.h:381
void sizeY(size_t ny)
Definition: Bimage.h:382
int verbose
void color_to_simple()
A color image is converted to a simple image.
Bimage * img_colour_power_spectrum(Bimage *p, float scale)
Generates a power spectrum with phases coloured according to a colour wheel.
Bimage * img_tiled_powerspectrum(Bimage *p, Vector3< int > tile_size, int flags)
Prepares an image for determining CTF parameters.
#define VERB_LABEL
Definition: utilities.h:98
Type y()
Definition: Vector3.h:140
void phase(double phi, double amp_ratio)
Definition: Color.h:115
int resize(Vector3< int > nusize, Vector3< int > translate, int fill_type, double fill)
Resizes without interpolation or rescaling.
int img_power_spectrum(Bimage *p, int flags)
Calculates a power spectrum.
FourierType fourier_type()
Definition: Bimage.h:372
void origin(double ox, double oy, double oz)
Definition: Bimage.h:498