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
95 addEdge (
int u,
int v,
double cap_uv,
double cap_vu = 0.0);
115 typedef std::pair<capacitated_edge::iterator, capacitated_edge::iterator>
edge_pair;
127 augmentPath (
const std::pair<int, int>& path, std::deque<int>& orphans);
138 isActive (
int u)
const {
return ((u == active_head_) || (active_list_[u].first != TERMINAL)); }
154 std::vector<unsigned char>
cut_;
158 static const int TERMINAL = -1;
160 std::vector<std::pair<int, edge_pair> > parents_;
162 std::vector<std::pair<int, int> > active_list_;
163 int active_head_, active_tail_;
170 Color (
float _r,
float _g,
float _b) :
r(_r),
g(_g),
b(_b) {}
173 template<
typename Po
intT>
176 r =
static_cast<float> (p.r);
177 g =
static_cast<float> (p.g);
178 b =
static_cast<float> (p.b);
181 template<
typename Po
intT>
185 p.r =
static_cast<uint32_t
> (
r);
186 p.g =
static_cast<uint32_t
> (
g);
187 p.b =
static_cast<uint32_t
> (
b);
230 GMM () : gaussians_ (0) {}
232 GMM (std::size_t
K) : gaussians_ (K) {}
237 getK ()
const {
return gaussians_.size (); }
240 resize (std::size_t
K) { gaussians_.resize (K); }
246 operator[] (std::size_t pos)
const {
return (gaussians_[pos]); }
256 std::vector<Gaussian> gaussians_;
264 : sum_ (Eigen::Vector3f::Zero ())
265 , accumulator_ (Eigen::Matrix3f::Zero ())
275 fit (
Gaussian& g, std::size_t total_count,
bool compute_eigens =
false)
const;
288 Eigen::Vector3f sum_;
290 Eigen::Matrix3f accumulator_;
300 const std::vector<int>& indices,
301 const std::vector<SegmentationValue> &hardSegmentation,
302 std::vector<std::size_t> &components,
303 GMM &background_GMM, GMM &foreground_GMM);
307 const std::vector<int>& indices,
308 const std::vector<SegmentationValue>& hard_segmentation,
309 std::vector<std::size_t>& components,
310 GMM& background_GMM, GMM& foreground_GMM);
322 template <
typename Po
intT>
399 extract (std::vector<pcl::PointIndices>& clusters);
431 computeNLink (uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2);
475 std::vector<segmentation::grabcut::TrimapValue>
trimap_;
489 #include <pcl/segmentation/impl/grabcut.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.
void computeBeta()
Update hard segmentation after running GraphCut,.
std::vector< segmentation::grabcut::SegmentationValue > hard_segmentation_
bool isSource(vertex_descriptor v)
void addSourceEdge(int u, double cap)
add edge from s to nodeId
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
void clearActive()
clear active set
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.
float computeNLink(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
Compute NLinks at a specific rectangular location.
Helper class that fits a single Gaussian to color samples.
std::vector< unsigned char > cut_
identifies which side of the cut a node falls
void markInactive(int u)
mark vertex as inactive
boost::shared_ptr< PointCloud< PointT > > Ptr
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 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.
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.
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 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 probabilityDensity(const Color &c)
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)
double solve()
solve the max-flow problem and return the flow
std::vector< vertex_descriptor > graph_nodes_
Graph nodes.
std::vector< int > indices
std::vector< float > weights
A structure representing RGB color information.
void clear()
clear the graph and internal datastructures
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.
void adoptOrphans(std::deque< int > &orphans)
adopt orphaned subtrees
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.
void markActive(int u)
mark vertex as active
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.
void computeNLinks()
Compute NLinks.
std::vector< double > source_edges_
edges leaving the source
PCLBase< PointT >::PointCloudConstPtr PointCloudConstPtr
uint32_t height_
image height
void initializeTrees()
initialize trees from source and target
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::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.
void addTargetEdge(int u, double cap)
add edge from nodeId to t
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.
BoykovKolmogorov(std::size_t max_nodes=0)
construct a maxflow/mincut problem with estimated max_nodes
virtual ~BoykovKolmogorov()
destructor
double operator()(int u, int v) const
returns the residual capacity for an edge (use -1 for terminal (-1,-1) is the current flow ...
void addEdge(int u, int v, double cap_uv, double cap_vu=0.0)
add edge from u to v and edge from v to u (requires cap_uv + cap_vu >= 0)
std::vector< float > dists
int addNodes(std::size_t n=1)
add nodes to the graph (returns the id of the first node added)
pcl::segmentation::grabcut::BoykovKolmogorov graph_
Graph for Graphcut.
void computeL()
Compute L parameter from given lambda.
Implementation of the GrabCut segmentation in "GrabCut — Interactive Foreground Extraction using Iter...
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.
void preAugmentPaths()
pre-augment s-u-t and s-u-v-t paths
void augmentPath(const std::pair< int, int > &path, std::deque< int > &orphans)
augment the path found by expandTrees; return orphaned subtrees
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 reset()
reset all edge capacities to zero (but don't free the graph)
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
std::pair< int, int > expandTrees()
expand trees until a path is found (or no path (-1, -1))
GaussianFitter(float epsilon=0.0001)
float determinant
determinant of the covariance matrix
Gaussian & operator[](std::size_t pos)
bool isActive(int u) const
active if head or previous node is not the terminal
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.