-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGrabCut.h
executable file
·88 lines (63 loc) · 2.74 KB
/
GrabCut.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef GRAB_CUT_H
#define GRAB_CUT_H
#include "graph.h"
#include "Image.h"
#include "Color.h"
#include "GMM.h"
namespace GrabCutNS {
class GrabCut
{
public:
GrabCut( Image<Color> *image );
~GrabCut();
// Initialize Trimap, inside rectangle is TrimapUnknown, outside is TrimapBackground
void initialize(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);
void initializeWithMask(Image<Color>* mask);
// Edit Trimap
void setTrimap(int x1, int y1, int x2, int y2, const TrimapValue& t);
// 显示GMM组件分配
void fitGMMs();
// Run Grabcut refinement on the hard segmentation
// 运行Grabcut细化算法收敛。
void refine();
// 运行一步Grabcut细化算法
int refineOnce(); // returns the number of pixels that have changed from foreground to background or vice versa
const Image<Real>* getAlphaImage() const { return m_AlphaImage; }
const Image<Real>* getNLinksImage() const { return m_NLinksImage; }
const Image<Color>* getTLinksImage() const { return m_TLinksImage; }
const Image<Color>* getGMMsImage() const { return m_GMMImage; }
const Image<Color>* getImage() const { return m_image; }
void buildImages();
private:
unsigned int m_w, m_h; // All the following Image<*> variables will be the same width and height.
// Store them here so we don't have to keep asking for them.
Image<Color> *m_image;
Image<TrimapValue> *m_trimap;
Image<unsigned int> *m_GMMcomponent;
Image<SegmentationValue> *m_hardSegmentation;
Image<Real> *m_softSegmentation; // Not yet implemented (this would be interpreted as alpha)
GMM *m_backgroundGMM, *m_foregroundGMM;
int updateHardSegmentation(); // Update hard segmentation after running GraphCut,
// Returns the number of pixels that have changed from foreground to background or vice versa.
// Variables used in formulas from the paper.
Real m_lambda; // lambda = 50. This value was suggested the GrabCut paper.
Real m_beta; // beta = 1 / ( 2 * average of the squared color distances between all pairs of neighboring pixels (8-neighborhood) )
Real m_L; // L = a large value to force a pixel to be foreground or background
void computeBeta();
void computeL();
// Precomputed N-link weights
Image<NLinks> *m_NLinks;
void computeNLinks();
Real computeNLink(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);
// Graph for Graphcut
Graph *m_graph;
Image<Graph::node_id> *m_nodes;
void initGraph(); // builds the graph for GraphCut
// Images of various variables that can be displayed for debugging.
Image<Real> *m_NLinksImage;
Image<Color> *m_TLinksImage;
Image<Color> *m_GMMImage;
Image<Real> *m_AlphaImage;
};
}
#endif //GRAB_CUT_H