KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pt_scene_diff.h
Go to the documentation of this file.
1 /* =========================================================================== *
2 |
3 | Copyright (c) 1994-2008 by Kobus Barnard (author).
4 |
5 | Personal and educational use of this code is granted, provided that this
6 | header is kept intact, and that the authorship is not misrepresented, that
7 | its use is acknowledged in publications, and relevant papers are cited.
8 |
9 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10 |
11 | Please note that the code in this file has not necessarily been adequately
12 | tested. Naturally, there is no guarantee of performance, support, or fitness
13 | for any particular task. Nonetheless, I am interested in hearing about
14 | problems that you encounter.
15 |
16 | Authors:
17 | Ernesto Brau
18 |
19 * =========================================================================== */
20 
21 /* $Id$ */
22 
23 #ifndef PT_SCENE_DIFF_H
24 #define PT_SCENE_DIFF_H
25 
28 #include <diff_cpp/diff_gradient.h>
29 #include <m_cpp/m_vector.h>
31 #include <vector>
32 #include <algorithm>
33 
34 namespace kjb {
35 namespace pt {
36 
39 {
40 public:
41  Scene_gradient(const Scene_posterior& post, const std::vector<double>& dx) :
42  post_(post), dx_(dx)
43  {}
44 
45  std::vector<double> operator()(const Scene& scene) const
46  {
47  Vector gv = gradient_ind_cfd(post_, scene, dx_, post_.adapter());
48  post_.reset();
49  return std::vector<double>(gv.begin(), gv.end());
50  }
51 
52 private:
53  Scene_posterior_ind post_;
54  std::vector<double> dx_;
55 };
56 
59 {
60 public:
61  Scene_hessian(const Scene_posterior& post, const std::vector<double>& dx) :
62  post_(post), dx_(dx)
63  {}
64 
65  Matrix operator()(const Scene& scene) const
66  {
67  Matrix H = hessian_symmetric_ind(post_, scene, dx_, post_.adapter());
68  post_.reset();
69  return H;
70  }
71 
72  void set_diagonals(const Scene& scene) const;
73 
74 private:
75  Scene_posterior_ind post_;
76  std::vector<double> dx_;
77 };
78 
83 template<class G>
85 {
86 private:
87  typedef std::vector<double> vec_t;
88 
89 public:
91  Gradient_adapter(const G& grad) : m_grad(grad)
92  {}
93 
95  template<class M>
96  vec_t operator()(const M& m) const
97  {
98  Vector g1 = m_grad(m);
99  vec_t g2(g1.size());
100 
101  std::copy(g1.begin(), g1.end(), g2.begin());
102 
103  return g2;
104  }
105 
106 private:
107  const G& m_grad;
108 };
109 
113 template<class G>
114 inline
116 {
117  return Gradient_adapter<G>(grad);
118 }
119 
122 {
123 public:
128  Pixel_move(const Matrix& C) : H(3, 3)
129  {
130  H.set_col(0, C.get_col(0));
131  H.set_col(1, C.get_col(2));
132  H.set_col(2, C.get_col(3));
133  K = matrix_inverse(H);
134 
135  ej1 = K.get_col(0);
136  ej2 = K.get_col(1);
137  eh31 = ej1[2];
138  eh32 = ej2[2];
139  ej1.resize(2);
140  ej2.resize(2);
141 
142  set_point(Vector().set(0.0, 0.0));
143  }
144 
146  void set_point(const Vector& pt)
147  {
148  r = pt;
149 
150  Vector rr = r;
151  rr.resize(3, 1.0);
152  w_G = 1 / dot(H.get_row(2), rr);
153  }
154 
156  Vector x(double eps) const
157  {
158  return (w_G*r + eps*ej1) / (w_G + eps*eh31);
159  }
160 
162  Vector y(double eps) const
163  {
164  return (w_G*r + eps*ej2) / (w_G + eps*eh32);
165  }
166 
167 private:
168  Matrix H;
169  Matrix K;
170  Vector ej1;
171  Vector ej2;
172  double eh31;
173  double eh32;
174  Vector r;
175  double w_G;
176 };
177 
182 std::vector<double> trajectory_gradient_step_sizes
183 (
184  const Scene& scene,
185  bool infer_head = true
186 );
187 
189 void make_max
190 (
191  const Scene& scene,
192  const Scene_posterior& pt,
193  const std::vector<double>& ss,
194  bool infer_head
195 );
196 
197 }} // namespace kjb::pt
198 
199 #endif /*PT_SCENE_DIFF_H */
200 
Vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: m_vector.cpp:242
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
void set_col(int col, const Generic_vector &v)
Replace a column of the matrix with the given vector. "vector" can be any collection of values conver...
Definition: m_matrix.h:1726
Gradient_adapter(const G &grad)
Construct a gradient adapter that wraps 'grad'.
Definition: pt_scene_diff.h:91
Wrapper for generic gradient function.
Definition: pt_scene_diff.h:38
Wraps Scene_gradient to work with ergo::hmc_step.
Definition: pt_scene_diff.h:84
const Scene_adapter & adapter() const
Definition: pt_scene_posterior.h:271
#define eps
Definition: hog.h:32
Functor designed to move 3D points by pixels in image plane.
Definition: pt_scene_diff.h:121
Class that represents a full scene in the PT universe.
Definition: pt_scene.h:40
Scene_hessian(const Scene_posterior &post, const std::vector< double > &dx)
Definition: pt_scene_diff.h:61
Matrix operator()(const Scene &scene) const
Definition: pt_scene_diff.h:65
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Pixel_move(const Matrix &C)
Construct a pixel-move functor with the given step size and camera.
Definition: pt_scene_diff.h:128
Vector y(double eps) const
Move point in the x-axis of the image.
Definition: pt_scene_diff.h:162
void reset() const
Definition: pt_scene_posterior.h:273
Matrix matrix_inverse(const Matrix &op1)
Invert this matrix.
Definition: m_matrix.cpp:730
void set_point(const Vector &pt)
Set the point to move.
Definition: pt_scene_diff.h:146
function straight edges straight lines(nlines,[x1 x2 y1 y2 theta r])%%To display result ss
Definition: APPgetLargeConnectedEdges.m:20
Matrix hessian_symmetric_ind(const Func &f, const Model &x, const std::vector< double > &dx, const Adapter &adapter)
Computes the Hessian of an "independent" function, evaluated at a point, using finite differences...
Definition: diff_hessian_ind.h:157
Posterior adapter to work with independent gradient computation.
Definition: pt_scene_posterior.h:239
iterator end()
Definition: m_vector.h:557
Scene_gradient(const Scene_posterior &post, const std::vector< double > &dx)
Definition: pt_scene_diff.h:41
iterator begin()
Definition: m_vector.h:537
std::vector< double > trajectory_gradient_step_sizes(const Scene &scene, bool infer_head=true)
Compute the step sizes that equivalent to movement of 1 pixel in image coordinate.
Definition: pt_scene_diff.cpp:128
Posterior distribution of a scene.
Definition: pt_scene_posterior.h:53
void make_max(const Scene &scene, const Scene_posterior &pt, const std::vector< double > &ss, bool infer_head)
Ensures posterior is at max (wrt a given step size).
Definition: pt_scene_diff.cpp:196
Vec_type get_row(int row) const
Return a specified row of this matrix, in the form of a Vector.
Definition: m_matrix.cpp:604
vec_t operator()(const M &m) const
Computes the gradient numerically.
Definition: pt_scene_diff.h:96
Gradient_adapter< G > make_gradient_adapter(const G &grad)
Utility function that makes a gradient adapter.
Definition: pt_scene_diff.h:115
Wrapper for generic hessian function.
Definition: pt_scene_diff.h:58
Vec_type get_col(int col) const
Return a specified column of this matrix, in the form of a Vector.
Definition: m_matrix.cpp:613
std::vector< double > operator()(const Scene &scene) const
Definition: pt_scene_diff.h:45
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
long int dot(const Int_vector &op1, const Int_vector &op2)
Returns dot product of this and op2.
Definition: l_int_vector.h:1532
for m
Definition: APPgetLargeConnectedEdges.m:64
Vector gradient_ind_cfd(const Func &f, const Model &x, const std::vector< double > &dx, const Adapter &adapter)
Computes the gradient of a function, evaluated at a point, using central finite differences.
Definition: diff_gradient.h:200
Vector x(double eps) const
Move point in the x-axis of the image.
Definition: pt_scene_diff.h:156
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void set_diagonals(const Scene &scene) const
Definition: pt_scene_diff.cpp:40