-
Notifications
You must be signed in to change notification settings - Fork 4
/
ForegroundExtractor.cpp
executable file
·53 lines (42 loc) · 1.49 KB
/
ForegroundExtractor.cpp
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
#include "ForegroundExtractor.h"
ForegroundExtractor::ForegroundExtractor () : iternum(5)
{
}
void ForegroundExtractor::setIternum(int num)
{
iternum = num;
}
cv::Mat ForegroundExtractor::getForegroundMask(const cv::Mat &src, const cv::Rect &roi)
{
cv::Mat bgModel, fgModel, dst; // the models (internally used)
// GrabCut segmentation
cv::grabCut(src, // input image
dst, // segmentation result
roi, // rectangle contain foreground
bgModel, fgModel, // models
iternum, // number of iterations
cv::GC_INIT_WITH_RECT // use rectangle
);
// Get the pixels marked as likely foreground
cv::compare(dst, cv::GC_PR_FGD, dst, cv::CMP_EQ);
return dst;
}
cv::Mat ForegroundExtractor::getForeground(const cv::Mat &src, const cv::Mat &foreMask)
{
// Generate output image
cv::Mat foreground(src.size(), CV_8UC3,
cv::Scalar(255, 255, 255));
src.copyTo(foreground, // bg pixels are not copied
foreMask);
return foreground;
}
cv::Mat ForegroundExtractor::getForegroundWeighted(const cv::Mat &src, const cv::Mat &foreMask, const double weight)
{
// Generate output image
cv::Mat foreground(src.size(), CV_8UC3,
cv::Scalar(255, 255, 255));
src.copyTo(foreground, // bg pixels are not copied
foreMask);
cv::addWeighted(foreground, 1.0-weight, src, weight, 0.0, foreground);
return foreground;
}