PICCANTE  0.4
The hottest HDR imaging library!
nelder_mead_opt_homography.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_COMPUTER_VISION_NELDER_MEAD_OPT_HOMOGRAPHY_HPP
19 #define PIC_COMPUTER_VISION_NELDER_MEAD_OPT_HOMOGRAPHY_HPP
20 
21 #include "../util/nelder_mead_opt_base.hpp"
22 #include "../util/std_util.hpp"
23 
24 #ifndef PIC_DISABLE_EIGEN
25 #ifndef PIC_EIGEN_NOT_BUNDLED
26  #include "../externals/Eigen/Dense"
27 #else
28  #include <Eigen/Dense>
29 #endif
30 #endif
31 
32 namespace pic {
33 
34 #ifndef PIC_DISABLE_EIGEN
35 
37 {
38 public:
39  std::vector< Eigen::Vector2f > m0, m1;
40 
47  NelderMeadOptHomography(std::vector< Eigen::Vector2f > &m0,
48  std::vector< Eigen::Vector2f > &m1,
49  std::vector< unsigned int > inliers) : NelderMeadOptBase()
50  {
51  filterInliers(m0, inliers, this->m0);
52  filterInliers(m1, inliers, this->m1);
53  }
54 
61  Eigen::Vector2f Homography(Eigen::Matrix3f &H, Eigen::Vector2f &p)
62  {
63  Eigen::Vector3f ret = H * Eigen::Vector3f(p[0], p[1], 1.0f);
64 
65  return Eigen::Vector2f(ret[0] / ret[2], ret[1] / ret[2]);
66  }
67 
74  float function(float *x, unsigned int n)
75  {
76  float err = 0.0f;
77 
78  Eigen::Matrix3f H = getMatrixfFromLinearArray(x, 3, 3);
79  H(2, 2) = 1.0f;
80 
81  Eigen::Matrix3f H_inv = H.inverse();
82 
83  for(unsigned int i = 0; i < m0.size(); i++) {
84  float dU, dV;
85 
86  // | H p0 - p1 | error
87  Eigen::Vector2f p0_H = Homography(H, m0[i]);
88 
89  dU = m1[i][0] - p0_H[0];
90  dV = m1[i][1] - p0_H[1];
91 
92  err += dU * dU + dV * dV;
93 
94  // | H p1 - p0 | error
95  Eigen::Vector2f p1_H = Homography(H_inv, m1[i]);
96 
97  dU = m0[i][0] - p1_H[0];
98  dV = m0[i][1] - p1_H[1];
99 
100  err += dU * dU + dV * dV;
101 
102  }
103 
104  return err;
105  }
106 
115  float *run(float *x_start, unsigned int n, float epsilon = 1e-4f, int max_iterations = 1000, float *x = NULL)
116  {
117  if(n != 8) {
118  return x_start;
119  }
120 
121  if(x == NULL) {
122  x = new float[n + 1];
123  }
124 
125  x = run_aux(x_start, n, epsilon, max_iterations, x);
126  x[8] = 1.0f;
127 
128  return x;
129  }
130 };
131 
132 #endif
133 
134 }
135 
136 #endif // PIC_COMPUTER_VISION_NELDER_MEAD_OPT_HOMOGRAPHY_HPP
float * run(float *x_start, unsigned int n, float epsilon=1e-4f, int max_iterations=1000, float *x=NULL)
run
Definition: nelder_mead_opt_homography.hpp:115
int max_iterations
Definition: nelder_mead_opt_base.hpp:295
std::vector< Eigen::Vector2f > m0
Definition: nelder_mead_opt_homography.hpp:39
std::vector< Eigen::Vector2f > m1
Definition: nelder_mead_opt_homography.hpp:39
PIC_INLINE Eigen::MatrixXf getMatrixfFromLinearArray(float *array, int rows, int cols)
getMatrixFromLinearArray
Definition: eigen_util.hpp:448
Eigen::Vector2f Homography(Eigen::Matrix3f &H, Eigen::Vector2f &p)
Homography.
Definition: nelder_mead_opt_homography.hpp:61
NelderMeadOptHomography(std::vector< Eigen::Vector2f > &m0, std::vector< Eigen::Vector2f > &m1, std::vector< unsigned int > inliers)
NelderMeadOptHomography.
Definition: nelder_mead_opt_homography.hpp:47
The NelderMeadOptBase class.
Definition: nelder_mead_opt_base.hpp:31
void filterInliers(std::vector< T > &vec, std::vector< unsigned int > &inliers, std::vector< T > &vecOut)
filterInliers
Definition: std_util.hpp:32
float * run_aux(float *x_start, unsigned int n, float epsilon, int max_iterations=1000, float *x=NULL)
run_aux
Definition: nelder_mead_opt_base.hpp:186
Definition: bilateral_separation.hpp:25
Definition: nelder_mead_opt_homography.hpp:36