45 #include <pcl/point_cloud.h> 46 #include <pcl/pcl_base.h> 49 #include <pcl/search/search.h> 53 namespace segmentation
84 addNodes (std::size_t n = 1);
90 addSourceEdge (
int u,
double cap);
93 addTargetEdge (
int u,
double cap);
97 addEdge (
int u,
int v,
double cap_uv,
double cap_vu = 0.0);
109 operator() (
int u,
int v)
const;
112 getSourceEdgeCapacity (
int u)
const;
115 getTargetEdgeCapacity (
int u)
const;
119 enum nodestate { FREE = 0x00, SOURCE = 0x01, TARGET = 0x02 };
123 using edge_pair = std::pair<capacitated_edge::iterator, capacitated_edge::iterator>;
135 augmentPath (
const std::pair<int, int>& path, std::deque<int>& orphans);
138 adoptOrphans (std::deque<int>& orphans);
146 isActive (
int u)
const {
return ((u == active_head_) || (active_list_[u].first != TERMINAL)); }
152 markInactive (
int u);
162 std::vector<unsigned char>
cut_;
166 static const int TERMINAL;
168 std::vector<std::pair<int, edge_pair> > parents_;
170 std::vector<std::pair<int, int> > active_list_;
171 int active_head_, active_tail_;
178 Color (
float _r,
float _g,
float _b) :
r(_r),
g(_g),
b(_b) {}
181 template<
typename Po
intT>
184 template<
typename Po
intT>
226 GMM () : gaussians_ (0) {}
228 GMM (std::size_t
K) : gaussians_ (
K) {}
233 getK ()
const {
return gaussians_.size (); }
239 operator[] (std::size_t pos) {
return (gaussians_[pos]); }
242 operator[] (std::size_t pos)
const {
return (gaussians_[pos]); }
245 probabilityDensity (
const Color &c);
248 probabilityDensity(std::size_t i,
const Color &c);
252 std::vector<Gaussian> gaussians_;
260 : sum_ (
Eigen::Vector3f::Zero ())
261 , accumulator_ (
Eigen::Matrix3f::Zero ())
271 fit (
Gaussian& g, std::size_t total_count,
bool compute_eigens =
false)
const;
284 Eigen::Vector3f sum_;
286 Eigen::Matrix3f accumulator_;
297 const std::vector<int>& indices,
298 const std::vector<SegmentationValue> &hardSegmentation,
299 std::vector<std::size_t> &components,
300 GMM &background_GMM, GMM &foreground_GMM);
304 const std::vector<int>& indices,
305 const std::vector<SegmentationValue>& hard_segmentation,
306 std::vector<std::size_t>& components,
307 GMM& background_GMM, GMM& foreground_GMM);
319 template <
typename Po
intT>
396 extract (std::vector<pcl::PointIndices>& clusters);
470 std::vector<segmentation::grabcut::TrimapValue>
trimap_;
484 #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.
shared_ptr< PointCloud< PointT > > Ptr
Defines functions, macros and traits for allocating and using memory.
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
int getNumberOfNeighbours() const
Returns the number of neighbours to find.
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
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.
bool inSourceTree(int u) const
return true if u is in the s-set after calling solve.
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.
bool isActive(int u) const
active if head or previous node is not the terminal
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.
bool inSinkTree(int u) const
return true if u is in the t-set after calling solve
std::vector< double > target_edges_
edges entering the target
bool isActiveSetEmpty() const
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.
Defines all the PCL implemented PointT point type structures.
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
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.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
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...
void fit(Gaussian &g, std::size_t total_count, bool compute_eigens=false) const
Build the gaussian out of all the added color samples.
virtual ~BoykovKolmogorov()
destructor
std::vector< float > dists
pcl::segmentation::grabcut::BoykovKolmogorov graph_
Graph for Graphcut.
void computeL()
Compute L parameter from given lambda.
std::size_t numNodes() const
get number of nodes in the graph
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 It...
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
void setK(std::uint32_t K)
Set K parameter to user given value.
Defines all the PCL and non-PCL macros used.
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.