Xmipp  v3.23.11-Nereus
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
AProgMovieAlignmentCorrelation< T > Class Template Referenceabstract

#include <movie_alignment_correlation_base.h>

Inheritance diagram for AProgMovieAlignmentCorrelation< T >:
Inheritance graph
[legend]
Collaboration diagram for AProgMovieAlignmentCorrelation< T >:
Collaboration graph
[legend]

Public Member Functions

virtual void readParams ()
 Read argument from command line. More...
 
virtual void show ()
 Show. More...
 
virtual void defineParams ()
 Define parameters. More...
 
void run ()
 Run. More...
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Protected Member Functions

Dimensions getMovieSizeRaw ()
 
Dimensions getMovieSize ()
 
AlignmentResult< T > computeAlignment (Matrix1D< T > &bX, Matrix1D< T > &bY, Matrix2D< T > &A, const core::optional< size_t > &refFrame, size_t N, int verbose)
 
int findReferenceImage (size_t N, const Matrix1D< T > &shiftX, const Matrix1D< T > &shiftY)
 
void computeTotalShift (int iref, int j, const Matrix1D< T > &shiftX, const Matrix1D< T > &shiftY, T &totalShiftX, T &totalShiftY)
 
MultidimArray< T > createLPF (T Ts, const Dimensions &dims)
 
void loadFrame (const MetaData &movie, const Image< T > &dark, const Image< T > &igain, size_t objId, Image< T > &out) const
 
virtual void applyShiftsComputeAverage (const MetaData &movie, const Image< T > &dark, const Image< T > &igain, Image< T > &initialMic, size_t &Ninitial, Image< T > &averageMicrograph, size_t &N, const AlignmentResult< T > &globAlignment)=0
 
virtual void applyShiftsComputeAverage (const MetaData &movie, const Image< T > &dark, const Image< T > &igain, Image< T > &initialMic, size_t &Ninitial, Image< T > &averageMicrograph, size_t &N, const LocalAlignmentResult< T > &alignment)=0
 
virtual AlignmentResult< T > computeGlobalAlignment (const MetaData &movie, const Image< T > &dark, const Image< T > &igain)=0
 
virtual LocalAlignmentResult< T > computeLocalAlignment (const MetaData &movie, const Image< T > &dark, const Image< T > &igain, const AlignmentResult< T > &globAlignment)=0
 
virtual void releaseAll ()=0
 
void storeResults (const LocalAlignmentResult< T > &alignment)
 
float getPixelResolution (float scaleFactor) const
 
float getScaleFactor () const
 
std::pair< size_t, size_t > getRequestedPatchSize () const
 
auto getBinning () const
 
bool applyBinning () const
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 

Protected Attributes

int nfirst
 
int nlast
 
float maxShift
 
FileName fnAligned
 
FileName fnAvg
 
int nfirstSum
 
int nlastSum
 
FileName fnInitialAvg
 
std::pair< size_t, size_t > localAlignPatches
 
Dimensions localAlignmentControlPoints = Dimensions(0)
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Static Protected Attributes

static constexpr int solverIterations = 2
 

Additional Inherited Members

- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Detailed Description

template<typename T>
class AProgMovieAlignmentCorrelation< T >

Definition at line 56 of file movie_alignment_correlation_base.h.

Member Function Documentation

◆ applyBinning()

template<typename T >
bool AProgMovieAlignmentCorrelation< T >::applyBinning ( ) const
inlineprotected

Definition at line 227 of file movie_alignment_correlation_base.h.

227  {
228  return binning != 1.0;
229  }

◆ applyShiftsComputeAverage() [1/2]

template<typename T >
virtual void AProgMovieAlignmentCorrelation< T >::applyShiftsComputeAverage ( const MetaData movie,
const Image< T > &  dark,
const Image< T > &  igain,
Image< T > &  initialMic,
size_t &  Ninitial,
Image< T > &  averageMicrograph,
size_t &  N,
const AlignmentResult< T > &  globAlignment 
)
protectedpure virtual

This method applies global shifts and can also produce 'average' image (micrograph)

Parameters
movieinput
darkcorrection to be used
igaincorrection to be used
initialMicsum of the unaligned micrographs
Ninitialwill store number of micrographs used for unaligned sum
averageMicrographsum of the aligned micrographs
Nwill store number of micrographs used for aligned sum
globAlignmentto apply

◆ applyShiftsComputeAverage() [2/2]

template<typename T >
virtual void AProgMovieAlignmentCorrelation< T >::applyShiftsComputeAverage ( const MetaData movie,
const Image< T > &  dark,
const Image< T > &  igain,
Image< T > &  initialMic,
size_t &  Ninitial,
Image< T > &  averageMicrograph,
size_t &  N,
const LocalAlignmentResult< T > &  alignment 
)
protectedpure virtual

This method applies local shifts and can also produce 'average' image (micrograph)

Parameters
movieinput
darkcorrection to be used
igaincorrection to be used
initialMicsum of the unaligned micrographs
Ninitialwill store number of micrographs used for unaligned sum
averageMicrographsum of the aligned micrographs
Nwill store number of micrographs used for aligned sum
alignmentto apply

◆ computeAlignment()

template<typename T >
AlignmentResult< T > AProgMovieAlignmentCorrelation< T >::computeAlignment ( Matrix1D< T > &  bX,
Matrix1D< T > &  bY,
Matrix2D< T > &  A,
const core::optional< size_t > &  refFrame,
size_t  N,
int  verbose 
)
protected

Compute alignment of the each frame from frame-to-frame shifts

Parameters
bXframe-to-frame shift in X dim
bYframe-to-frame shift in Y dim
Asystem matrix to be used
refFramereference frame
Nno of frames
verboselevel
Returns
respective global alignment

Definition at line 400 of file movie_alignment_correlation_base.cpp.

402  {
403  // now get the estimated shift (from the equation system)
404  // from each frame to successive frame
405  Matrix1D<T> shiftX, shiftY;
406  EquationSystemSolver::solve(bX, bY, A, shiftX, shiftY, verbose, solverIterations);
407  // prepare result
408  AlignmentResult<T> result {.refFrame = refFrame ?
409  refFrame.value() :
410  this->findReferenceImage(N, shiftX, shiftY)};
411  result.shifts.reserve(N);
412  // compute total shift in respect to reference frame
413  for (size_t i = 0; i < N; ++i) {
414  T x, y;
415  this->computeTotalShift(result.refFrame, i, shiftX, shiftY, x, y);
416  result.shifts.emplace_back(x, y);
417  }
418  return result;
419 }
int findReferenceImage(size_t N, const Matrix1D< T > &shiftX, const Matrix1D< T > &shiftY)
static double * y
void computeTotalShift(int iref, int j, const Matrix1D< T > &shiftX, const Matrix1D< T > &shiftY, T &totalShiftX, T &totalShiftY)
T & value() const
Definition: optional.h:67
doublereal * x
#define i
static void solve(Matrix1D< T > &bXt, Matrix1D< T > &bYt, Matrix2D< T > &At, Matrix1D< T > &shiftXt, Matrix1D< T > &shiftYt, int verbosity, int iterations)
int verbose
Verbosity level.
Definition: ctf.h:38

◆ computeGlobalAlignment()

template<typename T >
virtual AlignmentResult<T> AProgMovieAlignmentCorrelation< T >::computeGlobalAlignment ( const MetaData movie,
const Image< T > &  dark,
const Image< T > &  igain 
)
protectedpure virtual

This method computes global shift of the frames of the movie

Parameters
movieto process
darkpixel correction
igaincorrection
Returns
global alignment of the movie

◆ computeLocalAlignment()

template<typename T >
virtual LocalAlignmentResult<T> AProgMovieAlignmentCorrelation< T >::computeLocalAlignment ( const MetaData movie,
const Image< T > &  dark,
const Image< T > &  igain,
const AlignmentResult< T > &  globAlignment 
)
protectedpure virtual

This method computes local shift of the frames of the movie

Parameters
movieto process
darkpixel correction
igaincorrection
Returns
local alignment of the movie

◆ computeTotalShift()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::computeTotalShift ( int  iref,
int  j,
const Matrix1D< T > &  shiftX,
const Matrix1D< T > &  shiftY,
T &  totalShiftX,
T &  totalShiftY 
)
protected

Method to compute sum of shifts of some image in respect to a reference image

Parameters
irefindex of the reference image
jindex of the queried image
shiftXrelative shifts in X dim
shiftYrelative shifts in Y dim
totalShiftXresulting shift in X dim
totalShiftYresulting shift in Y dim

Definition at line 229 of file movie_alignment_correlation_base.cpp.

231  {
232  totalShiftX = totalShiftY = 0;
233  if (iref < j) {
234  for (int jj = j - 1; jj >= iref; --jj) {
235  totalShiftX -= shiftX(jj);
236  totalShiftY -= shiftY(jj);
237  }
238  } else if (iref > j) {
239  for (int jj = j; jj <= iref - 1; ++jj) {
240  totalShiftX += shiftX(jj);
241  totalShiftY += shiftY(jj);
242  }
243  }
244 }
#define j

◆ createLPF()

template<typename T >
MultidimArray< T > AProgMovieAlignmentCorrelation< T >::createLPF ( Ts,
const Dimensions dims 
)
protected

Method will create a 2D Low Pass Filter of given size

Parameters
Tspixel resolution of the resulting filter
dimsdimension of the filter (in spatial domain)
Returns
requested LPF

Definition at line 185 of file movie_alignment_correlation_base.cpp.

185  {
186  assert(Ts >= this->Ts);
187 
188  // Construct 1D profile of the lowpass filter
189  MultidimArray<T> lpf(dims.x());
190  createLPF(Ts, lpf);
191 
192  // scale 1D filter to 2D
193  MultidimArray<T> result;
194  result.initZeros(dims.y(), (dims.x() / 2) + 1);
195  scaleLPF(lpf, dims, result);
196  return result;
197 }
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
MultidimArray< T > createLPF(T Ts, const Dimensions &dims)
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
void initZeros(const MultidimArray< T1 > &op)

◆ defineParams()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::defineParams ( )
virtual

Define parameters.

Reimplemented from XmippProgram.

Reimplemented in ProgMovieAlignmentCorrelation< T >, and ProgMovieAlignmentCorrelationGPU< T >.

Definition at line 112 of file movie_alignment_correlation_base.cpp.

112  {
113  addUsageLine("Align a set of frames by cross-correlation of the frames");
115  " -i <metadata> : Metadata with the list of frames to align");
117  " [-o <fn=\"out.xmd\">] : Metadata with the shifts of each frame.");
119  " : If no filename is given, the input is rewritten");
121  " [--bin <s=1>] : Binning factor, it may be any floating number > 1.");
123  " : Binning is applied during the data loading, i.e. the program will processed and store binned data.");
125  " [--maxShift <s=50>] : Maximum shift allowed in A");
127  " [--maxResForCorrelation <R=30>]: Maximum resolution to align (in Angstroms)");
129  " [--sampling <Ts=1>] : Sampling rate (A/pixel)");
131  " [--oaligned <fn=\"\">] : Aligned movie consists of aligned frames used for micrograph generation");
133  " [--oavgInitial <fn=\"\">] : Give the name of a micrograph to generate an unaligned (initial) micrograph");
135  " [--oavg <fn=\"\">] : Give the name of a micrograph to generate an aligned micrograph");
137  " [--frameRange <n0=-1> <nF=-1>] : First and last frame to align, frame numbers start at 0");
139  " [--frameRangeSum <n0=-1> <nF=-1>] : First and last frame to sum, frame numbers start at 0");
140  addParamsLine(" [--dark <fn=\"\">] : Dark correction image");
141  addParamsLine(" [--gain <fn=\"\">] : Gain correction image (we will multiply by it)");
143  " [--skipLocalAlignment] : If used, only global alignment will be performed. It's faster, but gives worse results.");
145  " [--controlPoints <x=6> <y=6> <t=5>]: Number of control points (including end points) used for defining the BSpline");
147  " [--patches <x=7> <y=7>]: Number of patches used for local alignment");
149  " [--minLocalRes <R=500>] : Minimal resolution (in A) of patches during local alignment");
150  addExampleLine("A typical example", false);
151  addSeeAlsoLine("xmipp_movie_optical_alignment_cpu");
152 }
void addSeeAlsoLine(const char *seeAlso)
void addExampleLine(const char *example, bool verbatim=true)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ findReferenceImage()

template<typename T >
int AProgMovieAlignmentCorrelation< T >::findReferenceImage ( size_t  N,
const Matrix1D< T > &  shiftX,
const Matrix1D< T > &  shiftY 
)
protected

Method finds a reference image, i.e. an image which has smallest relative shift to all other images.

Parameters
Nno of images
shiftXrelative X shift of each image
shiftYrelative Y shift of each image
Returns
position of the reference frame

Definition at line 247 of file movie_alignment_correlation_base.cpp.

248  {
249  int bestIref = -1;
250  // Choose reference image as the minimax of shifts
251  T worstShiftEver = std::numeric_limits<T>::max();
252  for (int iref = 0; iref < N; ++iref) {
253  T worstShift = -1;
254  for (int j = 0; j < N; ++j) {
255  T totalShiftX, totalShiftY;
256  computeTotalShift(iref, j, shiftX, shiftY, totalShiftX,
257  totalShiftY);
258  if (fabs(totalShiftX) > worstShift)
259  worstShift = fabs(totalShiftX);
260  }
261  if (worstShift < worstShiftEver) {
262  worstShiftEver = worstShift;
263  bestIref = iref;
264  }
265  }
266  return bestIref;
267 }
void computeTotalShift(int iref, int j, const Matrix1D< T > &shiftX, const Matrix1D< T > &shiftY, T &totalShiftX, T &totalShiftY)
void max(Image< double > &op1, const Image< double > &op2)
#define j

◆ getBinning()

template<typename T >
auto AProgMovieAlignmentCorrelation< T >::getBinning ( ) const
inlineprotected

Get binning factor for resulting micrograph / alignend movie

Definition at line 223 of file movie_alignment_correlation_base.h.

223  {
224  return binning;
225  }

◆ getMovieSize()

template<typename T >
Dimensions AProgMovieAlignmentCorrelation< T >::getMovieSize ( )
protected
Returns
dimension of the movie frames and their count after binning, if applied

Definition at line 357 of file movie_alignment_correlation_base.cpp.

357  {
358  if (movieSize) return movieSize.value();
359  auto full = getMovieSizeRaw();
360  if (applyBinning()) {
361  // to make FFT fast, we want the size to be a multiple of 2
362  auto x = ((static_cast<float>(full.x()) / binning) / 2.f) * 2.f;
363  auto y = ((static_cast<float>(full.y()) / binning) / 2.f) * 2.f;
364  movieSize = Dimensions(static_cast<size_t>(x), static_cast<size_t>(y), 1L, full.n());
365  } else {
366  movieSize = full;
367  }
368  return movieSize.value();
369 }
static double * y
doublereal * x
double * f

◆ getMovieSizeRaw()

template<typename T >
Dimensions AProgMovieAlignmentCorrelation< T >::getMovieSizeRaw ( )
protected
Returns
dimension of the raw movie frames and their count (taking into account slicing indicated by the user)

Definition at line 339 of file movie_alignment_correlation_base.cpp.

339  {
340  if (this->movieSizeRaw) return movieSizeRaw.value();
341  int noOfImgs = this->nlast - this->nfirst + 1;
342  auto fn = fnMovie;
343  if (fnMovie.isMetaData()) {
344  MetaDataVec md;
345  md.read(fnMovie);
346  md.getValue(MDL_IMAGE, fn, md.firstRowId()); // assuming all frames have the same resolution
347  }
348  ImageGeneric movieStack;
349  movieStack.read(fn, HEADER);
350  size_t xdim, ydim, zdim, ndim;
351  movieStack.getDimensions(xdim, ydim, zdim, ndim);
352  this->movieSizeRaw = Dimensions(xdim, ydim, 1, noOfImgs);
353  return movieSizeRaw.value();
354 }
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
size_t firstRowId() const override
bool getValue(MDObject &mdValueOut, size_t id) const override
bool isMetaData(bool failIfNotExists=true) const
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
Name of an image (std::string)

◆ getPixelResolution()

template<typename T >
float AProgMovieAlignmentCorrelation< T >::getPixelResolution ( float  scaleFactor) const
protected

Returns pixel resolution of the scaled movie

Parameters
scaleFactor(<= 1) used to change size of the movie

Definition at line 180 of file movie_alignment_correlation_base.cpp.

180  {
181  return this->Ts / scaleFactor;
182 }

◆ getRequestedPatchSize()

template<typename T >
std::pair<size_t, size_t> AProgMovieAlignmentCorrelation< T >::getRequestedPatchSize ( ) const
inlineprotected

Returns size of the patch as requested by user

Definition at line 217 of file movie_alignment_correlation_base.h.

217  {
218  return {minLocalRes / Ts, minLocalRes / Ts};
219  }

◆ getScaleFactor()

template<typename T >
float AProgMovieAlignmentCorrelation< T >::getScaleFactor ( ) const
protected

Returns scale factor as requested by user

Definition at line 311 of file movie_alignment_correlation_base.cpp.

311  {
312  // scale is ration between original pixel size and new pixel size
313  return Ts / getTsPrime();
314 }

◆ loadFrame()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::loadFrame ( const MetaData movie,
const Image< T > &  dark,
const Image< T > &  igain,
size_t  objId,
Image< T > &  out 
) const
protected

Method loads a single frame from the movie and apply gain and dark pixel correction

Parameters
movieto load from
darkpixel correction
igaininverse gain correction
objIdid of the image to load
outloaded frame

Definition at line 155 of file movie_alignment_correlation_base.cpp.

157  {
158  FileName fnFrame;
159  movie.getValue(MDL_IMAGE, fnFrame, objId);
160  out.read(fnFrame);
161  if (XSIZE(dark()) > 0) {
162  if ((XSIZE(dark()) != XSIZE(out()))
163  || (YSIZE(dark()) != YSIZE(out()))) {
165  "The dark image size does not match the movie frame size.");
166  }
167  out() -= dark();
168  }
169  if (XSIZE(igain()) > 0) {
170  if ((XSIZE(igain()) != XSIZE(out()))
171  || (YSIZE(igain()) != YSIZE(out()))) {
173  "The gain image size does not match the movie frame size.");
174  }
175  out() *= igain();
176  }
177 }
#define YSIZE(v)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
Incorrect argument received.
Definition: xmipp_error.h:113
#define XSIZE(v)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Name of an image (std::string)

◆ readParams()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::readParams ( )
virtual

Read argument from command line.

Reimplemented from XmippProgram.

Reimplemented in ProgMovieAlignmentCorrelation< T >, and ProgMovieAlignmentCorrelationGPU< T >.

Definition at line 33 of file movie_alignment_correlation_base.cpp.

33  {
34  fnMovie = getParam("-i");
35  fnOut = getParam("-o");
36  fnInitialAvg = getParam("--oavgInitial");
37  fnDark = getParam("--dark");
38  fnGain = getParam("--gain");
39  binning = getFloatParam("--bin");
40  if (binning < 1.0)
41  REPORT_ERROR(ERR_ARG_INCORRECT, "Binning must be >= 1");
42  Ts = getFloatParam("--sampling") * binning;
43  maxShift = getFloatParam("--maxShift") / Ts;
44  maxResForCorrelation = getFloatParam("--maxResForCorrelation");
45  fnAligned = getParam("--oaligned");
46  fnAvg = getParam("--oavg");
47  nfirst = getIntParam("--frameRange", 0);
48  nlast = getIntParam("--frameRange", 1);
49  nfirstSum = getIntParam("--frameRangeSum", 0);
50  nlastSum = getIntParam("--frameRangeSum", 1);
51  skipLocalAlignment = checkParam("--skipLocalAlignment");
52  minLocalRes = getIntParam("--minLocalRes");
53 
54  // read control points
55  Dimensions cPoints(
56  this->getIntParam("--controlPoints", 0),
57  this->getIntParam("--controlPoints", 1),
58  1,
59  this->getIntParam("--controlPoints", 2));
60  if ((cPoints.x() < 3) || (cPoints.y() < 3) || (cPoints.n() < 3))
62  "All control points has to be bigger than 2");
64 
65 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
float getFloatParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)
Incorrect argument received.
Definition: xmipp_error.h:113
FileName fnOut
bool checkParam(const char *param)
int getIntParam(const char *param, int arg=0)

◆ releaseAll()

template<typename T >
virtual void AProgMovieAlignmentCorrelation< T >::releaseAll ( )
protectedpure virtual

This method releases all resources allocated so far

◆ run()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::run ( )
virtual

Run.

Reimplemented from XmippProgram.

Definition at line 530 of file movie_alignment_correlation_base.cpp.

530  {
531  // preprocess input data
532  MetaDataVec movie;
533  readMovie(movie);
534  correctLoopIndices(movie);
535 
536  setNoOfPatches();
537  show();
538  checkSettings();
539 
540  Image<T> dark, igain;
541  loadDarkCorrection(dark);
542  loadGainCorrection(igain);
543 
544  auto globalAlignment = AlignmentResult<T>();
545  std::cout << "Computing global alignment ...\n";
546  globalAlignment = computeGlobalAlignment(movie, dark, igain);
547 
548  if ( ! fnOut.isEmpty()) {
549  storeGlobalShifts(globalAlignment, movie);
550  }
551 
552  if (verbose) printGlobalShift(globalAlignment);
553 
554  size_t N, Ninitial;
555  Image<T> initialMic, averageMicrograph;
556  // Apply shifts and compute average
557  if (skipLocalAlignment) {
558  applyShiftsComputeAverage(movie, dark, igain, initialMic, Ninitial,
559  averageMicrograph, N, globalAlignment);
560  } else {
561  std::cout << "Computing local alignment ...\n";
562  auto localAlignment = computeLocalAlignment(movie, dark, igain, globalAlignment);
563  applyShiftsComputeAverage(movie, dark, igain, initialMic, Ninitial,
564  averageMicrograph, N, localAlignment);
565  storeResults(localAlignment);
566  }
567 
568  storeResults(initialMic, Ninitial, averageMicrograph, N, movie, globalAlignment.refFrame);
569 
570  releaseAll();
571 }
virtual LocalAlignmentResult< T > computeLocalAlignment(const MetaData &movie, const Image< T > &dark, const Image< T > &igain, const AlignmentResult< T > &globAlignment)=0
void storeResults(const LocalAlignmentResult< T > &alignment)
FileName fnOut
virtual AlignmentResult< T > computeGlobalAlignment(const MetaData &movie, const Image< T > &dark, const Image< T > &igain)=0
int verbose
Verbosity level.
virtual void applyShiftsComputeAverage(const MetaData &movie, const Image< T > &dark, const Image< T > &igain, Image< T > &initialMic, size_t &Ninitial, Image< T > &averageMicrograph, size_t &N, const AlignmentResult< T > &globAlignment)=0
bool isEmpty() const

◆ show()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::show ( )
virtual

Show.

Reimplemented in ProgMovieAlignmentCorrelationGPU< T >.

Definition at line 89 of file movie_alignment_correlation_base.cpp.

89  {
90  if (!verbose)
91  return;
92  std::cout
93  << "Input movie: " << fnMovie << std::endl
94  << "Output metadata: " << fnOut << std::endl
95  << "Dark image: " << fnDark << std::endl
96  << "Gain image: " << fnGain << std::endl
97  << "Max. Shift (A / px): " << (maxShift * Ts) << " / " << maxShift << std::endl
98  << "Max resolution (A): " << maxResForCorrelation << std::endl
99  << "Sampling: " << Ts << std::endl
100  << "Aligned movie: " << fnAligned << std::endl
101  << "Aligned micrograph: " << fnAvg << std::endl
102  << "Unaligned micrograph: " << fnInitialAvg << std::endl
103  << "Frame range alignment: " << nfirst << " " << nlast << std::endl
104  << "Frame range sum: " << nfirstSum << " " << nlastSum << std::endl
105  << "Binning factor: " << binning << std::endl
106  << "Skip local alignment: " << (skipLocalAlignment ? "yes" : "no") << std::endl
107  << "Control points: " << this->localAlignmentControlPoints << std::endl
108  << "No of patches: " << this->localAlignPatches.first << " * " << localAlignPatches.second << std::endl;
109 }
FileName fnOut
int verbose
Verbosity level.

◆ storeResults()

template<typename T >
void AProgMovieAlignmentCorrelation< T >::storeResults ( const LocalAlignmentResult< T > &  alignment)
protected

Method to store all computed alignment to hard drive

Definition at line 462 of file movie_alignment_correlation_base.cpp.

463  {
464  if (fnOut.isEmpty()) {
465  return;
466  }
467  if ( ! alignment.bsplineRep) {
469  "Missing BSpline representation. This should not happen. Please contact developers.");
470  }
471  // Store average
472  std::vector<double> shifts;
473  for (auto &&p : alignment.shifts) {
474  int tileCenterX = p.first.rec.getCenter().x;
475  int tileCenterY = p.first.rec.getCenter().y;
476  int tileIdxT = p.first.id_t;
477  auto shift = BSplineHelper::getShift(
478  alignment.bsplineRep.value(), alignment.movieDim,
479  tileCenterX, tileCenterY, tileIdxT);
480  auto globalShift = alignment.globalHint.shifts.at(tileIdxT);
481  shifts.emplace_back(hypot(shift.first - globalShift.x, shift.second - globalShift.y));
482  }
483  MetaDataVec mdIref;
484  size_t id = mdIref.addObject();
485  // Store confidence interval
486  std::sort(shifts.begin(), shifts.end(), std::less<double>());
487  size_t indexL = shifts.size() * 0.025;
488  size_t indexU = shifts.size() * 0.975;
489  mdIref.setValue(MDL_LOCAL_ALIGNMENT_CONF_2_5_PERC, shifts.at(indexL), id);
490  mdIref.setValue(MDL_LOCAL_ALIGNMENT_CONF_97_5_PERC, shifts.at(indexU), id);
491  // Store patches
493  std::vector<size_t>{localAlignPatches.first, localAlignPatches.second}, id);
494  // Store coefficients
495  std::vector<double> tmpX;
496  std::vector<double> tmpY;
497  size_t size = alignment.bsplineRep.value().getCoeffsX().size();
498  for (int i = 0; i < size; ++i) {
499  tmpX.push_back(alignment.bsplineRep.value().getCoeffsX()(i));
500  tmpY.push_back(alignment.bsplineRep.value().getCoeffsY()(i));
501  }
502  mdIref.setValue(MDL_LOCAL_ALIGNMENT_COEFFS_X, tmpX, id);
503  mdIref.setValue(MDL_LOCAL_ALIGNMENT_COEFFS_Y, tmpY, id);
504  // Store control points
506  std::vector<size_t>{
510  id);
511  // Safe to file
512  mdIref.write("localAlignment@" + fnOut, MD_APPEND);
513 }
static std::pair< T, T > getShift(const BSplineGrid< T > &grid, Dimensions dim, size_t x, size_t y, size_t n)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
BSpline coefficient in Y dim.
std::vector< std::pair< FramePatchMeta< T >, Point2D< T > > > shifts
const AlignmentResult< T > & globalHint
T & value() const
Definition: optional.h:67
A shift amount at confidence level of 2.5%.
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
#define i
Three values representing number of control points used for local alignment (X, Y, N)
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
Two values representing number of patches used for local alignment (X, Y)
A shift amount at confidence level of 95.5%.
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
FileName fnOut
core::optional< BSplineGrid< T > > bsplineRep
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
BSpline coefficient in X dim.
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
bool isEmpty() const
Incorrect value received.
Definition: xmipp_error.h:195

Member Data Documentation

◆ fnAligned

template<typename T >
FileName AProgMovieAlignmentCorrelation< T >::fnAligned
protected

Aligned movie

Definition at line 320 of file movie_alignment_correlation_base.h.

◆ fnAvg

template<typename T >
FileName AProgMovieAlignmentCorrelation< T >::fnAvg
protected

Aligned micrograph

Definition at line 322 of file movie_alignment_correlation_base.h.

◆ fnInitialAvg

template<typename T >
FileName AProgMovieAlignmentCorrelation< T >::fnInitialAvg
protected

Aligned micrograph

Definition at line 326 of file movie_alignment_correlation_base.h.

◆ localAlignmentControlPoints

template<typename T >
Dimensions AProgMovieAlignmentCorrelation< T >::localAlignmentControlPoints = Dimensions(0)
protected

Control points used for local alignment

Definition at line 330 of file movie_alignment_correlation_base.h.

◆ localAlignPatches

template<typename T >
std::pair<size_t, size_t> AProgMovieAlignmentCorrelation< T >::localAlignPatches
protected

Number of patches used for local alignment

Definition at line 328 of file movie_alignment_correlation_base.h.

◆ maxShift

template<typename T >
float AProgMovieAlignmentCorrelation< T >::maxShift
protected

Max shift in pixels

Definition at line 318 of file movie_alignment_correlation_base.h.

◆ nfirst

template<typename T >
int AProgMovieAlignmentCorrelation< T >::nfirst
protected

First and last frame (inclusive)

Definition at line 316 of file movie_alignment_correlation_base.h.

◆ nfirstSum

template<typename T >
int AProgMovieAlignmentCorrelation< T >::nfirstSum
protected

First and last frame

Definition at line 324 of file movie_alignment_correlation_base.h.

◆ nlast

template<typename T >
int AProgMovieAlignmentCorrelation< T >::nlast
protected

Definition at line 316 of file movie_alignment_correlation_base.h.

◆ nlastSum

template<typename T >
int AProgMovieAlignmentCorrelation< T >::nlastSum
protected

Definition at line 324 of file movie_alignment_correlation_base.h.

◆ solverIterations

template<typename T >
constexpr int AProgMovieAlignmentCorrelation< T >::solverIterations = 2
staticprotected

Solver iterations

Definition at line 332 of file movie_alignment_correlation_base.h.


The documentation for this class was generated from the following files: