00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef PF_SKELETON_TRACKER_H
00017 #define PF_SKELETON_TRACKER_H
00018
00019 #include <ctime>
00020
00021 #include "particle_filter.h"
00022 #include "angle_state.h"
00023 #include "pik_state.h"
00024 #include "tangent_state.h"
00025 #include "projection_state.h"
00026 #include "KBK_stick_state.h"
00027 #include "stick_state.h"
00028 #include "stick2d_state.h"
00029 #include "brownian_stick_state.h"
00030 #include "pik_stick_state.h"
00031 #include "brownian_state.h"
00032 #include "bgsub_state.h"
00033 #include "angle_bgsub_state.h"
00034 #include "hull_state.h"
00035 #include "point_cloud.h"
00036 #include "kinect_cloud.h"
00037 #include "triclops_cloud.h"
00038 #include "cloud_and_stick.h"
00039 #include "skeleton_cloud_measure.h"
00040 #include "skeleton_pose_estimator.h"
00041 #include "options.h"
00042 #include "mocap_data.h"
00043 #include "calibration.h"
00044
00045 #define TRACKER(state_type, observation_type) \
00046 pf_skeleton_tracker< state_type<observation_type>, observation_type >
00047
00048
00049 #define FILTER_TYPE bootstrap
00050
00054 template <class _state_type, class observation_type>
00055 class pf_skeleton_tracker : public FILTER_TYPE<_state_type, observation_type>
00056 {
00057 public:
00058 typedef _state_type state_type;
00059
00070 pf_skeleton_tracker (const options &opts, calibration &_calib, const bool _playback = false)
00071 : FILTER_TYPE<state_type, observation_type> (opts, _calib, _playback),
00072 calib (_calib),
00073 observation (opts, _calib),
00074 output_directory (opts.output_directory ()),
00075 mean_file (fullfile (output_directory, "mean_points.txt")),
00076 validation_file (fullfile (output_directory, "validation.txt")),
00077 playback (_playback),
00078 m (opts, _calib),
00079 meas (opts, _calib),
00080 mocap (opts, _calib)
00081 {
00082 mean_fid = fopen (mean_file.c_str (), "w");
00083 validation_fid = fopen (validation_file.c_str (), "w");
00084 if (!playback)
00085 state_log.open (fullfile (output_directory, "state_log.txt").c_str ());
00086
00087 srand (time (NULL));
00088
00089 observation.refresh (false);
00090 };
00091
00092 ~pf_skeleton_tracker ()
00093 {
00094 if (mean_fid != NULL)
00095 fclose (mean_fid);
00096
00097 if (validation_fid != NULL)
00098 fclose (validation_fid);
00099
00100 if (!playback)
00101 state_log.close ();
00102 }
00103
00110 bool refresh ()
00111 {
00112 bool valid = observation.refresh ();
00113 mocap.refresh ();
00114 if (valid)
00115 {
00116 if (playback)
00117 {
00118 valid = FILTER_TYPE<state_type, observation_type>::load_states ();
00119 }
00120 else
00121 {
00122 FILTER_TYPE<state_type, observation_type>::refresh (observation);
00123
00124 for (size_t k = 0; k < this->num_particles; k++)
00125 this->states [k]->write_to_log (state_log);
00126 }
00127 compute_mode ();
00128
00129
00130 if (validation_fid != NULL)
00131 {
00132 const double score = mocap.average_pose_dist (m);
00133 fprintf (validation_fid, "%lf\n", score);
00134 }
00135 }
00136
00137 return valid;
00138 };
00139
00144 calibration& get_calibration ()
00145 {
00146 return calib;
00147 };
00148
00152 void prepare_mesh ()
00153 {
00154 m.prepare_mesh ();
00155 }
00156
00160 void show () const
00161 {
00162 glPushMatrix ();
00163 glScalef (0.5, 0.5, 0.5);
00164 glTranslatef (0, -4, 0);
00165
00166
00167 for (size_t k = 0; k < this->num_particles; k++)
00168 this->states [k]->show ();
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 observation.show ();
00189 mocap.show ();
00190
00191 glPopMatrix ();
00192 };
00193
00198 const state_type& get_pose_estimate ()
00199 {
00200 return m;
00201 };
00202
00207 const observation_type& get_observation () const
00208 {
00209 return observation;
00210 }
00211
00212 protected:
00213 double measure_hypothesis (state_type &state, const observation_type &observation)
00214 {
00215 return meas.log_measure (state, observation);
00216 };
00217
00218 void compute_mode ()
00219 {
00220
00221
00222 mean< state_type > (this->weights, this->states, m);
00223
00224
00225
00226 m.compute_pose ();
00227
00228 if (mean_fid != NULL)
00229 {
00230 for (const_bone_iter iter = m.bone_begin (); iter != m.bone_end (); iter++)
00231 {
00232 const vector3 p = iter->absolute ().T ();
00233 fprintf (mean_fid, "%f %f %f ", p (0), p (1), p (2));
00234 }
00235 fprintf (mean_fid, "\n");
00236 }
00237 };
00238
00239 calibration &calib;
00240 observation_type observation;
00241 const std::string output_directory, mean_file, validation_file;
00242 const bool playback;
00243 state_type m;
00244 measure<state_type, observation_type> meas;
00245 FILE *mean_fid;
00246 mocap_data<observation_type> mocap;
00247 FILE *validation_fid;
00248 std::ofstream state_log;
00249 };
00250
00251 #endif // PF_SKELETON_TRACKER_H
00252