Xmipp  v3.23.11-Nereus
image_find_center.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Monica Chagoyen
4  * Carlos Oscar coss@cnb.csic.es (2011)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia, CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 /****************************************************************************/
27 /* Program for finding the center of an image. The original in Fortran was */
28 /* written by A. Santisteban. For any information about the program, contact*/
29 /* him. Translated into C by Juan P. Secilla (MSC) Jun/86 */
30 /****************************************************************************/
31 
32 #include "core/metadata_vec.h"
33 #include "core/xmipp_program.h"
34 #include "core/xmipp_image.h"
36 
37 // Old code -----------------------------------------------------------------
38 //constexpr int NATURAL = 1; natural, 1 byte/pixel format
39 constexpr int INTFMT = 2; /* integer, 2 byte/pixel format */
40 constexpr int LONGFMT = 3; /* long, 4 bytes/pixel format */
41 constexpr int FLOATFMT = 4; /* float, 4 bytes/pixel format */
42 constexpr int FOURIER = 5; /* Fourier transform format */
43 constexpr int SPIDER = 6; /* Spider (header) format */
44 constexpr int DEF_IT = 5; /*Default iterations number */
45 constexpr int DEF_DEL = 2;
46 constexpr int DEF_IN = 2;
47 constexpr int ALLOC_SIZE = 65000; /* Allocation unit size */
48 typedef unsigned char BYTE; /*** Only this and float are used ***/
49 typedef unsigned short UWORD;
50 typedef unsigned long ULONG;
51 
52 float coseno [1281];
54 float r1, r2, r3, cxp1, cxp2, cxp3, cxm1, cxm2, cxm3, rh = 1000., xc0, yc0, del,
55  rbajo, ralto, zzz0;
57 int ir, m, mu1, mu4, ntot, mt, idz, ncic2, ntot4;
58 
59 // Routines needed --------------------------------------------------------
60 /**************************************************************************/
61 /* Image allocation routine. Returns a pointer to pointers to individually*/
62 /* allocated rows of the image. (row, col) are the image dimensions. */
63 /* Format indicates the format of the resultant image. */
64 /* Returns: the pointer to the pointers if everything OK, NULL if there */
65 /* is not enough memory. In this latter case, it leaves everything as was */
66 /* before the call. */
67 /* Version 2.0: allocates blocks of rows to improve speed */
68 /* Version 3.0: modified for OS/2 1.0 ==> halloc used instead of malloc */
69 /* just because malloc does not give more then 4Mb, and halloc does. */
70 /* should work in any environment just changing halloc for malloc */
71 /**************************************************************************/
72 
73 void **imalloc(int row, int col, int format)
74 {
75  size_t i, j, k; /* Counters */
76  unsigned element_size; /* Size of element to allocate */
77  unsigned pointer_size; /* Id. of pointers */
78  char **temp; /* Temporal value to work with */
79  unsigned no_blocks; /* No. of ALLOC_SIZE blocks to allocate */
80  long tot_size; /* Total allocation size */
81  unsigned row_alloc; /* No. of rows to alloc at the same time*/
82  unsigned all_size; /* No. of bits to alloc at one time */
83  unsigned rest_size; /* Rest of bytes to alloc */
84  unsigned rest_rows; /* Rest of rows to alloc. */
85  char *aux; /* Aux. pointer */
86 
87 
88  /******************* Assign appropriate value to size flag ******************/
89 
90  if (format == FOURIER)
91  col += 2; /* Special treatment for FFT format (see foutrans.c) */
92 
93  switch (format)
94  {
95  case NATURAL:
96  element_size = sizeof(BYTE);
97  pointer_size = sizeof(BYTE *);
98  break;
99  case INTFMT:
100  element_size = sizeof(UWORD);
101  pointer_size = sizeof(UWORD *);
102  break;
103  case LONGFMT:
104  element_size = sizeof(ULONG);
105  pointer_size = sizeof(ULONG *);
106  break;
107  case FLOATFMT:
108  case FOURIER:
109  element_size = sizeof(float);
110  pointer_size = sizeof(float *);
111  break;
112  default:
113  return NULL;
114  }
115 
116  row_alloc = ALLOC_SIZE / (col * element_size); /* No. of rows to alloc */
117  all_size = row_alloc * col * element_size;
118  tot_size = ((long) element_size) * ((long) row) * ((long) col);
119  no_blocks = tot_size / all_size;
120  rest_size = tot_size - ((long) no_blocks) * ((long) all_size);
121  rest_rows = rest_size / (col * element_size);
122 
123  /********************* Allocate base pointer ********************************/
124 
125  if ((temp = (char **) malloc(row * pointer_size)) == NULL)
126  return NULL; /* Not even this little bit of memory */
127 
128  /*********************** Allocate most blocks *******************************/
129 
130  j = 0;
131  for (i = 0; i < no_blocks; i++)
132  {
133  if ((aux = (char *)malloc((long)all_size)) == NULL)
134  {
135  for (j = 0; j < i; j++)
136  free(temp[j*row_alloc]);
137  free((char *) temp);
138  return NULL;
139  }
140  for (k = 0; k < row_alloc; k++, j++)
141  temp [j] = aux + k * col * element_size;
142  }
143 
144  /*************************** Alloc the last block ***************************/
145 
146  if (rest_size != 0)
147  {
148  if ((aux = (char *)malloc((long)rest_size)) == NULL)
149  {
150  for (j = 0; j < no_blocks; j++)
151  free(temp[j*row_alloc]);
152  free(temp);
153  return NULL;
154  }
155  for (k = 0; k < rest_rows; k++, j++)
156  temp [j] = aux + k * col * element_size;
157  }
158 
159  /************************* return OK pointer value **********************/
160 
161  return (void **)temp;
162 }
163 
164 /**************************************************************************/
165 /* This function frees an image previously allocated with image_alloc. */
166 /* hfree used instead of free (see imalloc header). Change hfree to free */
167 /* for portability */
168 /**************************************************************************/
169 
170 void imfree(char **image, int row, int col, int format)
171 
172 {
173  size_t i; /* Counters */
174  unsigned element_size; /* Size of element to allocate */
175  unsigned no_blocks; /* No. of ALLOC_SIZE blocks to allocate */
176  long tot_size; /* Total allocation size */
177  unsigned row_alloc; /* No. of rows to alloc at the same time*/
178  unsigned all_size; /* No. of bits to alloc at one time */
179 
180  if (image == NULL) /* No allocation at the moment */
181  return;
182 
183  if (format == FOURIER)
184  col += 2; /* Special treatment for FFT format (see foutrans.c) */
185 
186  switch (format)
187  {
188  case NATURAL:
189  element_size = sizeof(BYTE);
190  break;
191  case INTFMT:
192  element_size = sizeof(UWORD);
193  break;
194  case LONGFMT:
195  element_size = sizeof(ULONG);
196  break;
197  case FLOATFMT:
198  case FOURIER:
199  element_size = sizeof(float);
200  break;
201  default:
202  return;
203  }
204 
205  row_alloc = ALLOC_SIZE / (col * element_size); /* No. of rows to free */
206  all_size = row_alloc * col * element_size;
207  tot_size = ((long) element_size) * ((long) row) * ((long) col);
208  no_blocks = tot_size / all_size;
209 
210  /*************************** Free most blocks *******************************/
211 
212  for (i = 0; i < no_blocks; i++)
213  if (image [i*row_alloc] != NULL)
214  free(image [i*row_alloc]);
215 
216  /*************************** Free the last block ****************************/
217 
218  if (image [i*row_alloc] != NULL)
219  free(image [i*row_alloc]);
220 
221  free(image);
222 }
223 
224 float conv1x(double y, double x)
225 /**************************************************************************/
226 /* Returns the value of the image at the point (y, x) using linear interp-*/
227 /* olation. For higher accuracy, use "conv3x" (available in 370 assembly */
228 /* languaje. */
229 /**************************************************************************/
230 { short i, j; /* Row and column */
231  float intfila1, intfila2; /* Partial row interpolations */
232  float escala; /* Scale factor */
233 
234  j = (short int) y; /* Trunc the x, y coordinates to short */
235  i = (short int) x;
236 
237  escala = y - j;
238  /* 1st row interpolation */
239  intfila1 = imagen[i][j] + escala * ((short)imagen[i][j+1] - (short)imagen[i][j]);
240  /* 2nd row interpolation */
241  intfila2 = imagen[i+1][j] + escala * ((short)imagen[i+1][j+1] - (short)imagen [i+1][j]);
242  /* Column interpolation */
243  return intfila1 + (x - i)*(intfila2 - intfila1);
244 }
245 
246 void ergrot(double xc0, double yc0, float* zzz)
247 {
248  //double xc0, yc0;
249  //float *zzz; /* It hides global variable, handle with care */
250 
251  static float a[266], b[257];
252  double za, zb;
253  double xp1, xp2, xp3, xm1, xm2, xm3;
254  double axp1, axp2, axp3, axm1, axm2, axm3;
255  short i7, iz2, iz1, kv, l1, l2, i1, i, j;
256  float r, x, y, zz, ai, bi;
257  float bj, aj, z;
258 
259  r = r1 - r3;
260  axp1 = 0.;
261  axm1 = 0.;
262  axp2 = 0.;
263  axm2 = 0.;
264  axp3 = 0.;
265  axm3 = 0.;
266  for (i7 = 1; i7 <= ir; i7++) /* do 31 */
267  { r += r3;
268  iz2 = ntot - m + mu4;
269  iz1 = iz2 - 1;
270  for (i = 1; i <= mu; i++) /* do 13 */
271  { iz1 += idz;
272  iz2 += idz;
273  za = 0.;
274  zb = 0.;
275  for (kv = 1; kv <= ncic; kv++) /* do 11 */
276  { iz1 += m;
277  x = xc0 + r * coseno[iz1];
278  y = yc0 + r * coseno[iz1-mu4];
279  z = conv1x(y, x);
280  iz1 += m;
281  x = xc0 + r * coseno[iz1];
282  y = yc0 + r * coseno[iz1-mu4];
283  zz = conv1x(y, x);
284  zb += (z - zz);
285  iz2 += m;
286  x = xc0 + r * coseno[iz2];
287  y = yc0 + r * coseno[iz2-mu4];
288  z = conv1x(y, x);
289  iz2 += m;
290  x = xc0 + r * coseno[iz2];
291  y = yc0 + r * coseno[iz2-mu4];
292  zz = conv1x(y, x);
293  za += (z - zz);
294  }
295  b[i] = zb;
296  a[i] = za;
297  }
298  xp1 = a[mu];
299  xp2 = b[mu];
300  xm2 = xp2 * xp2;
301  xp3 = xp1 * xp2 * coseno[mu4-ncic];
302  xm3 = xp3;
303  xp2 = xm2 * coseno[mu4-ncic2];
304  xp1 = xp1 * xp1;
305  xm1 = xp1;
306  for (i = 1; i <= mu1; i++) /* do 14 */
307  { l1 = 4 * i * ncic + mu4;
308  l2 = mu4;
309  ai = a[i];
310  bi = b[i];
311  xp1 += (ai * ai * coseno[l1]);
312  xp2 += (bi * bi * coseno[l1-ncic2]);
313  xp3 += (ai * bi * coseno[l1-ncic]);
314  xm1 += (ai * ai);
315  xm2 += (bi * bi);
316  xm3 += (ai * bi * coseno[mu4+ncic]);
317  i1 = i + 1;
318  ai = a[i];
319  bi = b[i];
320  for (j = i1; j <= mu; j++) /* do 15 */
321  { l1 += ncic2;
322  l2 += ncic2;
323  aj = a[j];
324  bj = b[j];
325  double ajai2=2.0*aj*ai;
326  double bjbi2=2.0*bj*bi;
327  double aibj=ai*bj;
328  double ajbi=aj*bi;
329  xp1 += (ajai2 * coseno[l1]);
330  xm1 += (ajai2 * coseno[l2]);
331  xp2 += (bjbi2 * coseno[l1-ncic2]);
332  xm2 += (bjbi2 * coseno[l2]);
333  xp3 += ((aibj + ajbi) * coseno[l1-ncic]);
334  xm3 += (aibj * coseno[l2-ncic] + ajbi * coseno[l2+ncic]);
335  }
336  }
337  axp1 += xp1 * r;
338  axm1 += xm1 * r;
339  axp2 += xp2 * r;
340  axm2 += xm2 * r;
341  axp3 += xp3 * r;
342  axm3 += xm3 * r;
343  }
344  (*zzz) = axp1 * cxp1 + axp2 * cxp2 + axp3 * cxp3 + axm1 * cxm1 + axm2 * cxm2 + axm3 * cxm3;
345  (*zzz) /= (zzz0 * ir);
346 }
347 
348 void suaviza()
349 {
350  unsigned char pix;
351  short i, j, k, n;
352  long isuma;
353  float racua, rbcua, dr, r;
354 
355  isuma = 0;
356  n = 0;
357  i = 0;
358  racua = ralto * ralto;
359  rbcua = rbajo * rbajo;
360  dr = 3.141592653 / (ralto - rbajo);
361  for (k = 1; k <= lancho; k++)
362  {
363  if (k % 16 == 0)
364  for (j = 1; j <= largo; j++)
365  {
366  r = (xc0 - k) * (xc0 - k) + (yc0 - j) * (yc0 - j);
367  if (r > racua)
368  {
369  isuma += imagen [k][j];
370  n++;
371  }
372  }
373  }
374  if (n != 0)
375  i = (short int)(((float) isuma) / n + .5);
376  m = i;
377  pix = i;
378  for (k = 1; k <= lancho; k++)
379  {
380  if (k % 16 == 0)
381  for (j = 1; j <= largo; j++)
382  {
383  r = (xc0 - k) * (xc0 - k) + (yc0 - j) * (yc0 - j);
384  if (r < rbcua)
385  continue;
386  if (r > racua)
387  {
388  imagen[k][j] = pix;
389  continue;
390  }
391  r = sqrt(r);
392  r -= rbajo;
393  imagen[k][j] = (unsigned char)((0.5 + 0.5 * cos(dr * r)) * (imagen[k][j] - m) + m);
394  }
395  }
396  printf("\nImage smoothed. Outer mean = %d\n", m);
397 }
398 
399 void busca()
400 {
401  static float f[22][22];
402  float pi = 3.141592653;
403  short in1, mu5, i, mu3, ind, ind1, indice, iflag, j, ix=0, iy=0, k;
404  float h, th, costh, sinth, fi, anc, xk0, yk0, hpi, x, y, z, xx, yy;
405  float b, g, d1, e1, ext;
406  int count = 0;
407 
408  suaviza();
409  in = XMIPP_MIN(in, 10);
410  mu1 = mu - 1;
411  m = 2 * mu;
412  mu4 = mu * ncic;
413  mt = 2 * m;
414  ntot = mt * ncic;
415  ntot4 = ntot + mu4 + ncic;
416  zzz0 = ntot;
417  ncic2 = 2 * ncic;
418  in1 = in + 1;
419  h = 2. * pi / ntot;
420  idz = 2 - ntot;
421  th = ncic * h;
422  costh = cos(th);
423  sinth = sin(th);
424  b = 2. / (th * th) * (1. + costh * costh - sin(2. * th) / th);
425  g = 4. / (th * th) * (sinth / th - costh);
426  d1 = 2. * th / 45.;
427  e1 = d1 * sinth * 2.;
428  d1 *= sin(2. * th);
429  mu5 = mu4 - 1;
430  for (i = 1; i <= mu5; i++)
431  {
432  fi = i * h;
433  coseno[i] = sin(fi);
434  }
435  coseno[mu4] = 1.;
436  mu3 = 2 * mu4;
437  for (i = 1; i <= mu5; i++)
438  {
439  coseno[mu3-i] = coseno[i];
440  coseno[mu3+i] = -coseno[i];
441  coseno[ntot-i] = -coseno[i];
442  coseno[ntot+i] = coseno[i];
443  }
444  coseno[mu3] = 0.;
445  coseno[mu3+mu4] = -1.;
446  coseno[ntot] = 0.;
447  coseno[ntot+mu4] = 1.;
448  ind = 2 * in + 1;
449  ind1 = ind - 1;
450  indice = 0;
451  iflag = 0;
452 e9:
453  if (indice >= ni)
454  goto e10;
455  if (iflag == 2)
456  goto e22;
457  anc = (int)(3. + in * del);
458  xk0 = xc0;
459  yk0 = yc0;
460  rh = XMIPP_MIN(rh, r2);
461  rh = XMIPP_MIN(rh, xk0 - anc - 1.);
462  rh = XMIPP_MIN(rh, yk0 - anc - 1.);
463  rh = XMIPP_MIN(rh, largo - xk0 - anc);
464  rh = XMIPP_MIN(rh, lancho - yk0 - anc);
465  ir = (int)((rh - r1) / r3 + 1);
466  hpi = h / 2. / pi / ir;
467  cxp1 = 2. * b * e1 * hpi;
468  cxp2 = -2. * g * d1 * hpi;
469  cxp3 = 2. * (g * e1 - b * d1) * hpi;
470  cxm1 = (b * b + e1 * e1) * hpi;
471  cxm2 = (g * g + d1 * d1) * hpi;
472  cxm3 = 2. * (b * g - d1 * e1) * hpi;
473  /* if(iflag == 1) goto 21 */
474  if (iflag == 2)
475  goto e22;
476  x = xc0 - in * del;
477  for (i = 1; i <= ind; i++) /* do 5 */
478  { y = yc0 - in * del;
479  for (j = 1; j <= ind; j++) /*do 4 */
480  { ergrot(x, y, &z);
481  /* printf ("%10.3f%10.3f%10.5f%10.3f%10.3f AA\n", x,y,z,del,rh);*/
482  printf(".");
483  fflush(stdout);
484  z = 100. + indmul * z;
485  f[i][j] = z;
486  y += del;
487  }
488  x += del;
489  }
490  // Introduced by Sjors dd 28.9.2004 to avoid infinite loops
491  count++;
492  if (count > 1000)
493  goto s1;
494 e23:
495  ext = -1000000.;
496  for (i = 1; i <= ind; i++) /* do 7 */
497  for (j = 1; j <= ind; j++) /* do 7 */
498  { if (f[i][j] > ext)
499  {
500  ix = i;
501  iy = j;
502  ext = f[i][j];
503  }
504  }
505  xc0 = xc0 + (ix - in - 1) * del;
506  yc0 = yc0 + (iy - in - 1) * del;
507  if (ix == 1 || ix == ind || iy == 1 || iy == ind)
508  goto e8;
509  del /= in;
510  iflag = 2;
511  indice++;
512  goto e9;
513 e8:
514  iflag = 1;
515  goto e9;
516 e22:
517  f[1][1] = f[ix-1][iy-1];
518  f[ind][1] = f[ix+1][iy-1];
519  f[1][ind] = f[ix-1][iy+1];
520  f[ind][ind] = f[ix+1][iy+1];
521  f[1][in1] = f[ix-1][iy];
522  f[in1][1] = f[ix][iy-1];
523  f[ind][in1] = f[ix+1][iy];
524  f[in1][ind] = f[ix][iy+1];
525  f[in1][in1] = f[ix][iy];
526  x = xc0 - (in - 1) * del;
527  for (i = 2; i < ind1; i++) /* do 11 */
528  { y = yc0 - (in - 1) * del;
529  for (j = 2;j <= ind1; j++) /* do 12 */
530  { if (i == in1 && j == in1)
531  goto e13;
532  ergrot(x, y, &z);
533  /* printf ("%10.3f%10.3f%10.5f%10.3f BB \n", x,y,z,del);*/
534  printf(".");
535  fflush(stdout);
536  z = 100. + indmul * z;
537  f[i][j] = z;
538 e13:
539  y += del;
540  }
541  x += del;
542  }
543  x = xc0 - (in - 1) * del;
544  y = yc0 - (in - 1) * del;
545 
546  for (k = 2; k <= ind1; k++) /* do 16 */
547  { if (k == in1)
548  goto e17;
549  xx = xc0 - in * del;
550  ergrot(xx, y, &z);
551  /* printf ("%10.3f%10.3f%10.5f%10.3f CC \n", xx,y,z,del);*/
552  printf(".");
553  fflush(stdout);
554  z = 100. + indmul * z;
555  f[1][k] = z;
556  yy = yc0 - in * del;
557  ergrot(x, yy, &z);
558  /* printf ("%10.3f%10.3f%10.5f%10.3f DD \n", xx,y,z,del);*/
559  printf(".");
560  fflush(stdout);
561  z = 100. + indmul * z;
562  f[k][1] = z;
563  xx = xc0 + in * del;
564  ergrot(xx, y, &z);
565  /* printf ("%10.3f%10.3f%10.5f%10.3f EE \n", xx,y,z,del);*/
566  printf(".");
567  fflush(stdout);
568  z = 100. + indmul * z;
569  f[ind][k] = z;
570  yy = yc0 + in * del;
571  ergrot(x, yy, &z);
572  /* printf ("%10.3f%10.3f%10.5f%10.3f FF\n", x,yy,z,del);*/
573  printf(".");
574  fflush(stdout);
575  z = 100. + indmul * z;
576  f[k][ind] = z;
577 e17:
578  x += del;
579  y += del;
580  }
581  goto e23;
582 e10:
583  std::cout << "\nOptimal center coordinates: x= " << yc0-1 << " ,y= " << xc0-1 << " " << std::endl;
584  return;
585 s1:
586  yc0=xc0=-1;
587  printf("\nNot-converged\n");
588  return;
589 }
590 
591 // Wrapper to old code ----------------------------------------------------
593 {
594 public:
597 
599  double _r1, _r2, _r3, _r4;
600 
602  double x0, y0;
603 
605  int _ncic;
606 
608  int _indmul;
609 
612  {
613  addUsageLine("Find the best symmetry of rotation of an image or collection of images");
614  addUsageLine("+It is very useful when you want to calculate the rotational symmetry of ");
615  addUsageLine("+a set of images (in fact it computes the center of the average image). ");
616  addUsageLine("+Image dimensions must be less than 512x512.");
617  addSeeAlsoLine("xmipp_image_rotational_spectrum");
618  addParamsLine(" -i <file> : Image, image stack or image selfile");
619  addParamsLine(" --oroot <rootname> : Rootname for output files");
620  addParamsLine(" :+ <rootname>_center.xmd contains the image center");
621  addParamsLine(" :+ <rootname>_analyzed_image.xmp (if verbose>=2) contains the image that was actually analyzed");
622  addParamsLine("[--r1+ <radius=15>] : Lowest integration radius (% of the image radius)");
623  addParamsLine("[--r2+ <radius=80>] : Highest integration radius (% of the image radius)");
624  addParamsLine("[--r3+ <radius=90>] : Lowest smoothing radius (% of the image radius)");
625  addParamsLine("[--r4+ <radius=100>] : Highest smoothing radius (% of the image radius)");
626  addParamsLine("[--x0 <x>] : Initial center of rotation");
627  addParamsLine("[--y0 <y>] : Initial center of rotation");
628  addParamsLine("[--harm+ <n=1>] : Harmonic to optimize");
629  addParamsLine("[--opt+ <opt=-1>] : Type of optimization (-1=minimize, +1=maximize)");
630  addExampleLine("xmipp_image_find_center -i image.xmp");
631  }
632 
633  void readParams()
634  {
635  fnIn=getParam("-i");
636  fnOroot=getParam("--oroot");
637  _r1=getDoubleParam("--r1");
638  _r2=getDoubleParam("--r2");
639  _r3=getDoubleParam("--r3");
640  _r4=getDoubleParam("--r4");
641  if (checkParam("--x0"))
642  x0=getDoubleParam("--x0");
643  else
644  x0=-1;
645  if (checkParam("--y0"))
646  y0=getDoubleParam("--y0");
647  else
648  y0=-1;
649  _ncic=getIntParam("--harm");
650  _indmul=getIntParam("--opt");
651  }
652 
653  void show()
654  {
655  if (verbose==0)
656  return;
657  std::cout << "Input: " << fnIn << std::endl
658  << "Output root: " << fnOroot << std::endl
659  << "R1: " << _r1 << std::endl
660  << "R2: " << _r2 << std::endl
661  << "R3: " << _r3 << std::endl
662  << "R4: " << _r4 << std::endl
663  << "Harmonic: " << _ncic << std::endl
664  << "Opt: " << _indmul << std::endl
665  << "Initial center: (" << x0 << "," << y0 << ")\n"
666  ;
667  }
668 
669  void run()
670  {
671  show();
672  size_t id;
673  // Get the input image or the average of the input images
674  Image<double> I, Iaux;
675  if (fnIn.isMetaData())
676  {
677  MetaDataVec MD(fnIn);
678  int N=0;
679  for (size_t objId : MD.ids())
680  {
681  Iaux.readApplyGeo(MD, objId);
682  if (N==0)
683  I()=Iaux();
684  else
685  I()+=Iaux();
686  ++N;
687  }
688  I()/=N;
689  }
690  else
691  {
692  I.read(fnIn, HEADER);
693  size_t Xdim, Ydim, Zdim, Ndim;
694  I.getDimensions(Xdim, Ydim, Zdim, Ndim);
695  I.clear();
696  if (Ndim>1)
697  {
698  for (size_t n = FIRST_IMAGE; n <= Ndim; n++)
699  {
700  Iaux.read(fnIn,DATA,n);
701  if (n == FIRST_IMAGE)
702  I=Iaux;
703  else
704  I()+=Iaux();
705  }
706  I()/=Ndim;
707  }
708  else
709  I.read(fnIn);
710  }
711  I().rangeAdjust(0,255);
712  if (verbose==2)
713  I.write(fnOroot+"_analyzed_image.xmp");
714 
715  // Adapt to old code
716  if ((imagen = (unsigned char **)imalloc(YSIZE(I()) + 1, XSIZE(I()) + 1, NATURAL)) == NULL)
719  imagen[i+1][j+1]=(unsigned char)IMGPIXEL(I,i,j);
720  if (x0>=0)
721  xc0=(float)x0+1; //+1 because of Fortran indexing
722  else
723  xc0=XSIZE(I())/2+1;
724  if (y0>=0)
725  yc0=(float)y0+1;
726  else
727  yc0=YSIZE(I())/2+1;
728  r1=(float)(_r1/100.0*XSIZE(I())/2.0);
729  r2=(float)(_r2/100.0*XSIZE(I())/2.0);
730  r3=1;
731  rbajo=(float)(_r3/100.0*XSIZE(I())/2.0);
732  ralto=(float)(_r4/100.0*XSIZE(I())/2.0);
733  ncic=_ncic;
734  indmul=_indmul;
735  del = DEF_DEL;
736  in = DEF_IN;
737  ni = DEF_IT;
738  mu = (int)(PI / 2 * r2 / ncic);
739  if (mu < 3)
740  REPORT_ERROR(ERR_ARG_INCORRECT,"A higher integration radius is needed (r2>6*harm/pi)");
741  largo=YSIZE(I());
742  lancho=XSIZE(I());
743  busca();
744  MetaDataVec MD;
745  id=MD.addObject();
746  if (yc0>0)
747  {
748  MD.setValue(MDL_X,(double)(yc0-1),id);
749  MD.setValue(MDL_Y,(double)(xc0-1),id);
750  }
751  else
752  {
753  MD.setValue(MDL_X,(double)(XSIZE(I())/2),id);
754  MD.setValue(MDL_Y,(double)(YSIZE(I())/2),id);
755  }
756  MD.write(fnOroot+"_center.xmd");
757  imfree((char**)imagen, YSIZE(I()) + 1, XSIZE(I()) + 1, NATURAL);
758  }
759 };
constexpr int FOURIER
#define YSIZE(v)
float conv1x(double y, double x)
double * bj
BYTE ** imagen
constexpr int LONGFMT
double getDoubleParam(const char *param, int arg=0)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
float cxm2
int mu4
unsigned char BYTE
doublereal * g
void sqrt(Image< double > &op)
static double * y
float del
There is not enough memory for allocation.
Definition: xmipp_error.h:166
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void ergrot(double xc0, double yc0, float *zzz)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
void imfree(char **image, int row, int col, int format)
float cxm3
virtual IdIteratorProxy< false > ids()
float xc0
int largo
doublereal * x
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams &params=DefaultApplyGeoParams)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
void addSeeAlsoLine(const char *seeAlso)
constexpr int SPIDER
doublereal * b
int ntot
float cxm1
const char * getParam(const char *param, int arg=0)
float cxp2
float coseno[1281]
bool setValue(const MDObject &mdValueIn, size_t id)
int indmul
size_t addObject() override
int in
double * f
int mu1
Incorrect argument received.
Definition: xmipp_error.h:113
constexpr int ALLOC_SIZE
#define XSIZE(v)
int _indmul
Optimization type.
free((char *) ob)
float rh
double z
void addExampleLine(const char *example, bool verbatim=true)
int verbose
Verbosity level.
int mt
constexpr int INTFMT
float cxp3
int ni
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
int m
FileName fnIn
Filenames.
constexpr int FLOATFMT
void suaviza()
double x0
Starting center.
bool isMetaData(bool failIfNotExists=true) const
float ralto
int ncic
void ** imalloc(int row, int col, int format)
Y component (double)
void defineParams()
Define parameters.
float r3
float yc0
unsigned short UWORD
X component (double)
int mu
int idz
int ntot4
int lancho
bool checkParam(const char *param)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
#define FIRST_IMAGE
int ncic2
float r2
constexpr int DEF_IN
#define pi
float rbajo
float zzz0
float cxp1
void addUsageLine(const char *line, bool verbatim=false)
void busca()
#define PI
Definition: tools.h:43
int getIntParam(const char *param, int arg=0)
int * n
doublereal * a
void clear()
Definition: xmipp_image.h:144
void addParamsLine(const String &line)
int ir
constexpr int DEF_DEL
#define IMGPIXEL(I, i, j)
float r1
constexpr int DEF_IT
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
unsigned long ULONG