40 #ifndef PCL_SEGMENTATION_GRABCUT
41 #define PCL_SEGMENTATION_GRABCUT
43 #include <pcl/point_cloud.h>
44 #include <pcl/pcl_base.h>
45 #include <pcl/point_types.h>
46 #include <pcl/segmentation/boost.h>
47 #include <pcl/search/search.h>
51 namespace segmentation
82 addNodes (std::size_t n = 1);
88 addSourceEdge (
int u,
double cap);
91 addTargetEdge (
int u,
double cap);
95 addEdge (
int u,
int v,
double cap_uv,
double cap_vu = 0.0);
107 operator() (
int u,
int v)
const;
110 getSourceEdgeCapacity (
int u)
const;
113 getTargetEdgeCapacity (
int u)
const;
117 typedef enum { FREE = 0x00, SOURCE = 0x01, TARGET = 0x02 } nodestate;
121 typedef std::pair<capacitated_edge::iterator, capacitated_edge::iterator>
edge_pair;
133 augmentPath (
const std::pair<int, int>& path, std::deque<int>& orphans);
136 adoptOrphans (std::deque<int>& orphans);
144 isActive (
int u)
const {
return ((u == active_head_) || (active_list_[u].first != TERMINAL)); }
150 markInactive (
int u);
160 std::vector<unsigned char>
cut_;
164 static const int TERMINAL = -1;
166 std::vector<std::pair<int, edge_pair> > parents_;
168 std::vector<std::pair<int, int> > active_list_;
169 int active_head_, active_tail_;
176 Color (
float _r,
float _g,
float _b) :
r(_r),
g(_g),
b(_b) {}
179 template<
typename Po
intT>
182 template<
typename Po
intT>
224 GMM () : gaussians_ (0) {}
226 GMM (std::size_t
K) : gaussians_ (K) {}
231 getK ()
const {
return gaussians_.size (); }
234 resize (std::size_t
K) { gaussians_.resize (K); }
237 operator[] (std::size_t pos) {
return (gaussians_[pos]); }
240 operator[] (std::size_t pos)
const {
return (gaussians_[pos]); }
243 probabilityDensity (
const Color &c);
246 probabilityDensity(std::size_t i,
const Color &c);
250 std::vector<Gaussian> gaussians_;
258 : sum_ (
Eigen::Vector3f::Zero ())
259 , accumulator_ (
Eigen::Matrix3f::Zero ())
269 fit (
Gaussian& g, std::size_t total_count,
bool compute_eigens =
false)
const;
282 Eigen::Vector3f sum_;
284 Eigen::Matrix3f accumulator_;
294 const std::vector<int>& indices,
295 const std::vector<SegmentationValue> &hardSegmentation,
296 std::vector<std::size_t> &components,
297 GMM &background_GMM, GMM &foreground_GMM);
301 const std::vector<int>& indices,
302 const std::vector<SegmentationValue>& hard_segmentation,
303 std::vector<std::size_t>& components,
304 GMM& background_GMM, GMM& foreground_GMM);
316 template <
typename Po
intT>
393 extract (std::vector<pcl::PointIndices>& clusters);
437 addEdge (vertex_descriptor v1, vertex_descriptor v2,
float capacity,
float rev_capacity);
467 std::vector<segmentation::grabcut::TrimapValue>
trimap_;
481 #include <pcl/segmentation/impl/grabcut_segmentation.hpp>
Color(float _r, float _g, float _b)
pcl::PointCloud< Color > Image
An Image is a point cloud of Color.
float L_
L = a large value to force a pixel to be foreground or background.
int nb_neighbours_
Number of neighbours.
std::vector< segmentation::grabcut::SegmentationValue > hard_segmentation_
bool isSource(vertex_descriptor v)
GMM(std::size_t K)
Initialize GMM with ddesired number of gaussians.
float beta_
beta = 1/2 * average of the squared color distances between all pairs of 8-neighboring pixels...
virtual void fitGMMs()
Fit Gaussian Multi Models.
PointCloud::ConstPtr PointCloudConstPtr
std::vector< std::size_t > GMM_component_
void setTrimap(const PointIndicesConstPtr &indices, segmentation::grabcut::TrimapValue t)
Edit Trimap.
void setLambda(float lambda)
Set lambda parameter to user given value.
Helper class that fits a single Gaussian to color samples.
std::vector< unsigned char > cut_
identifies which side of the cut a node falls
double edge_capacity_type
std::vector< float > soft_segmentation_
Eigen::Matrix3f inverse
inverse of the covariance matrix
void addConstant(double c)
add constant flow to graph
void computeNLinksOrganized()
Compute NLinks from image.
void initGraph()
Build the graph for GraphCut.
void addEdge(vertex_descriptor v1, vertex_descriptor v2, float capacity, float rev_capacity)
Add an edge to the graph, graph must be oriented so we add the edge and its reverse.
size_t numNodes() const
get number of nodes in the graph
bool isActiveSetEmpty() const
GMM()
Initialize GMM with ddesired number of gaussians.
void computeBetaNonOrganized()
Compute beta from cloud.
void resize(std::size_t K)
resize gaussians
void extract(std::vector< pcl::PointIndices > &clusters)
This method launches the segmentation algorithm and returns the clusters that were obtained during th...
float colorDistance(const Color &c1, const Color &c2)
Compute squared distance between two colors.
uint32_t width_
image width
void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Eigen::Matrix3f covariance
covariance matrix of the gaussian
Color(const pcl::RGB &color)
std::vector< vertex_descriptor > graph_nodes_
Graph nodes.
std::vector< int > indices
std::vector< float > weights
boost::shared_ptr< PointCloud< PointT > > Ptr
A structure representing RGB color information.
std::vector< capacitated_edge > nodes_
nodes and their outgoing internal edges
float lambda_
lambda = 50. This value was suggested the GrabCut paper.
SegmentationValue
Grabcut derived hard segementation values.
PointCloud::Ptr PointCloudPtr
void setNumberOfNeighbours(int nb_neighbours)
Allows to set the number of neighbours to find.
std::vector< double > target_edges_
edges entering the target
KdTreePtr tree_
Pointer to the spatial search object.
segmentation::grabcut::Image::Ptr image_
Converted input.
void setBackgroundPoints(const PointCloudConstPtr &background_points)
Set background points, foreground points = points \ background points.
boost::shared_ptr< pcl::search::Search< PointT > > Ptr
void fit(Gaussian &g, std::size_t total_count, bool compute_eigens=false) const
Build the gaussian out of all the added color samples.
std::vector< NLinks > n_links_
Precomputed N-link weights.
uint32_t K_
Number of GMM components.
Eigen::Vector3f eigenvector
eigenvector corresponding to the heighest eigenvector
boost::shared_ptr< PointIndices const > PointIndicesConstPtr
float pi
weighting of this gaussian in the GMM.
std::vector< double > source_edges_
edges leaving the source
PCLBase< PointT >::PointCloudConstPtr PointCloudConstPtr
void computeBetaOrganized()
Compute beta from image.
uint32_t height_
image height
pcl::segmentation::grabcut::BoykovKolmogorov::vertex_descriptor vertex_descriptor
bool inSinkTree(int u) const
return true if u is in the t-set after calling solve
PCL_EXPORTS void learnGMMs(const Image &image, const std::vector< int > &indices, const std::vector< SegmentationValue > &hard_segmentation, std::vector< std::size_t > &components, GMM &background_GMM, GMM &foreground_GMM)
Iteratively learn GMMs using GrabCut updating algorithm.
pcl::search::Search< PointT >::Ptr KdTreePtr
std::vector< segmentation::grabcut::TrimapValue > trimap_
boost::shared_ptr< ::pcl::PointIndices const > PointIndicesConstPtr
KdTreePtr getSearchMethod()
Get a pointer to the search method used.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool inSourceTree(int u) const
return true if u is in the s-set after calling solve.
Color mu
mean of the gaussian
boost implementation of Boykov and Kolmogorov's maxflow algorithm doesn't support negative flows whic...
void setK(uint32_t K)
Set K parameter to user given value.
virtual ~GrabCut()
Desctructor.
virtual ~BoykovKolmogorov()
destructor
std::vector< float > dists
pcl::segmentation::grabcut::BoykovKolmogorov graph_
Graph for Graphcut.
void computeL()
Compute L parameter from given lambda.
PCL_EXPORTS void buildGMMs(const Image &image, const std::vector< int > &indices, const std::vector< SegmentationValue > &hardSegmentation, std::vector< std::size_t > &components, GMM &background_GMM, GMM &foreground_GMM)
Build the initial GMMs using the Orchard and Bouman color clustering algorithm.
Implementation of the GrabCut segmentation in "GrabCut — Interactive Foreground Extraction using Ite...
int getNumberOfNeighbours() const
Returns the number of neighbours to find.
segmentation::grabcut::GMM background_GMM_
PCLBase< PointT >::PointCloudPtr PointCloudPtr
Structure to save RGB colors into floats.
std::pair< capacitated_edge::iterator, capacitated_edge::iterator > edge_pair
edge pair
void setEpsilon(float epsilon)
set epsilon which will be added to the covariance matrix diagonal which avoids singular covariance ma...
A point structure representing Euclidean xyz coordinates, and the RGB color.
int updateHardSegmentation()
TrimapValue
User supplied Trimap values.
GrabCut(uint32_t K=5, float lambda=50.f)
Constructor.
pcl::search::Search< PointT > KdTree
segmentation::grabcut::GMM foreground_GMM_
void setTerminalWeights(vertex_descriptor v, float source_capacity, float sink_capacity)
Set the weights of SOURCE –> v and v –> SINK.
bool initialized_
is segmentation initialized
double flow_value_
current flow value (includes constant)
virtual void refine()
Run Grabcut refinement on the hard segmentation.
float eigenvalue
heighest eigenvalue of covariance matrix
std::map< int, double > capacitated_edge
capacitated edge
GaussianFitter(float epsilon=0.0001)
float determinant
determinant of the covariance matrix
bool isActive(int u) const
active if head or previous node is not the terminal
void computeNLinksNonOrganized()
Compute NLinks from cloud.
void add(const Color &c)
Add a color sample.
void setBackgroundPointsIndices(int x1, int y1, int x2, int y2)
Set background indices, foreground indices = indices \ background indices.
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.