object_tracker_25d.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2012, Georgia Institute of Technology
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Georgia Institute of Technology nor the names of
00018  *     its contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
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 // PCL
00043 #include <pcl16/point_cloud.h>
00044 #include <pcl16/point_types.h>
00045 
00046 // OpenCV
00047 #include <opencv2/core/core.hpp>
00048 
00049 // Boost
00050 #include <boost/shared_ptr.hpp>
00051 
00052 // tabletop_pushing
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 // STL
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   // Getters & Setters
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   // I/O Functions
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


tabletop_pushing
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:59:44