Xmipp  v3.23.11-Nereus
tomo_align_tilt_series.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es (2008)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 #ifndef _PROG_ANGULAR_ASSIGN_FOR_TILTSERIES
26 #define _PROG_ANGULAR_ASSIGN_FOR_TILTSERIES
27 
28 #include "core/matrix1d.h"
29 #include "core/matrix2d.h"
30 #include "core/metadata_vec.h"
31 #include "core/multidim_array.h"
32 #include "core/xmipp_program.h"
33 
38 
41  const MultidimArray<unsigned char> &I2, int maxShift, int maxIterDE,
42  const FileName &fn_affine,
43  Matrix2D<double> &A12, Matrix2D<double> &A21, bool show,
44  double thresholdAffine, bool globalAffine, bool isMirror,
45  bool checkRotation);
46 
50 class Landmark
51 {
52 public:
53  double x;
54  double y;
55  int imgIdx;
57  {
58  x=rhs.x;
59  y=rhs.y;
60  imgIdx=rhs.imgIdx;
61  return *this;
62  }
63  int operator<(const Landmark &rhs) const
64  {
65  return imgIdx<rhs.imgIdx;
66  }
67 };
68 
70 typedef std::vector<Landmark> LandmarkChain;
71 
72 /* Forward prototype */
73 class Alignment;
74 
77 {
78 public:
81 
84 
87 
90 
93 
96 
98  size_t Ncritical;
99 
101  size_t seqLength;
102 
106 
109 
112 
115 
117  double psiMax;
118 
120  int maxStep;
121 
123  double deltaRot;
124 
126  double localSize;
127 
130 
133 
136 
138  bool difficult;
139 
142 
145 
148 
151 
154 
157 
159  int lastStep;
160 
161  // iteration counter as a progress measure
163 
166 
168  void defineParams();
169 
171  void readParams();
172 
174  void show();
175 
177  void produceSideInfo();
178 
180  void computeAffineTransformations(bool globalAffineToUse);
181 
183  void identifyOutliers(bool mark);
184 
186  void produceInformationFromLandmarks();
187 
194  bool refineLandmark(int ii, int jj, const Matrix1D<double> &rii,
195  Matrix1D<double> &rjj, double &maxCorr, bool tryFourier) const;
196 
197 
201  bool refineLandmark(const MultidimArray<double> &pieceii, int jj,
202  Matrix1D<double> &rjj, double actualCorrThreshold,
203  bool reversed, double &maxCorr) const;
204 
206  bool refineChain(LandmarkChain &chain, double &corrChain);
207 
209  void generateLandmarkSet();
210 
212  void writeLandmarkSet(const FileName &fnLandmark) const;
213 
215  void writeTransformations(const FileName &fnTransformations) const;
216 
218  void readLandmarkSet(const FileName &fnLandmark);
219 
221  void removeOutlierLandmarks(const Alignment &alignment);
222 
224  void alignImages(const Alignment &alignment);
225 
227  void run();
228 
229 public:
230  // MetaData with the input images
232 
233  // MetaData with the input images
235 
236  // List of image pointers
237  std::vector < MultidimArray<unsigned char> *> img;
238 
239  // List of mask pointers
240  std::vector < MultidimArray<unsigned char> *> maskImg;
241 
242  // Index of the image closest to 0 degrees in tilt
243  int iMinTilt;
244 
245  // List of tilt angles
246  std::vector < double > tiltList;
247 
248  // List of names
249  std::vector < std::string > name_list;
250 
251  // Average forward patch correlation
253 
254  // Average backward patch correlation
256 
257  // Outlier
259 
260  // Total number of images
261  int Nimg;
262 
263  // Element [i][j] is the transformation of coordinates of i into
264  // coordinates of j
265  std::vector< std::vector< Matrix2D<double> > > affineTransformations;
266 
267  // List of affine costs
268  std::vector < double > correlationList;
269 
270  // Landmark matrix (X component)
272 
273  // Landmark matrix (Y component)
275 
276  // Set of all landmarks seen in image i
277  std::vector< std::vector<int> > Vseti;
278 
279  // Set of all images in which the landmark j is seen
280  std::vector< std::vector<int> > Vsetj;
281 
282  // Average of the landmarks in a given image
283  std::vector< Matrix1D<double> > barpi;
284 
285  // Number of landmarks in image i
287 
288  // Best exhaustive alignmnet
290 
291  // Show refinement
293 };
294 
296 {
297 public:
299  std::vector< Matrix1D<double> > di;
300  std::vector< Matrix1D<double> > rj;
302  double rot;
303  double tilt;
305 
306 public:
308  {
309  prm=_prm;
310  Nimg=MAT_XSIZE(_prm->allLandmarksX);
311  Nlandmark=MAT_YSIZE(_prm->allLandmarksX);
312  clear();
313  }
314 
316  void clear()
317  {
318  psi.initZeros(Nimg);
319  rot=90;
320  tilt=90;
321  raxis.initZeros(3);
322 
323  Ai.clear();
324  Ait.clear();
325  Aip.clear();
326  Aipt.clear();
327  di.clear();
328  diaxis.clear();
329  B1i.clear();
330  B2i.clear();
331  barri.clear();
332  Matrix2D<double> dummy23(2,3);
333  Matrix2D<double> dummy32(3,2);
334  Matrix2D<double> dummy33(3,3);
335  Matrix2D<double> dummy22(2,2);
336  Matrix1D<double> dummy2(2);
337  Matrix1D<double> dummy3(3);
338  for (int i=0; i<Nimg; i++)
339  {
340  Ai.push_back(dummy23);
341  Ait.push_back(dummy32);
342  Aip.push_back(dummy23);
343  Aipt.push_back(dummy32);
344  di.push_back(dummy2);
345  diaxis.push_back(dummy2);
346  B1i.push_back(dummy33);
347  B2i.push_back(dummy33);
348  barri.push_back(dummy3);
349  }
350  allLandmarksPredictedX.initZeros(Nlandmark,Nimg);
351  allLandmarksPredictedY.initZeros(Nlandmark,Nimg);
352  errorLandmark.initZeros(Nlandmark);
353 
354  rj.clear();
355  for (int j=0; j<Nlandmark; j++)
356  rj.push_back(dummy3);
357  }
358 
361  {
362  if (this!=&op)
363  {
364  prm=op.prm;
365  Nimg=op.Nimg;
366  Nlandmark=op.Nlandmark;
367  psi=op.psi;
368  rot=op.rot;
369  tilt=op.tilt;
370  Ai=op.Ai;
371  Ait=op.Ait;
372  Aip=op.Aip;
373  Aipt=op.Aipt;
374  diaxis=op.diaxis;
375  B1i=op.B1i;
376  B2i=op.B2i;
377  raxis=op.raxis;
378  di=op.di;
379  rj=op.rj;
380  barri=op.barri;
381  allLandmarksPredictedX=op.allLandmarksPredictedX;
382  allLandmarksPredictedY=op.allLandmarksPredictedY;
383  errorLandmark=op.errorLandmark;
384  }
385  return *this;
386  }
387 
389  double optimizeGivenAxisDirection();
390 
392  void computeGeometryDependentOfAxis();
393 
395  void computeGeometryDependentOfRotation();
396 
398  double computeError() const;
399 
401  void computeErrorForLandmarks();
402 
404  void updateModel();
405 
407  friend std::ostream& operator<<(std::ostream &out,
408  Alignment &alignment);
409 
410 public:
411  // Number of images
412  int Nimg;
413 
414  // Number of landmarks
416 
417  // Set of Ai matrices
418  std::vector< Matrix2D<double> > Ai;
419 
420  // Set of Ait matrices
421  std::vector< Matrix2D<double> > Ait;
422 
423  // Set of Ai prime matrices
424  std::vector< Matrix2D<double> > Aip;
425 
426  // Set of Ai prime transpose matrices
427  std::vector< Matrix2D<double> > Aipt;
428 
429  // Set of bar ri
430  std::vector< Matrix1D<double> > barri;
431 
432  // Set of shifts due to raxis
433  std::vector< Matrix1D<double> > diaxis;
434 
435  // Set of B1i matrices
436  std::vector< Matrix2D<double> > B1i;
437 
438  // Set of B2i matrices
439  std::vector< Matrix2D<double> > B2i;
440 
441  // Matrix used for updating raxis
443 
444  // Set of predicted landmarks (component X)
446 
447  // Set of predicted landmarks (component Y)
449 
450  // Set of errors associated to each landmark
452 };
453 
459  const MultidimArray<double> &I2, int maxShift, int maxIterDE,
460  Matrix2D<double> &A12, Matrix2D<double> &A21, bool show,
461  double thresholdAffine, bool globalAffine, bool isMirror,
462  int pyramidLevel);
464 #endif
bool globalAffine
Look for local affine transformation.
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
std::vector< Matrix1D< double > > di
double computeAffineTransformation(const MultidimArray< unsigned char > &I1, const MultidimArray< unsigned char > &I2, int maxShift, int maxIterDE, const FileName &fn_affine, Matrix2D< double > &A12, Matrix2D< double > &A21, bool show, double thresholdAffine, bool globalAffine, bool isMirror, bool checkRotation)
double maxShiftPercentage
Maxshift percentage.
const ProgTomographAlignment * prm
std::vector< Matrix1D< double > > rj
Matrix2D< double > allLandmarksY
std::vector< double > tiltList
double alignImages(const MultidimArray< double > &Iref, const AlignmentTransforms &IrefTransforms, MultidimArray< double > &I, Matrix2D< double > &M, bool wrap, AlignmentAux &aux, CorrelationAux &aux2, RotationalCorrelationAux &aux3)
Definition: filters.cpp:2047
size_t seqLength
Sequence Length.
Matrix2D< double > Binvraxis
MultidimArray< double > errorLandmark
std::vector< MultidimArray< unsigned char > * > maskImg
std::vector< Matrix1D< double > > barpi
double corrThreshold
Correlation threshold for a good landmark.
FileName fnSel
MetaData File with all images.
int maxIterDE
Maximum number of iterations in Differential Evolution.
std::vector< Matrix2D< double > > Ait
Matrix2D< double > allLandmarksX
int operator<(const Landmark &rhs) const
int maxStep
Maximum Step for chain refinement.
FileName fnRoot
Output root.
std::ostream & operator<<(std::ostream &os, const Message &sb)
std::vector< Matrix2D< double > > Ai
#define i
size_t Ncritical
Number of critical points.
std::vector< std::string > name_list
Matrix1D< double > avgForwardPatchCorr
std::vector< Matrix1D< double > > diaxis
int lastStep
Last step to run (-1=run them all)
int numThreads
Number of threads to use for parallel computing.
Matrix1D< double > avgBackwardPatchCorr
Matrix1D< double > psi
std::vector< std::vector< int > > Vseti
std::vector< Landmark > LandmarkChain
double thresholdAffine
Threshold affine.
std::vector< Matrix2D< double > > B2i
std::vector< Matrix2D< double > > Aip
FileName fnSelOrig
MetaData File with all images at the original scale.
bool doNotIdentifyOutliers
Do not identify outlier micrographs.
double identifyOutliersZ
Identify outlier micrographs.
void initZeros()
Definition: matrix1d.h:592
std::vector< MultidimArray< unsigned char > * > img
bool useCriticalPoints
Use critical points.
Landmark & operator=(const Landmark &rhs)
std::vector< Matrix2D< double > > B1i
#define j
Matrix1D< double > raxis
std::vector< std::vector< Matrix2D< double > > > affineTransformations
double localSize
Local refinement size.
std::vector< std::vector< int > > Vsetj
std::vector< Matrix2D< double > > Aipt
Alignment(const ProgTomographAlignment *_prm)
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
Matrix2D< double > allLandmarksPredictedX
bool isCapillar
This tilt series comes from a capillar.
Matrix2D< double > allLandmarksPredictedY
double psiMax
Maximum rotation.
std::vector< Matrix1D< double > > barri
bool optimizeTiltAngle
Optimize tilt angle of tilt axis.
bool showAffine
Show affine transformations.
bool dontNormalize
Don&#39;t normalize the corrected images.
std::vector< double > correlationList