Xmipp  v3.23.11-Nereus
Static Public Member Functions | List of all members
BSplineHelper Class Reference

#include <bspline_helper.h>

Static Public Member Functions

template<typename T >
static std::pair< Matrix1D< T >, Matrix1D< T > > computeBSplineCoeffs (const Dimensions &movieSize, const LocalAlignmentResult< T > &alignment, const Dimensions &controlPoints, const std::pair< size_t, size_t > &noOfPatches, int verbosity, int solverIters)
 
template<typename T >
static std::pair< T, T > getShift (const BSplineGrid< T > &grid, Dimensions dim, size_t x, size_t y, size_t n)
 
template<typename T >
static T Bspline03 (T argument)
 
template<typename T >
static void getShift (int lX, int lY, int lN, int xdim, int ydim, int ndim, int x, int y, int n, T &shiftY, T &shiftX, const T *coeffsX, const T *coeffsY)
 

Detailed Description

Definition at line 41 of file bspline_helper.h.

Member Function Documentation

◆ Bspline03()

template<typename T >
static T BSplineHelper::Bspline03 ( argument)
inlinestatic

Interpolate by cubic BSpline

Parameters
argumentto interpolate

Definition at line 78 of file bspline_helper.h.

78  {
79 
80  static_assert(std::is_same<float, T>::value
81  || std::is_same<double, T>::value, "T must be either float or double");
82  if (std::is_same<float, T>::value) {
83  argument = fabsf(argument);
84  } else {
85  argument = fabs(argument);
86  }
87  if (argument < (T)1) {
88  return(argument * argument * (argument - (T)2) * (T)0.5 + (T)2 / (T)3);
89  }
90  else if (argument < (T)2) {
91  argument -= (T)2;
92  return(argument * argument * argument * ((T)-1 / (T)6));
93  }
94  else {
95  return (T)0;
96  }
97  }

◆ computeBSplineCoeffs()

template<typename T >
std::pair< Matrix1D< T >, Matrix1D< T > > BSplineHelper::computeBSplineCoeffs ( const Dimensions movieSize,
const LocalAlignmentResult< T > &  alignment,
const Dimensions controlPoints,
const std::pair< size_t, size_t > &  noOfPatches,
int  verbosity,
int  solverIters 
)
static

Computes BSpline coefficients from given data

Parameters
movieSize
alignmentto use
controlPointsof the resulting spline
noOfPatchesused for generating the alignment
verbositylevel
solverItersmax iterations of the solver
Returns
coefficients of the BSpline representing the local shifts

Definition at line 34 of file bspline_helper.cpp.

37  {
38  if(verbosity) std::cout << "Computing BSpline coefficients" << std::endl;
39  // get coefficients of the BSpline that can represent the shifts (formula from the paper)
40  int lX = controlPoints.x();
41  int lY = controlPoints.y();
42  int lT = controlPoints.n();
43  int noOfPatchesXY = noOfPatches.first * noOfPatches.second;
44  Matrix2D<T>A(noOfPatchesXY*movieSize.n(), lX * lY * lT);
45  Matrix1D<T>bX(noOfPatchesXY*movieSize.n());
46  Matrix1D<T>bY(noOfPatchesXY*movieSize.n());
47  T hX = (lX == 3) ? movieSize.x() : (movieSize.x() / (T)(lX-3));
48  T hY = (lY == 3) ? movieSize.y() : (movieSize.y() / (T)(lY-3));
49  T hT = (lT == 3) ? movieSize.n() : (movieSize.n() / (T)(lT-3));
50 
51  for (auto &&r : alignment.shifts) {
52  auto meta = r.first;
53  auto shift = r.second;
54  int tileIdxT = meta.id_t;
55  int tileCenterT = tileIdxT * 1 + 0 + 0;
56  int tileIdxX = meta.id_x;
57  int tileIdxY = meta.id_y;
58  int tileCenterX = meta.rec.getCenter().x;
59  int tileCenterY = meta.rec.getCenter().y;
60  int i = (tileIdxY * noOfPatches.first) + tileIdxX;
61 
62  for(int controlIdxT = -1; controlIdxT < (lT - 1); ++controlIdxT) {
63  T tmpT = Bspline03((tileCenterT / hT) - controlIdxT);
64  if (tmpT == (T)0) continue;
65  for(int controlIdxY = -1; controlIdxY < (lY - 1); ++controlIdxY) {
66  T tmpY = Bspline03((tileCenterY / hY) - controlIdxY);
67  if (tmpY == (T)0) continue;
68  for(int controlIdxX = -1; controlIdxX < (lX - 1); ++controlIdxX) {
69  T tmpX = Bspline03((tileCenterX / hX) - controlIdxX);
70  T val = tmpT * tmpY * tmpX;
71  int j = ((controlIdxT + 1) * lX * lY) +
72  ((controlIdxY + 1) * lX) + (controlIdxX + 1);
73  MAT_ELEM(A,tileIdxT*noOfPatchesXY + i, j) = val;
74  }
75  }
76  }
77  VEC_ELEM(bX,tileIdxT*noOfPatchesXY + i) = -shift.x; // we want the BSPline describing opposite transformation,
78  VEC_ELEM(bY,tileIdxT*noOfPatchesXY + i) = -shift.y; // so that we can use it to compensate for the shift
79  }
80 
81  // solve the equation system for the spline coefficients
82  Matrix1D<T> coefsX;
83  Matrix1D<T> coefsY;
84  EquationSystemSolver::solve(bX, bY, A, coefsX, coefsY, verbosity + 1, solverIters);
85  return std::make_pair(coefsX, coefsY);
86 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
std::vector< std::pair< FramePatchMeta< T >, Point2D< T > > > shifts
static T Bspline03(T argument)
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
Definition: mask.h:36
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
static void solve(Matrix1D< T > &bXt, Matrix1D< T > &bYt, Matrix2D< T > &At, Matrix1D< T > &shiftXt, Matrix1D< T > &shiftYt, int verbosity, int iterations)
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
#define j
Definition: ctf.h:38

◆ getShift() [1/2]

template<typename T >
std::pair< T, T > BSplineHelper::getShift ( const BSplineGrid< T > &  grid,
Dimensions  dim,
size_t  x,
size_t  y,
size_t  n 
)
static

Get shift of the particular point using BSpline interpolation

Parameters
gridused for interpolation
dimof the original problem
xqueried position
yqueried position
nqueried position
Returns
shifted position

Definition at line 95 of file bspline_helper.cpp.

96  {
97  T shiftX = 0;
98  T shiftY = 0;
99  getShift(grid.getDim().x(), grid.getDim().y(), grid.getDim().n(),
100  dim.x(), dim.y(), dim.n(),
101  x, y, n,
102  shiftX, shiftY,
103  grid.getCoeffsX().vdata, grid.getCoeffsY().vdata);
104 
105  return std::make_pair(shiftX, shiftY);
106 }
static std::pair< T, T > getShift(const BSplineGrid< T > &grid, Dimensions dim, size_t x, size_t y, size_t n)
static double * y
constexpr const Matrix1D< T > & getCoeffsX() const
Definition: bspline_grid.h:46
constexpr const Dimensions & getDim() const
Definition: bspline_grid.h:42
doublereal * x
constexpr const Matrix1D< T > & getCoeffsY() const
Definition: bspline_grid.h:50
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
int * n

◆ getShift() [2/2]

template<typename T >
void BSplineHelper::getShift ( int  lX,
int  lY,
int  lN,
int  xdim,
int  ydim,
int  ndim,
int  x,
int  y,
int  n,
T &  shiftY,
T &  shiftX,
const T *  coeffsX,
const T *  coeffsY 
)
static

Get shift of the particular point using BSpline interpolation

Parameters
lXno of control points in X (including end points)
lYno of control points in Y (including end points)
lNno of control points in N (including end points)
xdimsize of the input
ydimsize of the input
ndimsize of the input
xqueried position
yqueried position
nqueried position
shiftXshifted position
shiftYshifted position
coeffsXBSpline coefficients
coeffsYBSpline coefficients

Definition at line 109 of file bspline_helper.cpp.

113  {
114  using std::max;
115  using std::min;
116 
117  T delta = 0.0001;
118  // take into account end points
119  T hX = (lX == 3) ? xdim : (xdim / (T) (lX - 3));
120  T hY = (lY == 3) ? ydim : (ydim / (T) (lY - 3));
121  T hT = (lN == 3) ? ndim : (ndim / (T) (lN - 3));
122  // index of the 'cell' where pixel is located (<0, N-3> for N control points)
123  T xPos = x / hX;
124  T yPos = y / hY;
125  T tPos = n / hT;
126  // indices of the control points are from -1 .. N-2 for N points
127  // pixel in 'cell' 0 may be influenced by points with indices <-1,2>
128  for (int idxT = max(-1, (int) (tPos) - 1);
129  idxT <= min((int) (tPos) + 2, lN - 2);
130  ++idxT) {
131  T tmpT = Bspline03(tPos - idxT);
132  for (int idxY = max(-1, (int) (yPos) - 1);
133  idxY <= min((int) (yPos) + 2, lY - 2);
134  ++idxY) {
135  T tmpY = Bspline03(yPos - idxY);
136  for (int idxX = max(-1, (int) (xPos) - 1);
137  idxX <= min((int) (xPos) + 2, lX - 2);
138  ++idxX) {
139  T tmpX = Bspline03(xPos - idxX);
140  T tmp = tmpX * tmpY * tmpT;
141  if (fabsf(tmp) > delta) {
142  size_t coeffOffset = (idxT + 1) * (lX * lY)
143  + (idxY + 1) * lX + (idxX + 1);
144  shiftX += coeffsX[coeffOffset] * tmp;
145  shiftY += coeffsY[coeffOffset] * tmp;
146  }
147  }
148  }
149  }
150 }
void min(Image< double > &op1, const Image< double > &op2)
static double * y
static T Bspline03(T argument)
doublereal * x
void max(Image< double > &op1, const Image< double > &op2)
int * n
double * delta

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