KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pt_target.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 * =========================================================================== */
17 
18 /* $Id: pt_target.h 18971 2015-04-29 23:31:48Z jguan1 $ */
19 
20 #ifndef PT_TARGET_H_
21 #define PT_TARGET_H_
22 
29 #include <m_cpp/m_vector.h>
32 #include <gp_cpp/gp_base.h>
33 #include <gp_cpp/gp_mean.h>
34 #include <gp_cpp/gp_covariance.h>
35 #include <gp_cpp/gp_prior.h>
36 #include <gp_cpp/gp_normal.h>
39 #include <boost/optional.hpp>
40 
41 namespace kjb {
42 namespace pt {
43 
44 // forward declarations
45 class Scene;
46 
50 class Target : public mcmcda::Generic_track<Detection_box>
51 {
52 private:
53  typedef gp::Prior<gp::Zero, gp::Squared_exponential> Gpp;
54 
55 public:
57 
59  Target(double height, double width, double girth, size_t vlen) :
60  traj(vlen, height, width, girth),
61  btraj(vlen, height, width, girth),
62  ftraj(vlen, height, width, girth),
63  gp_pos_prior(
64  gp::Zero(),
65  gp::Sqex(10.0, 100.0),
66  gp::Inputs::const_iterator(),
67  gp::Inputs::const_iterator()),
68  gp_dir_prior(
69  gp::Zero(),
70  gp::Sqex(200.0, M_PI*M_PI/16.0),
71  gp::Inputs::const_iterator(),
72  gp::Inputs::const_iterator()),
73  gp_fdir_prior(
74  gp::Zero(),
75  gp::Sqex(200.0, M_PI*M_PI/64.0),
76  gp::Inputs::const_iterator(),
77  gp::Inputs::const_iterator())
78  {
79  IFT(height > 0 && width > 0 && girth > 0, Illegal_argument,
80  "Target dimensions must all be positive.");
81  }
82 
83 public:
86  {
87  return traj;
88  }
89 
92  {
93  return btraj;
94  }
95 
98  {
99  return ftraj;
100  }
101 
103  const Gpp& pos_prior() const
104  {
105  return gp_pos_prior;
106  }
107 
109  const Gpp& dir_prior() const
110  {
111  return gp_dir_prior;
112  }
113 
115  const Gpp& fdir_prior() const
116  {
117  return gp_fdir_prior;
118  }
119 
120 public:
122  void set_unchanged() const
123  {
124  set_changed_start(-1);
125  set_changed_end(-1);
126  }
127 
129  void set_changed_all() const
130  {
133  }
134 
136  bool changed() const
137  {
138  if(changed_start() == -1 || changed_end() == -1)
139  {
140  assert(changed_start() == -1 && changed_end() == -1);
141  return false;
142  }
143 
144  assert(changed_start() >= get_start_time()
145  && changed_end() <= get_end_time());
146  return true;
147  }
148 
149 public:
152  (
153  const Perspective_camera& cam,
154  double nsx = 0.01,
155  double nsz = 1.0
156  ) const;
157 
160  (
161  const Perspective_camera& cam,
162  const std::vector<Integral_flow>& x_flows,
163  const std::vector<Integral_flow>& y_flows,
164  const std::vector<Integral_flow>& back_x_flows,
165  const std::vector<Integral_flow>& back_y_flows,
166  size_t frame_rate,
167  double nsx = 0.01,
168  double nsz = 1.0
169  ) const;
170 
172  void estimate_height(const Perspective_camera& cam) const;
173 
175  void refine_trajectory(const Perspective_camera& cam) const;
176 
179  (
180  bool infer_head = true,
181  double nsb = 0.1,
182  double nsf = 0.1
183  ) const;
184 
185 private:
199  template<class GetTraj, class GetMean>
200  Vector smooth_trajectory
201  (
202  size_t sf,
203  size_t ef,
204  const std::vector<double>& nvar,
205  double scale,
206  double svar,
207  const GetTraj& data,
208  const GetMean& mean
209  ) const;
210 
211 public:
213  void update_pos_gp(double sc, double sv) const
214  {
215  update_gp(gp_pos_prior, sc, sv);
216  }
217 
219  void update_dir_gp(double sc, double sv) const
220  {
221  update_gp(gp_dir_prior, sc, sv);
222  }
223 
225  void update_fdir_gp(double sc, double sv) const
226  {
227  update_gp(gp_fdir_prior, sc, sv);
228  }
229 
231  void update_boxes(const Perspective_camera& cam) const;
232 
234  void update_faces(const Perspective_camera& cam) const;
235 
238  {
239  return hessian_;
240  }
241 
242 public:
246  friend
247  void swap(Target& t1, Target& t2)
248  {
249  using std::swap;
250 
251  swap(t1.traj, t2.traj);
252  swap(t1.btraj, t2.btraj);
253  swap(t1.ftraj, t2.ftraj);
254  swap(t1.gp_pos_prior, t2.gp_pos_prior);
255  swap(t1.gp_dir_prior, t2.gp_dir_prior);
256  swap(t1.gp_fdir_prior, t2.gp_fdir_prior);
257  swap(t1.hessian_, t2.hessian_);
258 
261  swap(m1, m2);
262  }
263 
264  //friend
265  //void update_scene_state(const Scene& scene, const Facemark_data& fmdata);
266 
267 private:
268  void update_gp(Gpp& gpp, double sc, double sv) const;
269 
271  // get_XXX() are helper functions used to get a generic smoothing //
273 
274  boost::optional<double> get_position(size_t fr, size_t dim) const
275  {
276  if(traj[fr - 1]) return traj[fr - 1]->value.position[dim];
277  return boost::none;
278  }
279 
280  boost::optional<double> get_bdir(size_t fr) const
281  {
282  if(traj[fr - 1]) return traj[fr - 1]->value.body_dir;
283  return boost::none;
284  }
285 
286  boost::optional<double> get_fdir(size_t fr) const
287  {
288  if(traj[fr - 1]) return traj[fr - 1]->value.face_dir[0];
289  return boost::none;
290  }
291 
292  double get_bdir_nc(size_t fr) const
293  {
294  return traj[fr - 1]->value.body_dir;
295  }
296 
297  double get_zero(size_t) const
298  {
299  return 0.0;
300  }
301 
302 private:
303  mutable Trajectory traj;
304  mutable Body_2d_trajectory btraj;
305  mutable Face_2d_trajectory ftraj;
306  mutable Gpp gp_pos_prior;
307  mutable Gpp gp_dir_prior;
308  mutable Gpp gp_fdir_prior;
309 
310  // I HATE THIS BUT IT'S NECESSARY!!
311  mutable Vector hessian_;
312 };
313 
315 inline
317 {
318  return Vector().set(cs.position[0], cs.position[2]);
319 }
320 
322 inline
324 {
325  return Vector().set(cs.face_dir[0], cs.face_dir[1]);
326 }
327 
328 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
329 
331 void sync_state(const Target& tg);
332 
333 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
334 
335 template<class GetTraj, class GetMean>
336 Vector Target::smooth_trajectory
337 (
338  size_t sf,
339  size_t ef,
340  const std::vector<double>& nvar,
341  double scale,
342  double svar,
343  const GetTraj& data,
344  const GetMean& mean
345 ) const
346 {
347  typedef gp::Predictive<
348  gp::Real_function_adapter<size_t>,
349  gp::Sqex,
350  gp::Linear_gaussian> Gppred;
351 
352  IFT(nvar.size() <= ef - sf + 1, Illegal_argument,
353  "Cannot smooth trajectory; wrong number of variances.");
354 
355  // fill buffer variances with 0.0, to ensure smoothness at the edges
356  size_t wsf = sf - get_start_time() > scale ? sf - scale : get_start_time();
357  size_t wef = std::min(get_end_time(), static_cast<int>(ef + scale));
358 
359  size_t nssz = nvar.size() + (sf - wsf) + (wef - ef);
360  Vector ns(static_cast<int>(nssz), 0.0);
361  assert(ns.size() >= nvar.size());
362  std::copy(nvar.begin(), nvar.end(), ns.begin() + sf - wsf);
363 
364  // fill in data
365  gp::Inputs trins; trins.reserve(ns.size());
366  Vector trouts; trouts.reserve(ns.size());
367  for(size_t t = wsf; t <= wef; ++t)
368  {
369  boost::optional<double> dp = data(t);
370 
371  if(dp)
372  {
373  trins.push_back(Vector().set(t));
374  trouts.push_back(*dp);
375  }
376  }
377 
378  IFT(ns.size() == trins.size(), Illegal_argument,
379  "Cannot smooth trajectory; wrong number of variances.");
380 
381  // and test inputs and GP posterior
382  gp::Inputs teins = gp::make_inputs(sf, ef);
383  Gppred gp_pred = make_predictive(
384  gp::Real_function_adapter<size_t>(mean),
385  gp::Sqex(scale, svar),
387  trins, trouts, teins);
388 
389  // return values
390  return gp_pred.normal().get_mean();
391 }
392 
393 }} //namespace kjb::pt
394 
395 #endif /*PT_TARGET_H_ */
396 
Generic_trajectory< Body_2d > Body_2d_trajectory
Definition: pt_body_2d_trajectory.h:36
Class that represents a detection bounding box from any source.
Definition: pt_detection_box.h:42
int get_end_time() const
Gets end time of track.
Definition: mcmcda_track.h:102
Definition of various standard probability distributions.
Vector get_cs_position(const Complete_state &cs)
Helper function – gets the position from CS as a kjb::Vector.
Definition: pt_target.h:316
Parent::const_iterator const_iterator
Definition: mcmcda_track.h:81
const Gpp & pos_prior() const
Returns the prior distribution used to evaluate this target.
Definition: pt_target.h:103
void set_changed_start(int f) const
Set the changed flag of this track.
Definition: mcmcda_track.h:208
Target(double height, double width, double girth, size_t vlen)
Construct a target with the given height.
Definition: pt_target.h:59
Trajectory & trajectory() const
Get this target's current 3D positions.
Definition: pt_target.h:85
void update_pos_gp(double sc, double sv) const
Updates internal distribution and caching mechanism.
Definition: pt_target.h:213
Int_matrix create_diagonal_matrix(const Int_matrix::Vec_type &diagonal)
Construct a one-row matrix by deep-copying a vector.
Definition: l_int_matrix.cpp:118
void refine_trajectory(const Perspective_camera &cam) const
Refine trajectory estimate using facemark information.
Definition: pt_target.cpp:328
void push_back(Value_type x)
inserts an element at the back of the vector in amortized constant time.
Definition: m_vector.h:1128
const Gpp & dir_prior() const
Returns the prior distribution used to evaluate this target.
Definition: pt_target.h:109
height
Definition: APPgetLargeConnectedEdges.m:33
int get_start_time() const
Gets start time of track.
Definition: mcmcda_track.h:93
Face_2d_trajectory & face_trajectory() const
Get this target's current 2D body trajectory.
Definition: pt_target.h:97
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
A class that represents a generic MCMCDA track.
Definition: mcmcda_track.h:40
Value_type mean(Iterator begin, Iterator end, const Value_type &)
Definition: prob_stat.h:56
#define IFT(a, ex, msg)
Definition: l_exception.h:101
void update_fdir_gp(double sc, double sv) const
Updates internal distribution and caching mechanism.
Definition: pt_target.h:225
Vector3 position
Definition: pt_complete_state.h:44
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
Vector2 face_dir
Definition: pt_complete_state.h:46
Vector & hessian_diagonal() const
Updates internal distribution and caching mechanism.
Definition: pt_target.h:237
void estimate_trajectory(const Perspective_camera &cam, double nsx=0.01, double nsz=1.0) const
Fix trajectory to match track.
Definition: pt_target.cpp:48
const Gpp & fdir_prior() const
Returns the prior distribution used to evaluate this target.
Definition: pt_target.h:115
int changed_start() const
Returns changed start flag.
Definition: mcmcda_track.h:196
friend void swap(Target &t1, Target &t2)
Efficiently swap two targets.
Definition: pt_target.h:247
void set_unchanged() const
Set changed flags to unchanged.
Definition: pt_target.h:122
Definition: perspective_camera.h:93
Vector & set(Value_type val)
Clone of zero_out(int)
Definition: m_vector.h:707
void reserve(int capacity)
Definition: m_vector.h:618
Class that represents a target moving through space.
Definition: pt_target.h:50
Body_2d_trajectory & body_trajectory() const
Get this target's current 2D body trajectory.
Definition: pt_target.h:91
void sync_state(const Target &tg)
Sync targets trajectory with endpoints.
Definition: pt_target.cpp:624
Generic_trajectory< Complete_state > Trajectory
Definition: pt_complete_trajectory.h:42
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Detection_box Element
Definition: pt_target.h:56
void set_changed_end(int f) const
Set the changed flag of this track.
Definition: mcmcda_track.h:214
#define M_PI
Definition: fft.cpp:206
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
void update_boxes(const Perspective_camera &cam) const
Update box trajectory.
Definition: pt_target.cpp:503
void set_changed_all() const
Set changed flags to unchanged.
Definition: pt_target.h:129
Vector get_cs_face_dir(const Complete_state &cs)
Helper function – gets the face_dir from CS as a kjb::Vector.
Definition: pt_target.h:323
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
void estimate_height(const Perspective_camera &cam) const
Estimate height from boxes and camera.
Definition: pt_target.cpp:287
void update_faces(const Perspective_camera &cam) const
Update face trajectory.
Definition: pt_target.cpp:551
Generic_trajectory< Face_2d > Face_2d_trajectory
Definition: pt_face_2d_trajectory.h:36
void scale(kjb::Axis_aligned_rectangle_2d &box, const kjb::Vector &s)
Definition: gr_2D_bounding_box.cpp:108
bool changed() const
Return true if this target has changed.
Definition: pt_target.h:136
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Represents the state of an actor at a frame.
Definition: pt_complete_state.h:42
void estimate_directions(bool infer_head=true, double nsb=0.1, double nsf=0.1) const
Estimate directions.
Definition: pt_target.cpp:406
void update_dir_gp(double sc, double sv) const
Updates internal distribution and caching mechanism.
Definition: pt_target.h:219
int changed_end() const
Returns changed end flag.
Definition: mcmcda_track.h:202