42 #include <pcl/memory.h>
43 #include <pcl/point_cloud.h>
44 #include <pcl/pcl_base.h>
45 #include <pcl/pcl_macros.h>
46 #include <pcl/point_types.h>
47 #include <pcl/segmentation/boost.h>
48 #include <pcl/search/search.h>
52 namespace segmentation
83 addNodes (std::size_t n = 1);
89 addSourceEdge (
int u,
double cap);
92 addTargetEdge (
int u,
double cap);
96 addEdge (
int u,
int v,
double cap_uv,
double cap_vu = 0.0);
108 operator() (
int u,
int v)
const;
111 getSourceEdgeCapacity (
int u)
const;
114 getTargetEdgeCapacity (
int u)
const;
118 enum nodestate { FREE = 0x00, SOURCE = 0x01, TARGET = 0x02 };
122 using edge_pair = std::pair<capacitated_edge::iterator, capacitated_edge::iterator>;
134 augmentPath (
const std::pair<int, int>& path, std::deque<int>& orphans);
137 adoptOrphans (std::deque<int>& orphans);
145 isActive (
int u)
const {
return ((u == active_head_) || (active_list_[u].first != TERMINAL)); }
151 markInactive (
int u);
161 std::vector<unsigned char>
cut_;
165 static const int TERMINAL;
167 std::vector<std::pair<int, edge_pair> > parents_;
169 std::vector<std::pair<int, int> > active_list_;
170 int active_head_, active_tail_;
177 Color (
float _r,
float _g,
float _b) :
r(_r),
g(_g),
b(_b) {}
180 template<
typename Po
intT>
183 template<
typename Po
intT>
225 GMM () : gaussians_ (0) {}
227 GMM (std::size_t
K) : gaussians_ (K) {}
232 getK ()
const {
return gaussians_.size (); }
235 resize (std::size_t
K) { gaussians_.resize (K); }
238 operator[] (std::size_t pos) {
return (gaussians_[pos]); }
241 operator[] (std::size_t pos)
const {
return (gaussians_[pos]); }
244 probabilityDensity (
const Color &c);
247 probabilityDensity(std::size_t i,
const Color &c);
251 std::vector<Gaussian> gaussians_;
259 : sum_ (
Eigen::Vector3f::Zero ())
260 , accumulator_ (
Eigen::Matrix3f::Zero ())
270 fit (
Gaussian& g, std::size_t total_count,
bool compute_eigens =
false)
const;
283 Eigen::Vector3f sum_;
285 Eigen::Matrix3f accumulator_;
287 std::uint32_t count_;
296 const std::vector<int>& indices,
297 const std::vector<SegmentationValue> &hardSegmentation,
298 std::vector<std::size_t> &components,
299 GMM &background_GMM, GMM &foreground_GMM);
303 const std::vector<int>& indices,
304 const std::vector<SegmentationValue>& hard_segmentation,
305 std::vector<std::size_t>& components,
306 GMM& background_GMM, GMM& foreground_GMM);
318 template <
typename Po
intT>
395 extract (std::vector<pcl::PointIndices>& clusters);
469 std::vector<segmentation::grabcut::TrimapValue>
trimap_;
483 #include <pcl/segmentation/impl/grabcut_segmentation.hpp>
Color(float _r, float _g, float _b)
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)
std::uint32_t width_
image width
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.
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
std::vector< float > soft_segmentation_
std::pair< capacitated_edge::iterator, capacitated_edge::iterator > edge_pair
edge pair
Eigen::Matrix3f inverse
inverse of the covariance matrix
void addConstant(double c)
add constant flow to graph
void computeNLinksOrganized()
Compute NLinks from image.
typename KdTree::Ptr KdTreePtr
std::uint32_t K_
Number of GMM components.
std::uint32_t height_
image height
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.
double edge_capacity_type
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...
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
float colorDistance(const Color &c1, const Color &c2)
Compute squared distance between two colors.
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
std::map< int, double > capacitated_edge
capacitated edge
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 segmentation values.
void setNumberOfNeighbours(int nb_neighbours)
Allows to set the number of neighbours to find.
std::vector< double > target_edges_
edges entering the target
shared_ptr< PointCloud< PointT > > Ptr
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.
void fit(Gaussian &g, std::size_t total_count, bool compute_eigens=false) const
Build the gaussian out of all the added color samples.
PointIndices::ConstPtr PointIndicesConstPtr
std::vector< NLinks > n_links_
Precomputed N-link weights.
Eigen::Vector3f eigenvector
eigenvector corresponding to the highest eigenvector
float pi
weighting of this gaussian in the GMM.
std::vector< double > source_edges_
edges leaving the source
void computeBetaOrganized()
Compute beta from image.
typename PointCloud::Ptr PointCloudPtr
bool inSinkTree(int u) const
return true if u is in the t-set after calling solve
std::size_t numNodes() const
get number of nodes in the graph
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.
std::vector< segmentation::grabcut::TrimapValue > trimap_
KdTreePtr getSearchMethod()
Get a pointer to the search method used.
bool inSourceTree(int u) const
return true if u is in the s-set after calling solve.
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide a pointer to the input dataset.
Color mu
mean of the gaussian
pcl::PointCloud< Color > Image
An Image is a point cloud of Color.
boost implementation of Boykov and Kolmogorov's maxflow algorithm doesn't support negative flows whic...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
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.
GrabCut(std::uint32_t K=5, float lambda=50.f)
Constructor.
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_
Structure to save RGB colors into floats.
shared_ptr< pcl::search::Search< PointT > > Ptr
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.
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
highest eigenvalue of covariance matrix
GaussianFitter(float epsilon=0.0001)
float determinant
determinant of the covariance matrix
PointIndices::ConstPtr PointIndicesConstPtr
bool isActive(int u) const
active if head or previous node is not the terminal
void setK(std::uint32_t K)
Set K parameter to user given value.
void computeNLinksNonOrganized()
Compute NLinks from cloud.
typename PointCloud::ConstPtr PointCloudConstPtr
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.
pcl::segmentation::grabcut::BoykovKolmogorov::vertex_descriptor vertex_descriptor
void setSearchMethod(const KdTreePtr &tree)
Provide a pointer to the search object.