tracking.h
Go to the documentation of this file.
00001 #ifndef __TRACKING_H__
00002 #define __TRACKING_H__
00003 #include <boost/accumulators/accumulators.hpp>
00004 #include <boost/accumulators/statistics.hpp>
00005 #include <boost/accumulators/statistics/median.hpp>
00006 #include <boost/accumulators/statistics/p_square_quantile.hpp>
00007 
00008 #include <iostream>
00009 // back-end
00010 #include <boost/msm/back/state_machine.hpp>
00011 //front-end
00012 #include <boost/msm/front/state_machine_def.hpp>
00013 #include <boost/array.hpp>
00014 #include <visp/vpImage.h>
00015 
00016 
00017 #include <visp/vpImage.h>
00018 #include <visp/vpRGBa.h>
00019 #include <visp/vpHomogeneousMatrix.h>
00020 #include <visp/vpCameraParameters.h>
00021 
00022 #include <visp/vpDisplay.h>
00023 #include <visp/vpHinkley.h>
00024 #include <visp/vpMe.h>
00025 #include <vector>
00026 #include <fstream>
00027 
00028 #include "visp_tracker/MovingEdgeSites.h"
00029 #include "visp_tracker/KltPoints.h"
00030 
00031 #include "cmd_line/cmd_line.h"
00032 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
00033 #include "detectors/detector_base.h"
00034 #else
00035 #  include <visp/vpDetectorBase.h>
00036 #endif
00037 
00038 #include <visp/vpMbEdgeTracker.h>
00039 #include "states.hpp"
00040 #include "events.h"
00041 
00042 using namespace boost::accumulators;
00043 namespace msm = boost::msm;
00044 namespace mpl = boost::mpl;
00045 namespace tracking{
00046 
00047   class Tracker_ : public msm::front::state_machine_def<Tracker_>{
00048   public:
00049     typedef struct {
00050       boost::accumulators::accumulator_set<
00051         double,
00052         boost::accumulators::stats<
00053           boost::accumulators::tag::median(boost::accumulators::with_p_square_quantile),
00054           boost::accumulators::tag::max,
00055           boost::accumulators::tag::mean
00056         >
00057       > var,var_x,var_y,var_z,var_wx,var_wy,var_wz,checkpoints;
00058 
00059     } statistics_t;
00060   private:
00061     CmdLine cmd;
00062     int iter_;
00063     vpImagePoint flashcode_center_;
00064     std::ofstream varfile_;
00065 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
00066     detectors::DetectorBase* detector_;
00067 #else
00068     vpDetectorBase *detector_;
00069 #endif
00070     typedef boost::array<vpHinkley,6> hinkley_array_t;
00071     hinkley_array_t hink_;
00072 
00073     vpMbTracker* tracker_; // Create a model based tracker.
00074     vpMe tracker_me_config_;
00075     vpImage<vpRGBa> *I_;
00076     vpImage<vpRGBa> *_I;
00077     vpHomogeneousMatrix cMo_; // Pose computed using the tracker.
00078     vpMatrix covariance_; // Covariance associated to the pose estimation
00079     vpCameraParameters cam_;
00080     vpImage<unsigned char> Igray_;
00081 
00082     std::vector<vpPoint> outer_points_3D_bcp_;
00083     std::vector<vpPoint> points3D_inner_;
00084     std::vector<vpPoint> points3D_outer_;
00085     std::vector<vpPoint> points3D_middle_;
00086     std::vector<vpPoint> f_;
00087     vpRect vpTrackingBox_;
00088     cv::Rect cvTrackingBox_;
00089     bool cvTrackingBox_init_;
00090 
00091     statistics_t statistics;
00092     bool flush_display_;
00093 
00094   public:
00095     //getters to access useful members
00096     void set_flush_display(bool val);
00097     bool get_flush_display();
00098 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
00099     detectors::DetectorBase& get_detector();
00100 #else
00101     vpDetectorBase& get_detector();
00102 #endif
00103     vpMbTracker& get_mbt();
00104     std::vector<vpPoint>& get_points3D_inner();
00105     std::vector<vpPoint>& get_points3D_outer();
00106     std::vector<vpPoint>& get_points3D_middle();
00107     std::vector<vpPoint>& get_flashcode();
00108 
00109     //returns tracking box where to look for pattern (may be full image)
00110     template<class T>
00111     const T& get_tracking_box();
00112     //returns currently treated image
00113     vpImage<vpRGBa>& get_I();
00114     //returns camera parameters
00115     vpCameraParameters& get_cam();
00116     //returns tracker configuration
00117     CmdLine& get_cmd();
00118 
00119     //constructor
00120     //inits tracker from a detector, a visp tracker
00121 #if VISP_VERSION_INT < VP_VERSION_INT(2,10,0)
00122     Tracker_(CmdLine& cmd, detectors::DetectorBase* detector,vpMbTracker* tracker_,bool flush_display = true);
00123 #else
00124     Tracker_(CmdLine& cmd, vpDetectorBase* detector,vpMbTracker* tracker_,bool flush_display = true);
00125 #endif
00126     typedef WaitingForInput initial_state;      //initial state of our state machine tracker
00127 
00128     //Guards
00129     bool input_selected(input_ready const& evt);
00130     bool no_input_selected(input_ready const& evt);
00131     bool flashcode_detected(input_ready const& evt);
00132     bool flashcode_redetected(input_ready const& evt);
00133     bool model_detected(msm::front::none const&);
00134     bool mbt_success(input_ready const& evt);
00135 
00136     //actions
00137     void find_flashcode_pos(input_ready const& evt);
00138     void track_model(input_ready const& evt);
00139 
00140     //gets statistics about the last tracking experience
00141     statistics_t& get_statistics();
00142 
00143     void updateMovingEdgeSites(visp_tracker::MovingEdgeSitesPtr sites);
00144     void updateKltPoints(visp_tracker::KltPointsPtr klt);
00145 
00146     //here is how the tracker works
00147     struct transition_table : mpl::vector<
00148       //    Start               Event              Target                       Action                         Guard
00149       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00150       g_row< WaitingForInput  , input_ready        , WaitingForInput       ,                               &Tracker_::no_input_selected    >,
00151       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00152       g_row< WaitingForInput  , input_ready        , DetectFlashcode       ,                               &Tracker_::input_selected       >,
00153       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00154        _row< WaitingForInput  , select_input       , DetectFlashcode                                                                       >,
00155       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00156        _row< DetectFlashcode  , input_ready        , DetectFlashcode                                        /* default behaviour */        >,
00157       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00158         row< DetectFlashcode  , input_ready        , DetectModel           , &Tracker_::find_flashcode_pos,&Tracker_::flashcode_detected   >,
00159       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00160        _row< DetectModel      , msm::front::none   , DetectFlashcode                                          /* default behaviour */      >,
00161       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00162       g_row< DetectModel      , msm::front::none   , TrackModel            ,                               &Tracker_::model_detected       >,
00163       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00164        _row< TrackModel       , input_ready        , ReDetectFlashcode                                        /* default behaviour */      >,
00165       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00166         row< TrackModel       , input_ready        , TrackModel            , &Tracker_::track_model       ,&Tracker_::mbt_success          >,
00167       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00168        _row< ReDetectFlashcode, input_ready        , DetectFlashcode                                        /* default behaviour */        >,
00169       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00170         row< ReDetectFlashcode, input_ready        , DetectModel           , &Tracker_::find_flashcode_pos,&Tracker_::flashcode_redetected >,
00171       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00172       //row< ReDetectFlashcode, input_ready        , TrackModel            , &Tracker_::track_model       ,&Tracker_::mbt_success          >,
00173       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00174        _row< TrackModel       , finished           , Finished                                                                              >,
00175       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00176        _row< DetectModel      , finished           , Finished                                                                              >,
00177       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00178        _row< DetectFlashcode  , finished           , Finished                                                                              >,
00179       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00180        _row< ReDetectFlashcode, finished           , Finished                                                                              >
00181       //   +------------------+--------------------+-----------------------+------------------------------+------------------------------+
00182       > {};
00183 
00184   };
00185 
00186   typedef msm::back::state_machine<Tracker_> Tracker;
00187 
00188 
00189 }
00190 #endif //__TRACKING_H__


visp_auto_tracker
Author(s): Filip Novotny
autogenerated on Sun Feb 19 2017 03:28:53