PICCANTE  0.4
The hottest HDR imaging library!
nelder_mead_opt_gordon_lowe.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_GORDON_LOWE_HPP
19 #define PIC_COMPUTER_VISION_NELDER_MEAD_OPT_GORDON_LOWE_HPP
20 
21 #include "../util/matrix_3_x_3.hpp"
22 #include "../util/nelder_mead_opt_base.hpp"
23 #include "../computer_vision/camera_matrix.hpp"
24 
25 #ifndef PIC_DISABLE_EIGEN
26 #ifndef PIC_EIGEN_NOT_BUNDLED
27  #include "../externals/Eigen/Dense"
28 #else
29  #include <Eigen/Dense>
30 #endif
31 #endif
32 
33 namespace pic {
34 
35 #ifndef PIC_DISABLE_EIGEN
36 
37 #define GL_PACKED_CAMERA_SIZE 11
38 #define GL_3D_POINT_SIZE 3
39 
41 {
42 public:
43  std::vector< std::vector< Vec<2, float> > > m;
44 
50  NelderMeadOptGordonLowe(std::vector< Vec<2, float> > m0, std::vector< Vec<2, float> > m1) : NelderMeadOptBase()
51  {
52  this->m.push_back(m0);
53  this->m.push_back(m1);
54  }
55 
56  static Eigen::Matrix34d parseCameraMatrix(float *x, unsigned int index)
57  {
58  Eigen::Matrix3d K, R;
59  Eigen::Vector3d t;
60 
61  //offset of the matrix
62  unsigned int c = index * 11;
63 
64  Eigen::Quaternion<double> reg;
65  reg.x() = x[c]; c++;
66  reg.y() = x[c]; c++;
67  reg.z() = x[c]; c++;
68  reg.w() = x[c]; c++;
69  R = reg.toRotationMatrix();
70 
71  t[0] = x[c]; c++;
72  t[1] = x[c]; c++;
73  t[2] = x[c]; c++;
74 
75  K.setZero();
76  K(0, 0) = x[c]; c++;
77  K(1, 1) = x[c]; c++;
78  K(0, 2) = x[c]; c++;
79  K(1, 2) = x[c];
80  K(2, 2) = 1.0;
81 
82  return getCameraMatrix(K, R, t);
83  }
84 
91  double ProjectionError(float *x, unsigned int index) {
92 
93  double err = 0.0;
94 
95  Eigen::Matrix34d P = parseCameraMatrix(x, index);
96 
97  //offset of vertices
98  int c = GL_PACKED_CAMERA_SIZE * int(m.size());
99 
100  Eigen::Vector4d point;
101  for(int i = 0; i < m[index].size(); i++) {
102  point = Eigen::Vector4d(x[c], x[c + 1], x[c + 2], 1.0);
103 
104  Eigen::Vector3d point_proj = P * point;
105  point_proj /= point_proj[2];
106 
107  double dx = point_proj[0] - m[index][i][0];
108  double dy = point_proj[1] - m[index][i][1];
109 
110  err += dx * dx + dy * dy;
111 
112  c += 3;
113  }
114 
115 
116  return err;
117  }
118 
125  float function(float *x, unsigned int n)
126  {
127  int n2 = int(m.size() * m[0].size());
128  double err = sqrt((ProjectionError(x, 0) + ProjectionError(x, 1)) / double(n2));
129 
130  return float(err);
131  }
132 
133 
141  static void init3DPoints(Eigen::Matrix3d K, std::vector< Vec<2, float> > &m, std::vector< Eigen::Vector3d > &x, float distance = 20.0f)
142  {
143  Eigen::Matrix3d K_inv = K.inverse();
144  \
145  printf("Points: %zd\n", m.size());
146 
147  for(unsigned int i = 0; i < m.size(); i++) {
148  Eigen::Vector3d point = Eigen::Vector3d (m[i][0], m[i][1], 1.0);
149 
150  point = K_inv * point;
151 
152  point *= distance;
153 
154  x.push_back(point);
155  }
156  }
157 
167  static double *prepareInputData(std::vector< Eigen::Matrix3d > &K, std::vector< Eigen::Matrix3d > &R, std::vector< Eigen::Vector3d > &t, std::vector< Eigen::Vector3d > &x, unsigned int &ret_size)
168  {
169  if(R.size() != t.size()) {
170  return NULL;
171  }
172 
173  if(x.empty()) {
174  return NULL;
175  }
176 
177  int n = int (R.size());
178  ret_size = GL_PACKED_CAMERA_SIZE * n + GL_3D_POINT_SIZE * int(x.size());
179  double *ret = new double[ret_size];
180 
181  int c = 0;
182  for(int i = 0; i < n; i++) {
183 
184  Eigen::Quaternion<double> reg(R[i]);
185 
186  ret[c] = reg.x(); c++;
187  ret[c] = reg.y(); c++;
188  ret[c] = reg.z(); c++;
189  ret[c] = reg.w(); c++;
190 
191  ret[c] = t[i][0]; c++;
192  ret[c] = t[i][1]; c++;
193  ret[c] = t[i][2]; c++;
194 
195  ret[c] = K[i](0, 0); c++;
196  ret[c] = K[i](1, 1); c++;
197  ret[c] = K[i](0, 2); c++;
198  ret[c] = K[i](1, 2); c++;
199  }
200 
201  for(size_t i = 0; i < x.size(); i++) {
202  ret[c] = x[i][0]; c++;
203  ret[c] = x[i][1]; c++;
204  ret[c] = x[i][2]; c++;
205  }
206 
207  return ret;
208  }
209 };
210 
211 #endif
212 
213 }
214 
215 #endif // PIC_COMPUTER_VISION_NELDER_MEAD_OPT_GORDON_LOWE_HPP
std::vector< std::vector< Vec< 2, float > > > m
Definition: nelder_mead_opt_gordon_lowe.hpp:43
NelderMeadOptGordonLowe(std::vector< Vec< 2, float > > m0, std::vector< Vec< 2, float > > m1)
NelderMeadOptGordonLowe.
Definition: nelder_mead_opt_gordon_lowe.hpp:50
PIC_INLINE Eigen::Matrix34d getCameraMatrix(Eigen::Matrix3d &K, Eigen::Matrix3d &R, Eigen::Vector3d &t)
getCameraMatrix
Definition: camera_matrix.hpp:132
static double * prepareInputData(std::vector< Eigen::Matrix3d > &K, std::vector< Eigen::Matrix3d > &R, std::vector< Eigen::Vector3d > &t, std::vector< Eigen::Vector3d > &x, unsigned int &ret_size)
prepareInputData
Definition: nelder_mead_opt_gordon_lowe.hpp:167
#define GL_PACKED_CAMERA_SIZE
Definition: nelder_mead_opt_gordon_lowe.hpp:37
#define GL_3D_POINT_SIZE
Definition: nelder_mead_opt_gordon_lowe.hpp:38
static Eigen::Matrix34d parseCameraMatrix(float *x, unsigned int index)
Definition: nelder_mead_opt_gordon_lowe.hpp:56
The NelderMeadOptBase class.
Definition: nelder_mead_opt_base.hpp:31
static void init3DPoints(Eigen::Matrix3d K, std::vector< Vec< 2, float > > &m, std::vector< Eigen::Vector3d > &x, float distance=20.0f)
init3DPoints
Definition: nelder_mead_opt_gordon_lowe.hpp:141
Definition: nelder_mead_opt_gordon_lowe.hpp:40
Definition: bilateral_separation.hpp:25
double ProjectionError(float *x, unsigned int index)
ProjectionError.
Definition: nelder_mead_opt_gordon_lowe.hpp:91
The Vec class.
Definition: vec.hpp:35