MTF
warpUtils.h
1 #ifndef MTF_WARP_UTILS_H
2 #define MTF_WARP_UTILS_H
3 
4 #include "mtf/Macros/common.h"
5 
6 _MTF_BEGIN_NAMESPACE
7 
8 namespace utils{
9  template<typename T1, typename T2>
10  inline void homogenize(const T1 &dehom_mat, T2 &hom_mat){
11  assert(hom_mat.cols() == dehom_mat.cols());
12  hom_mat.topRows(2) = dehom_mat;
13  hom_mat.row(2).fill(1);
14  }
15 
16  template<typename T1, typename T2>
17  inline void dehomogenize(const T1 &hom_mat, T2 &dehom_mat){
18  assert(hom_mat.cols() == dehom_mat.cols());
19  dehom_mat = hom_mat.topRows(2);
20  dehom_mat = dehom_mat.array().rowwise() / hom_mat.array().row(2);
21  }
22 
23  // returning versions
24  inline HomPtsT homogenize(const PtsT &dehom_mat){
25  HomPtsT hom_mat;
26  hom_mat.resize(NoChange, dehom_mat.cols());
27  homogenize(dehom_mat, hom_mat);
28  return hom_mat;
29  }
30  inline PtsT dehomogenize(const HomPtsT &hom_mat){
31  PtsT dehom_mat;
32  dehom_mat.resize(NoChange, hom_mat.cols());
33  dehomogenize(hom_mat, dehom_mat);
34  return dehom_mat;
35  }
36  inline ProjWarpT getTranslationMatrix(double tx, double ty){
37  ProjWarpT trans_mat = ProjWarpT::Identity();
38  trans_mat(0, 2) = tx;
39  trans_mat(1, 2) = ty;
40  return trans_mat;
41  }
42  inline ProjWarpT getRotationMatrix(double theta){
43  ProjWarpT rot_mat = ProjWarpT::Identity();
44  double cos_theta = cos(theta);
45  double sin_theta = sin(theta);
46  rot_mat(0, 0) = cos_theta;
47  rot_mat(0, 1) = -sin_theta;
48  rot_mat(1, 0) = sin_theta;
49  rot_mat(1, 1) = cos_theta;
50  return rot_mat;
51  }
52  inline ProjWarpT getScalingMatrix(double s){
53  ProjWarpT scale_mat = ProjWarpT::Identity();
54  scale_mat(0, 0) += s;
55  scale_mat(1, 1) += s;
56  return scale_mat;
57  }
58  inline ProjWarpT getShearingMatrix(double a, double b){
59  ProjWarpT shear_mat = ProjWarpT::Identity();
60  shear_mat(0, 0) += a;
61  shear_mat(0, 1) = b;
62  return shear_mat;
63  }
64 
67  ProjWarpT computeHomographyDLT(const PtsT &in_pts, const PtsT &out_pts, int n_pix);
68  ProjWarpT computeHomographyDLT(const CornersT &in_corners, const CornersT &out_corners);
69  ProjWarpT computeHomographyNDLT(const CornersT &in_corners, const CornersT &out_corners);
70  ProjWarpT computeAffineDLT(const CornersT &in_corners, const CornersT &out_corners);
71  ProjWarpT computeAffineDLT(const PtsT &in_pts, const PtsT &out_pts);
72  ProjWarpT computeAffineDLT(const Matrix23d &in_pts, const Matrix23d &out_pts);
73  ProjWarpT computeAffineNDLT(const CornersT &in_corners, const CornersT &out_corners);
74  ProjWarpT computeASRTDLT(const CornersT &in_corners, const CornersT &out_corners);
75  ProjWarpT computeASRTDLT(const PtsT &in_pts, const PtsT &out_pts);
76  ProjWarpT computeSimilitudeDLT(const CornersT &in_corners, const CornersT &out_corners);
77  ProjWarpT computeSimilitudeNDLT(const CornersT &in_corners, const CornersT &out_corners);
78  ProjWarpT computeSimilitudeDLT(const PtsT &in_pts, const PtsT &out_pts);
79  ProjWarpT computeSimilitudeNDLT(const PtsT &in_pts, const PtsT &out_pts);
80  ProjWarpT computeASTDLT(const CornersT &in_corners, const CornersT &out_corners);
81  ProjWarpT computeASTDLT(const PtsT &in_corners, const PtsT &out_corners);
82  Vector3d computeIsometryDLT(const PtsT &in_pts, const PtsT &out_pts);
83  Vector3d computeIsometryDLT(const CornersT &in_pts, const CornersT &out_pts);
84  ProjWarpT computeISTDLT(const CornersT &in_corners, const CornersT &out_corners);
85  ProjWarpT computeISTDLT(const PtsT &in_corners, const PtsT &out_corners);
86 
87  void decomposeHomographyForward(ProjWarpT &affine_mat, ProjWarpT &proj_mat, const ProjWarpT &hom_mat);
88  void decomposeHomographyInverse(ProjWarpT &affine_mat, ProjWarpT &proj_mat, const ProjWarpT &hom_mat);
89  void decomposeAffineForward(Vector6d &affine_params, const ProjWarpT &affine_mat);
90  void decomposeAffineInverse(Vector6d &affine_params, const ProjWarpT &affine_mat);
91  void decomposeAffineInverse(ProjWarpT &trans_mat, ProjWarpT &rot_mat,
92  ProjWarpT &scale_mat, ProjWarpT &shear_mat, const ProjWarpT &affine_mat);
93  void decomposeAffineForward(ProjWarpT &trans_mat, ProjWarpT &rot_mat,
94  ProjWarpT &scale_mat, ProjWarpT &shear_mat, const ProjWarpT &affine_mat);
95 
96 
102  void normalizePts(CornersT &norm_pts, ProjWarpT &inv_norm_mat, const CornersT &pts);
103  void normalizePts(PtsT &norm_pts, ProjWarpT &inv_norm_mat, const PtsT &pts);
108  void getNormUnitSquarePts(PtsT &std_grid, CornersT &basis_corners,
109  int resx, int resy, double min_x = -0.5, double min_y = -0.5,
110  double max_x = 0.5, double max_y = 0.5);
111  void getPtsFromCorners(PtsT &pts, const CornersT &corners,
112  int resx, int resy);
114  PtsT getPtsFromCorners(const CornersT &corners,
115  int resx, int resy);
117  PtsT getPtsFromCorners(const cv::Mat &corners_cv,int resx, int resy);
118  void getPtsFromCorners(PtsT &pts, const CornersT &corners,
119  const PtsT basis_pts, const CornersT &basis_corners);
120 
126  void getBoundingPts(cv::Mat &bounding_pts, const PtsT &grid_pts, int res_x, int res_y);
131  bool isInsideRegion(const cv::Mat &verices, double testx, double testy);
133  void getBilinearPts(cv::Vec4i &neigh_pts_id, std::vector<cv::Vec2d> &neigh_pts_dist,
134  double x, double y, const mtf::PtsT &grid_pts, int n_pts);
136  int getNearestPt(double x, double y, const mtf::PtsT &grid_pts, int n_pts);
137 
138  MatrixX2d computeTPS(const CornersT &in_corners, const CornersT &out_corners);
139  void applyTPS(PtsT &out_pts, const PtsT &in_pts,
140  const PtsT &control_pts, const MatrixX2d &tps_params);
141  inline double tps(double r){ return r*r*log(r); }
142 
143  //void getSupportPoints(ProjWarpT &warp, PtsT &pts, HomPtsT &pts_hm,
144  // const CornersT &corners, const CornersT &basis_corners,
145  // const HomPtsT &basis_pts_hm);
146 }
147 _MTF_END_NAMESPACE
148 #endif
int getNearestPt(double x, double y, const mtf::PtsT &grid_pts, int n_pts)
returns the id of the grid point nearest to the given point
void getBilinearPts(cv::Vec4i &neigh_pts_id, std::vector< cv::Vec2d > &neigh_pts_dist, double x, double y, const mtf::PtsT &grid_pts, int n_pts)
returns the four nearest grid points around the given point that can be used for bilinear interpolati...
ProjWarpT computeHomographyDLT(const PtsT &in_pts, const PtsT &out_pts, int n_pix)
Variants of Direct Linear Transformation (DLT) algorithm to estimate best fit parameters for differen...
void normalizePts(CornersT &norm_pts, ProjWarpT &inv_norm_mat, const CornersT &pts)
normalizes the given points so that their mean (centroid) moves to origin and their mean distance fro...
void getBoundingPts(cv::Mat &bounding_pts, const PtsT &grid_pts, int res_x, int res_y)
extract points along the boundary of the given region represented by a grid of points with the given ...
bool isInsideRegion(const cv::Mat &verices, double testx, double testy)
tests if the given point is inside the given region - specified by the vertices of the corresponding ...
basic functions for preprocessing the raw input image using filtering, resizing and histogram equaliz...
Definition: histUtils.h:20
void getNormUnitSquarePts(PtsT &std_grid, CornersT &basis_corners, int resx, int resy, double min_x=-0.5, double min_y=-0.5, double max_x=0.5, double max_y=0.5)
computes the 2D coordinates for an equally spaced grid of points that covers a square centered at the...