Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef ANGLE_BGSUB_STATE_H
00017 #define ANGLE_BGSUB_STATE_H
00018
00019 #include "skeleton_state.h"
00020 #include "options.h"
00021 #include "simulator.h"
00022
00039 template <class observation_type>
00040 class angle_bgsub_state : public skeleton_state<observation_type>
00041 {
00042 public:
00043 angle_bgsub_state (const options &opts, const calibration &_calib, const bool _for_show = true)
00044 : skeleton_state<observation_type> (opts, _calib, _for_show),
00045 pred_noise (opts.pred_noise ()),
00046 noise (0, pred_noise),
00047 extrapolate (opts.extrapolate ())
00048 {
00049 OpenTissue::kinematics::inverse::get_joint_parameters (this->skeleton, prev_theta);
00050 for (size_t k = 0; k < this->theta.size (); k++)
00051 this->prev_theta [k] = this->theta [k];
00052 };
00053
00054 double predict (const observation_type &observation, const double variance_scale = 1.0)
00055 {
00056 const size_t max_rejections = 10000;
00057 const project2d &proj2d = observation.get_project2d ();
00058 const size_t theta_size = this->theta.size ();
00059
00060 num_rejections = 0;
00061
00062 for (size_t attempt = 0; attempt < max_rejections; attempt++)
00063 {
00064 for (size_t idx = 0; idx < theta_size; idx++)
00065 {
00066 const double v = (idx == 27 || idx == 37) ? 250 : 500;
00067 von_mises hack_noise (0, v);
00068
00069 const double min_limit = this->get_min_limit (idx);
00070 const double max_limit = this->get_max_limit (idx);
00071
00072
00073 double new_det_val;
00074 if (extrapolate)
00075 {
00076 new_det_val = 2.0 * this->theta [idx] - this->prev_theta [idx];
00077 new_det_val = force_in_range (new_det_val, min_limit, max_limit);
00078 }
00079 else
00080 {
00081 new_det_val = this->theta [idx];
00082 }
00083
00084
00085
00086 double new_rand_val;
00087 do
00088 {
00089 new_rand_val = new_det_val + hack_noise.random ();
00090 }
00091 while (new_rand_val > max_limit || new_rand_val < min_limit);
00092
00093
00094 this->prev_theta [idx] = this->theta [idx];
00095 this->theta [idx] = new_rand_val;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 this->compute_pose ();
00107 bool accept = true;
00108 for (bone_iter iter = this->bone_begin (); iter != this->bone_end (); iter++)
00109 {
00110 const vector3 bone_pos = iter->absolute ().T ();
00111 const CvPoint bone_pos2d = this->calib.world_to_pixel (bone_pos, PROJ2D_SCALE);
00112 if (!proj2d.is_inside (bone_pos2d))
00113 {
00114 accept = false;
00115 break;
00116 }
00117 }
00118
00119 if (accept)
00120 break;
00121
00122 num_rejections ++;
00123 }
00124
00125 return 0;
00126 };
00127
00128 angle_bgsub_state& operator= (angle_bgsub_state &new_state)
00129 {
00130 skeleton_state<observation_type>::operator= (new_state);
00131
00132 for (size_t k = 0; k < this->prev_theta.size (); k++)
00133 this->prev_theta [k] = new_state.prev_theta [k];
00134
00135 return *this;
00136 };
00137
00138 bool load (const std::string filename)
00139 {
00140 const bool success = skeleton_state<observation_type>::load (filename);
00141
00142 for (size_t k = 0; k < this->theta.size (); k++)
00143 this->prev_theta [k] = this->theta [k];
00144
00145 return success;
00146 };
00147
00148 bool load (std::ifstream &file)
00149 {
00150 const bool success = skeleton_state<observation_type>::load (file);
00151
00152 for (size_t k = 0; k < this->theta.size (); k++)
00153 this->prev_theta [k] = this->theta [k];
00154
00155 return success;
00156 };
00157
00158 void write_to_log (std::ofstream &file) const
00159 {
00160 file << num_rejections << std::endl;
00161 }
00162
00163 protected:
00164 solver_type::vector_type prev_theta;
00165 const double pred_noise;
00166 const von_mises noise;
00167 const bool extrapolate;
00168 size_t num_rejections;
00169 };
00170
00171 #endif // ANGLE_BGSUB_STATE_H