Go to the documentation of this file.00001
00063 #ifndef __DEPTH_SEGMENTATION_H__
00064 #define __DEPTH_SEGMENTATION_H__
00065
00066 #include <math.h>
00067 #include <queue>
00068
00069 #include <ros/console.h>
00070
00071 #include <pcl/point_cloud.h>
00072 #include <pcl/point_types.h>
00073
00074 #include "cob_3d_mapping_common/point_types.h"
00075 #include "cob_3d_segmentation/general_segmentation.h"
00076 #include "cob_3d_segmentation/cluster_graph_structure.h"
00077
00078 namespace cob_3d_segmentation
00079 {
00080 struct SegmentationCandidate
00081 {
00082 typedef boost::shared_ptr<SegmentationCandidate> Ptr;
00083 SegmentationCandidate(int u_in, int v_in, float angle_in) : u(u_in), v(v_in), dot_value(angle_in) { }
00084 int u;
00085 int v;
00086 float dot_value;
00087 };
00088
00089 inline const bool operator< (const SegmentationCandidate& lhs, const SegmentationCandidate& rhs){return lhs.dot_value < rhs.dot_value;}
00090 inline const bool operator> (const SegmentationCandidate& lhs, const SegmentationCandidate& rhs){return operator< (rhs, lhs);}
00091 inline const bool operator<=(const SegmentationCandidate& lhs, const SegmentationCandidate& rhs){return !operator> (lhs, rhs);}
00092 inline const bool operator>=(const SegmentationCandidate& lhs, const SegmentationCandidate& rhs){return !operator< (lhs, rhs);}
00093
00094 struct ptr_deref
00095 {
00096 template<typename T>
00097 bool operator() (const boost::shared_ptr<T>& lhs, const boost::shared_ptr<T>& rhs) const { return operator< (*lhs, *rhs); }
00098 };
00099
00100 typedef std::priority_queue<SegmentationCandidate::Ptr, std::vector<SegmentationCandidate::Ptr>, ptr_deref> SegmentationQueue;
00101
00102 struct PredefinedSegmentationTypes
00103 {
00104 typedef PointLabel Label;
00105 typedef pcl::PointXYZRGB Point;
00106 typedef pcl::Normal Normal;
00107 typedef DepthClusterHandler<Label,Point,Normal> CH;
00108 typedef BoundaryPointsEdgeHandler<Label,Point> EH;
00109 typedef ClusterGraphStructure<CH,EH> Graph;
00110 };
00111
00112
00113 template <typename ClusterGraphT, typename PointT, typename PointNT, typename PointLabelT>
00114 class DepthSegmentation : public GeneralSegmentation<PointT, PointLabelT>
00115 {
00116 public:
00117 typedef pcl::PointCloud<PointT> PointCloud;
00118 typedef typename PointCloud::Ptr PointCloudPtr;
00119 typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00120
00121 typedef pcl::PointCloud<PointNT> NormalCloud;
00122 typedef typename NormalCloud::Ptr NormalCloudPtr;
00123 typedef typename NormalCloud::ConstPtr NormalCloudConstPtr;
00124
00125 typedef pcl::PointCloud<PointLabelT> LabelCloud;
00126 typedef typename LabelCloud::Ptr LabelCloudPtr;
00127 typedef typename LabelCloud::ConstPtr LabelCloudConstPtr;
00128
00129 typedef typename ClusterGraphT::Ptr ClusterGraphPtr;
00130 typedef typename ClusterGraphT::ClusterPtr ClusterPtr;
00131 typedef typename ClusterGraphT::EdgePtr EdgePtr;
00132
00133 public:
00134 DepthSegmentation ()
00135
00136
00137
00138 : min_dot_normals_(35.0f / 180.0f * M_PI)
00139 , min_dot_boundary_(cos(50.0f / 180.0f * M_PI))
00140
00141 , min_cluster_size_(150)
00142 { };
00143
00144 ~DepthSegmentation ()
00145 { };
00146
00147 inline void setClusterGraphOut(ClusterGraphPtr clusters) { graph_ = clusters; }
00148
00149 inline void setNormalCloudIn(NormalCloudConstPtr normals) { normals_ = normals; }
00150 inline void setLabelCloudInOut(LabelCloudPtr labels) { labels_ = labels; }
00151 virtual void setInputCloud(const PointCloudConstPtr& points) { surface_ = points; }
00152 virtual LabelCloudConstPtr getOutputCloud() { return labels_; }
00153 virtual bool compute()
00154 {
00155 performInitialSegmentation();
00156 refineSegmentation();
00157 return true;
00158 }
00159
00160 void performInitialSegmentation();
00161
00162 void refineSegmentation();
00163
00164 void getPotentialObjects(std::map<int,int>& objs, int max_size = 0);
00165
00167 virtual operator cob_3d_mapping_msgs::ShapeArray() const {ROS_ERROR("TODO: do it"); return cob_3d_mapping_msgs::ShapeArray();}
00168
00169 private:
00170 void addIfIsValid(int u, int v, int idx, int idx_prev, float dist_th, float p_z, Eigen::Vector3f& n,
00171 SegmentationQueue& seg_queue, ClusterPtr c);
00172
00173 void computeBoundaryProperties(ClusterPtr c, EdgePtr e);
00174 void computeBoundaryProperties(ClusterPtr c);
00175 void computeEdgeSmoothness(EdgePtr e);
00176 void computeEdgeSmoothness();
00177 void addSmallNeighbors(ClusterPtr c, std::map<int,int>& objs, std::set<int>& processed, int obj_counter, int max_size);
00178
00179
00180
00181 float min_dot_normals_;
00182 float min_dot_boundary_;
00183 int min_cluster_size_;
00184
00185 ClusterGraphPtr graph_;
00186 PointCloudConstPtr surface_;
00187 NormalCloudConstPtr normals_;
00188 LabelCloudPtr labels_;
00189
00190 };
00191 }
00192
00193 #endif