00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 #ifndef object_tracker_25d_h_DEFINED
00035 #define object_tracker_25d_h_DEFINED 1
00036 
00037 #include <ros/ros.h>
00038 
00039 #include <std_msgs/Header.h>
00040 #include <geometry_msgs/PoseStamped.h>
00041 
00042 
00043 #include <pcl16/point_cloud.h>
00044 #include <pcl16/point_types.h>
00045 
00046 
00047 #include <opencv2/core/core.hpp>
00048 
00049 
00050 #include <boost/shared_ptr.hpp>
00051 
00052 
00053 #include <tabletop_pushing/VisFeedbackPushTrackingAction.h>
00054 #include <tabletop_pushing/point_cloud_segmentation.h>
00055 #include <tabletop_pushing/arm_obj_segmentation.h>
00056 #include <tabletop_pushing/extern/gmm/gmm.h>
00057 
00058 
00059 #include <string>
00060 
00061 namespace tabletop_pushing
00062 {
00063 class ObjectTracker25D
00064 {
00065  public:
00066   ObjectTracker25D(boost::shared_ptr<PointCloudSegmentation> segmenter,
00067                    boost::shared_ptr<ArmObjSegmentation> arm_segmenter,
00068                    int num_downsamples = 0,
00069                    bool use_displays=false, bool write_to_disk=false,
00070                    std::string base_output_path="", std::string camera_frame="",
00071                    bool use_cv_ellipse = false, bool use_mps_segmentation=false, bool use_graphcut_arm_seg_=false,
00072                    double hull_alpha=0.01);
00073 
00074   ProtoObject findTargetObject(cv::Mat& in_frame, pcl16::PointCloud<pcl16::PointXYZ>& cloud,
00075                                bool& no_objects, bool init=false);
00076 
00077   ProtoObject findTargetObjectGC(cv::Mat& in_frame, XYZPointCloud& cloud, cv::Mat& depth_frame,
00078                                  cv::Mat self_mask, bool& no_objects, bool init=false);
00079 
00080   void computeState(ProtoObject& cur_obj, pcl16::PointCloud<pcl16::PointXYZ>& cloud,
00081                     std::string proxy_name, cv::Mat& in_frame,
00082                     tabletop_pushing::VisFeedbackPushTrackingFeedback& state, bool init_state=false);
00083 
00084   void fitObjectEllipse(ProtoObject& obj, cv::RotatedRect& ellipse);
00085 
00086   void fitHullEllipse(XYZPointCloud& hull_cloud, cv::RotatedRect& obj_ellipse);
00087 
00088   void findFootprintEllipse(ProtoObject& obj, cv::RotatedRect& ellipse);
00089 
00090   void findFootprintBox(ProtoObject& obj, cv::RotatedRect& ellipse);
00091 
00092   void fit2DMassEllipse(ProtoObject& obj, cv::RotatedRect& ellipse);
00093 
00094   void initTracks(cv::Mat& in_frame, cv::Mat& depth_frame, cv::Mat& self_mask,
00095                   pcl16::PointCloud<pcl16::PointXYZ>& cloud, std::string proxy_name,
00096                   tabletop_pushing::VisFeedbackPushTrackingFeedback& state, bool start_swap=false);
00097 
00098   double getThetaFromEllipse(cv::RotatedRect& obj_ellipse);
00099 
00100   void updateTracks(cv::Mat& in_frame, cv::Mat& depth_frame, cv::Mat& self_mask,
00101                     pcl16::PointCloud<pcl16::PointXYZ>& cloud, std::string proxy_name,
00102                     tabletop_pushing::VisFeedbackPushTrackingFeedback& state);
00103 
00104   void pausedUpdate(cv::Mat in_frame);
00105 
00106   
00107   
00108   
00109   bool isInitialized() const
00110   {
00111     return initialized_;
00112   }
00113 
00114   void stopTracking()
00115   {
00116     initialized_ = false;
00117     paused_ = false;
00118   }
00119 
00120   void setNumDownsamples(int num_downsamples)
00121   {
00122     num_downsamples_ = num_downsamples;
00123     upscale_ = std::pow(2,num_downsamples_);
00124   }
00125 
00126   tabletop_pushing::VisFeedbackPushTrackingFeedback getMostRecentState() const
00127   {
00128     return previous_state_;
00129   }
00130 
00131   ProtoObject getMostRecentObject() const
00132   {
00133     return previous_obj_;
00134   }
00135 
00136   cv::RotatedRect getMostRecentEllipse() const
00137   {
00138     return previous_obj_ellipse_;
00139   }
00140 
00141   void pause()
00142   {
00143     paused_ = true;
00144   }
00145 
00146   void unpause()
00147   {
00148     paused_ = false;
00149   }
00150 
00151   bool isPaused() const
00152   {
00153     return paused_;
00154   }
00155 
00156   bool getSwapState() const
00157   {
00158     return swap_orientation_;
00159   }
00160 
00161   void toggleSwap()
00162   {
00163     swap_orientation_ = !swap_orientation_;
00164   }
00165 
00166   
00167   
00168   
00169 
00170   void trackerDisplay(cv::Mat& in_frame, ProtoObject& cur_obj, cv::RotatedRect& obj_ellipse);
00171 
00172   void trackerBoxDisplay(cv::Mat& in_frame, ProtoObject& cur_obj, cv::RotatedRect& obj_ellipse);
00173 
00174   void trackerDisplay(cv::Mat& in_frame, tabletop_pushing::VisFeedbackPushTrackingFeedback& state, ProtoObject& obj,
00175                       bool other_color=false);
00176 
00177  protected:
00178   void updateHeading(tabletop_pushing::VisFeedbackPushTrackingFeedback& state, bool init_state);
00179   ProtoObject matchToTargetObject(ProtoObjects& objects, cv::Mat& in_frame, bool init=false);
00180   cv::Mat getTableMask(XYZPointCloud& cloud, XYZPointCloud& table_cloud, cv::Size mask_size,
00181                        XYZPointCloud& obj_cloud);
00182   GMM buildColorModel(XYZPointCloud& cloud, cv::Mat& frame, int num_clusters);
00183   boost::shared_ptr<PointCloudSegmentation> pcl_segmenter_;
00184   boost::shared_ptr<ArmObjSegmentation> arm_segmenter_;
00185   int num_downsamples_;
00186   bool initialized_;
00187   int frame_count_;
00188   int upscale_;
00189   double previous_time_;
00190   ProtoObject previous_obj_;
00191   tabletop_pushing::VisFeedbackPushTrackingFeedback previous_state_;
00192   tabletop_pushing::VisFeedbackPushTrackingFeedback init_state_;
00193   cv::RotatedRect previous_obj_ellipse_;
00194   bool use_displays_;
00195   bool write_to_disk_;
00196   std::string base_output_path_;
00197   int record_count_;
00198   bool swap_orientation_;
00199   bool paused_;
00200   int frame_set_count_;
00201   std::string camera_frame_;
00202   bool use_cv_ellipse_fit_;
00203   bool use_mps_segmentation_;
00204   bool obj_saved_;
00205   GMM obj_color_model_;
00206   GMM table_color_model_;
00207   bool have_obj_color_model_;
00208   bool have_table_color_model_;
00209   bool use_graphcut_arm_seg_;
00210   double hull_alpha_;
00211   XYZPointCloud previous_hull_cloud_;
00212 };
00213 };
00214 #endif // object_tracker_25d_h_DEFINED