Go to the documentation of this file.00001
00063 #ifndef __FAST_SEGMENTATION_H__
00064 #define __FAST_SEGMENTATION_H__
00065
00066 #include <math.h>
00067
00068 #include <pcl/point_cloud.h>
00069 #include <pcl/point_types.h>
00070
00071 #include "cob_3d_mapping_common/point_types.h"
00072 #include "cob_3d_mapping_common/sensor_model.h"
00073 #include "cob_3d_segmentation/general_segmentation.h"
00074 #include "cob_3d_segmentation/cluster_handler.h"
00075 #include "cob_3d_segmentation/cluster_graph_structure.h"
00076
00077 namespace cob_3d_segmentation
00078 {
00079 namespace Options
00080 {
00081 struct ComputeGraph {};
00082 struct WithoutGraph {};
00083 }
00084
00085 enum SeedMethod
00086 {
00087 SEED_RANDOM,
00088 SEED_LINEAR
00089 };
00090
00091 struct SeedPoint
00092 {
00093 typedef boost::shared_ptr<SeedPoint> Ptr;
00094 SeedPoint(int idx_, int from_, float value_)
00095 : idx(idx_)
00096 , i_came_from(from_)
00097 , value(value_)
00098 { }
00099
00100 int idx;
00101 int i_came_from;
00102 float value;
00103 };
00104
00105 inline const bool operator< (const SeedPoint& lhs, const SeedPoint& rhs) {
00106 return lhs.value < rhs.value; }
00107
00108 inline const bool operator> (const SeedPoint& lhs, const SeedPoint& rhs) {
00109 return operator< (rhs, lhs); }
00110
00111 inline const bool operator<= (const SeedPoint& lhs, const SeedPoint& rhs) {
00112 return !operator> (lhs, rhs); }
00113
00114 inline const bool operator>= (const SeedPoint& lhs, const SeedPoint& rhs) {
00115 return !operator< (lhs, rhs); }
00116
00117 struct ptr_deref
00118 {
00119 template<typename T>
00120 bool operator() (const boost::shared_ptr<T>& lhs,
00121 const boost::shared_ptr<T>& rhs) const {
00122 return operator< (*lhs, *rhs); }
00123 };
00124
00125
00126 template<
00127 typename PointT,
00128 typename PointNT,
00129 typename PointLabelT,
00130 typename OptionsT = cob_3d_segmentation::Options::WithoutGraph,
00131 typename SensorT = cob_3d_mapping::PrimeSense,
00132 typename ClusterHdlT = cob_3d_segmentation::DepthClusterHandler<
00133 PointLabelT, PointT, PointNT>
00134 >
00135 class FastSegmentation : public GeneralSegmentation<PointT, PointLabelT>
00136 {
00137 public:
00138 typedef pcl::PointCloud<PointT> PointCloud;
00139 typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00140 typedef pcl::PointCloud<PointNT> NormalCloud;
00141 typedef typename NormalCloud::ConstPtr NormalCloudConstPtr;
00142 typedef pcl::PointCloud<PointLabelT> LabelCloud;
00143 typedef typename LabelCloud::Ptr LabelCloudPtr;
00144 typedef typename LabelCloud::ConstPtr LabelCloudConstPtr;
00145
00146 typedef cob_3d_segmentation::BoundaryPointsEdgeHandler<
00147 PointLabelT,PointT> EdgeHdlT;
00148 typedef cob_3d_segmentation::ClusterGraphStructure<
00149 ClusterHdlT, EdgeHdlT> ClusterGraphT;
00150 typedef typename ClusterHdlT::Ptr ClusterHdlPtr;
00151 typedef typename ClusterHdlT::ClusterPtr ClusterPtr;
00152 typedef typename ClusterGraphT::Ptr ClusterGraphPtr;
00153
00154
00155 public:
00156 FastSegmentation ()
00157 : graph_(new ClusterGraphT)
00158 , min_angle_(cos(30.0f / 180.0f * M_PI))
00159
00160 , max_angle_(cos(45.0f / 180.0f * M_PI))
00161 , min_cluster_size_(200)
00162 , seed_method_(SEED_LINEAR)
00163 {
00164 clusters_ = graph_->clusters();
00165 }
00166
00167 virtual ~FastSegmentation () { }
00168
00169 virtual void setInputCloud(const PointCloudConstPtr& points) {
00170 surface_ = points; clusters_->setPointCloudIn(points); }
00171 virtual LabelCloudConstPtr getOutputCloud() { return labels_; }
00172 virtual bool compute();
00173
00174 inline bool hasLabel(int label_new, int label_this,
00175 int idx_new, int idx_this, Options::ComputeGraph t)
00176 {
00177 if (label_new == I_UNDEF) return false;
00178 if (label_new != label_this)
00179 {
00180 graph_->edges()->updateProperties(
00181 graph_->connect(label_this, label_new), label_this,
00182 idx_this, label_new, idx_new);
00183 }
00184 return true;
00185 }
00186
00187 inline bool hasLabel(int label_new, int label_this,
00188 int idx_new, int idx_this, Options::WithoutGraph t) {
00189 return (label_new != I_UNDEF); }
00190
00191 void setNormalCloudIn(const NormalCloudConstPtr& normals) { normals_ = normals; }
00192 void setLabelCloudInOut(const LabelCloudPtr & labels) { labels_ = labels; }
00193 void setSeedMethod(SeedMethod type) { seed_method_ = type; }
00194 void createSeedPoints();
00195 void mapSegmentColor(pcl::PointCloud<PointXYZRGB>::Ptr color_cloud) {
00196 clusters_->mapClusterColor(color_cloud); }
00197
00198 ClusterHdlPtr clusters() { return clusters_; }
00199 ClusterGraphPtr graph() { return graph_; }
00200
00202 virtual operator cob_3d_mapping_msgs::ShapeArray() const {
00203 return cob_3d_mapping_msgs::ShapeArray();
00204 }
00205
00206 private:
00207 inline float n_threshold(int size)
00208 {
00209 if(size > min_cluster_size_) size = min_cluster_size_;
00210 return (min_angle_ - max_angle_) / min_cluster_size_ * size + max_angle_;
00211 }
00212
00213
00214 ClusterGraphPtr graph_;
00215 ClusterHdlPtr clusters_;
00216 PointCloudConstPtr surface_;
00217 NormalCloudConstPtr normals_;
00218 LabelCloudPtr labels_;
00219
00220 float min_angle_;
00221 float max_angle_;
00222 float min_cluster_size_;
00223 SeedMethod seed_method_;
00224 std::vector<std::list<unsigned int>::iterator> p_seeds_;
00225 std::list<unsigned int> seeds_;
00226 };
00227 }
00228
00229
00230 #endif