|
| 1 | +/************************************************************************* |
| 2 | +This software allows for filtering in high-dimensional observation and |
| 3 | +state spaces, as described in |
| 4 | +
|
| 5 | +M. Wuthrich, P. Pastor, M. Kalakrishnan, J. Bohg, and S. Schaal. |
| 6 | +Probabilistic Object Tracking using a Range Camera |
| 7 | +IEEE/RSJ Intl Conf on Intelligent Robots and Systems, 2013 |
| 8 | +
|
| 9 | +In a publication based on this software pleace cite the above reference. |
| 10 | +
|
| 11 | +
|
| 12 | +Copyright (C) 2014 Manuel Wuthrich |
| 13 | +
|
| 14 | +This program is free software: you can redistribute it and/or modify |
| 15 | +it under the terms of the GNU General Public License as published by |
| 16 | +the Free Software Foundation, either version 3 of the License, or |
| 17 | +(at your option) any later version. |
| 18 | +
|
| 19 | +This program is distributed in the hope that it will be useful, |
| 20 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +GNU General Public License for more details. |
| 23 | +
|
| 24 | +You should have received a copy of the GNU General Public License |
| 25 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 | +*************************************************************************/ |
| 27 | + |
| 28 | +#ifndef POSE_TRACKING_MODELS_OBSERVATION_MODELS_APPROXIMATE_KINECT_PIXEL_OBSERVATION_MODEL_HPP |
| 29 | +#define POSE_TRACKING_MODELS_OBSERVATION_MODELS_APPROXIMATE_KINECT_PIXEL_OBSERVATION_MODEL_HPP |
| 30 | + |
| 31 | +#include <cmath> |
| 32 | +#include <tuple> |
| 33 | + |
| 34 | +#include <Eigen/Dense> |
| 35 | + |
| 36 | +#include <fast_filtering/distributions/interfaces/evaluation.hpp> |
| 37 | +#include <fast_filtering/distributions/exponential_distribution.hpp> |
| 38 | +#include <fast_filtering/distributions/uniform_distribution.hpp> |
| 39 | +#include <fast_filtering/distributions/truncated_gaussian.hpp> |
| 40 | +#include <fast_filtering/utils/helper_functions.hpp> |
| 41 | + |
| 42 | +#include <pose_tracking/models/observation_models/kinect_pixel_observation_model.hpp> |
| 43 | +#include <pose_tracking/utils/rigid_body_renderer.hpp> |
| 44 | +#include <pose_tracking/utils/hash_mapping.hpp> |
| 45 | + |
| 46 | +#include <boost/unordered_map.hpp> |
| 47 | + |
| 48 | +namespace ff |
| 49 | +{ |
| 50 | + |
| 51 | +/** |
| 52 | + * \class KinectObservationModel |
| 53 | + * |
| 54 | + * \ingroup distributions |
| 55 | + * \ingroup observation_models |
| 56 | + */ |
| 57 | +template <typename State> |
| 58 | +class ApproximateKinectPixelObservationModel: |
| 59 | + public GaussianMap<double, Eigen::Matrix<double, 1, 1> >, |
| 60 | + public Evaluation<double, double> |
| 61 | +{ |
| 62 | +public: |
| 63 | + typedef Eigen::Matrix<double, 1, 1> Noise; |
| 64 | + typedef double Scalar; |
| 65 | + typedef double Observation; |
| 66 | + |
| 67 | + typedef boost::shared_ptr<RigidBodyRenderer> ObjectRendererPtr; |
| 68 | + |
| 69 | + ApproximateKinectPixelObservationModel( |
| 70 | + const ObjectRendererPtr object_renderer, |
| 71 | + const Eigen::Matrix3d& camera_matrix, |
| 72 | + const size_t n_rows, |
| 73 | + const size_t n_cols, |
| 74 | + const Scalar sensor_failure_probability = 0.01, |
| 75 | + const Scalar object_model_sigma = 0.003, |
| 76 | + const Scalar sigma_factor = 0.00142478, |
| 77 | + const Scalar half_life_depth = 1.0, |
| 78 | + const Scalar max_depth = 6.0, |
| 79 | + const Scalar min_depth = 0.0, |
| 80 | + const Scalar approximation_depth = 1.5, |
| 81 | + const size_t depth_count = 10000, |
| 82 | + const size_t occlusion_count = 100) |
| 83 | + : object_renderer_(object_renderer), |
| 84 | + camera_matrix_(camera_matrix), |
| 85 | + n_rows_(n_rows), |
| 86 | + n_cols_(n_cols), |
| 87 | + predictions_(100), // the size of this vector reflects the number of different |
| 88 | + occluded_observation_model_(sensor_failure_probability, |
| 89 | + object_model_sigma, |
| 90 | + sigma_factor, |
| 91 | + half_life_depth, |
| 92 | + max_depth), |
| 93 | + visible_observation_model_(occluded_observation_model_), |
| 94 | + exponential_distribution_(-log(0.5)/half_life_depth, min_depth), |
| 95 | + object_model_noise_(0.0, object_model_sigma), |
| 96 | + sensor_failure_distribution_(min_depth, max_depth), |
| 97 | + sensor_failure_probability_(sensor_failure_probability), |
| 98 | + sigma_factor_(sigma_factor), |
| 99 | + max_depth_(max_depth), |
| 100 | + min_depth_(min_depth), |
| 101 | + approximation_depth_(approximation_depth), |
| 102 | + occlusion_step_(1.0 / double(occlusion_count - 1)), |
| 103 | + depth_step_((max_depth_ - min_depth_) / double(depth_count - 1)) |
| 104 | + { |
| 105 | + for(size_t occlusion_index = 0; occlusion_index < occlusion_count; occlusion_index++) |
| 106 | + { |
| 107 | + std::vector<double> log_probs(depth_count); |
| 108 | + for(size_t depth_index = 0; depth_index < depth_count; depth_index++) |
| 109 | + { |
| 110 | + Condition(approximation_depth_, |
| 111 | + hf::Logit(double(occlusion_index) * occlusion_step_)); |
| 112 | + log_probs[depth_index] = LogProbability( |
| 113 | + min_depth_ + double(depth_index) * depth_step_); |
| 114 | + } |
| 115 | + samplers_.push_back(hf::DiscreteDistribution(log_probs)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + virtual ~ApproximateKinectPixelObservationModel() {} |
| 121 | + |
| 122 | + virtual Observation MapStandardGaussian(const Noise& sample) const |
| 123 | + { |
| 124 | + int depth_index = samplers_[occlusion_index_].MapStandardGaussian(sample(0)); |
| 125 | + |
| 126 | + return rendered_depth_ - approximation_depth_ + min_depth_ |
| 127 | + + depth_step_ * double(depth_index); |
| 128 | + } |
| 129 | + |
| 130 | + virtual Scalar Probability(const Observation& observation) const |
| 131 | + { |
| 132 | + Scalar probability_given_occluded = |
| 133 | + occluded_observation_model_.Probability(observation); |
| 134 | + |
| 135 | + Scalar probability_given_visible = |
| 136 | + visible_observation_model_.Probability(observation); |
| 137 | + |
| 138 | + return probability_given_occluded * occlusion_probability_ + |
| 139 | + probability_given_visible * (1.0 - occlusion_probability_); |
| 140 | + } |
| 141 | + |
| 142 | + virtual Scalar LogProbability(const Observation& observation) const |
| 143 | + { |
| 144 | + return std::log(Probability(observation)); |
| 145 | + } |
| 146 | + |
| 147 | + virtual void ResetCache() |
| 148 | + { |
| 149 | + predictions_.clear(); |
| 150 | + } |
| 151 | + |
| 152 | + virtual void Condition(const State& state, |
| 153 | + const Scalar& occlusion, |
| 154 | + size_t index) |
| 155 | + { |
| 156 | + |
| 157 | + if (predictions_.find(state) == predictions_.end()) |
| 158 | + { |
| 159 | + object_renderer_->state(state); |
| 160 | + object_renderer_->Render(camera_matrix_, |
| 161 | + n_rows_, |
| 162 | + n_cols_, |
| 163 | + predictions_[state]); |
| 164 | + } |
| 165 | + |
| 166 | + Condition(predictions_[state][index], occlusion); |
| 167 | + } |
| 168 | + |
| 169 | + virtual void Condition(const Scalar& rendered_depth, |
| 170 | + const Scalar& occlusion) |
| 171 | + { |
| 172 | + rendered_depth_ = rendered_depth; |
| 173 | + occlusion_probability_ = hf::Sigmoid(occlusion); |
| 174 | + occlusion_index_ = occlusion_probability_ / occlusion_step_; |
| 175 | + occluded_observation_model_.Condition(rendered_depth, true); |
| 176 | + visible_observation_model_.Condition(rendered_depth, false); |
| 177 | + } |
| 178 | + |
| 179 | +private: |
| 180 | + ObjectRendererPtr object_renderer_; |
| 181 | + |
| 182 | + Eigen::Matrix3d camera_matrix_; |
| 183 | + size_t n_rows_; |
| 184 | + size_t n_cols_; |
| 185 | + |
| 186 | + /** |
| 187 | + * Harbors pairs of (intersect_indices, image_prediction). |
| 188 | + */ |
| 189 | + boost::unordered_map<Eigen::MatrixXd, std::vector<float> > predictions_; |
| 190 | + |
| 191 | + KinectPixelObservationModel occluded_observation_model_; |
| 192 | + KinectPixelObservationModel visible_observation_model_; |
| 193 | + |
| 194 | + Scalar rendered_depth_; |
| 195 | + Scalar occlusion_probability_; |
| 196 | + |
| 197 | + TruncatedGaussian object_model_noise_; |
| 198 | + UniformDistribution uniform_distribution_; |
| 199 | + ExponentialDistribution exponential_distribution_; |
| 200 | + UniformDistribution sensor_failure_distribution_; |
| 201 | + |
| 202 | + // parameters |
| 203 | + const Scalar sensor_failure_probability_, sigma_factor_, |
| 204 | + max_depth_, min_depth_, approximation_depth_; |
| 205 | + |
| 206 | + |
| 207 | + std::vector<hf::DiscreteDistribution> samplers_; |
| 208 | + |
| 209 | + double occlusion_step_; |
| 210 | + double depth_step_; |
| 211 | + int occlusion_index_; |
| 212 | +}; |
| 213 | + |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +#endif |
0 commit comments