Repository navigation
Expand file tree
/
Copy pathGrabCutExtracting.cpp
More file actions
120 lines (106 loc) · 3.82 KB
/
Copy pathGrabCutExtracting.cpp
File metadata and controls
120 lines (106 loc) · 3.82 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// This file is part of VideoObjectExtractor.
//
// Copyright 2014-2015 Tomasz Posłuszny
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "FrameGrabber.hpp"
#include "GrabCutExtracting.hpp"
#include "MorphologicalProcessing.hpp"
#include "RegionSizeFiltering.hpp"
GrabCutExtracting::GrabCutExtracting() : m_kernel_size(9)
{
}
GrabCutExtracting::~GrabCutExtracting()
{
}
void GrabCutExtracting::process_implementation(Mat &a, void* data)
{
const Mat& current_frame = FrameGrabber::instance()->getCurrentFrame();
Mat gc_fgd;
Rect dummy_rect;
Mat bmask;
Mat bgd_model;
Mat fgd_model;
Mat kernel = getStructuringElement(MORPH_RECT, Size(m_kernel_size, m_kernel_size));
Mat gc_mask(a.rows, a.cols, CV_8UC1);
int fgd = 0, bgd = 0;
int i, j;
// TODO: think more how to do it to get best results
erode(a, gc_fgd, kernel);
// everything beyond eroded mask is assumed to be background
// imshow("Eroded mask", gc_fgd);
dilate(a, a, kernel);
// everything inside dilated mask is assumed to be foreground
// imshow("Dilated mask", a);
// grabcut input mask visualization
Mat mvis(a.rows, a.cols, CV_8UC1);
// create grabcut mask
FOR_PIXELS(i, j, a)
if (BINARY_ONE == gc_fgd.at<uchar>(i, j)) {
gc_mask.at<uchar>(i, j) = GC_FGD;
fgd++;
mvis.at<uchar>(i, j) = BINARY_ONE;
}
else if (BINARY_ZERO == a.at<uchar>(i, j)) {
gc_mask.at<uchar>(i, j) = GC_BGD;
bgd++;
mvis.at<uchar>(i, j) = BINARY_ZERO;
} else {
gc_mask.at<uchar>(i, j) = GC_PR_FGD;
fgd++;
mvis.at<uchar>(i, j) = BINARY_HALF;
}
// if foreground or background mask is very small, then return original image
if (bgd < 1000 || fgd < 1000) {
//current_frame.copyTo(a);
a = Mat::zeros(a.rows, a.cols, CV_8UC1);
return;
}
// execute grabcut
grabCut(current_frame, gc_mask, dummy_rect, bgd_model, fgd_model, 1, GC_INIT_WITH_MASK);
// show cut out image (silhouette)
get_bin_mask(gc_mask, bmask);
// current_frame.copyTo(a, bmask);
// gc_mask = gc_mask + 1;
// applyColorMap(gc_mask, mvis, COLORMAP_JET);
imshow("GrabCut mask", mvis);
/*
for (i = 1; i < a.rows; i++)
for (j = 1; j < a.cols; j++) {
if (Vec3b(0, 0, 0) == a.at<Vec3b>(i, j)) {
a.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
}
}*/
// make foreground pixels white
FOR_PIXELS(i,j,bmask) {
if (bmask.at<uchar>(i, j) > 0)
bmask.at<uchar>(i, j) = 255;
}
// result: foreground binary mask
bmask.copyTo(a);
}
void GrabCutExtracting::get_bin_mask(const Mat& com_mask, Mat& bin_mask)
{
if( com_mask.empty() || com_mask.type()!=CV_8UC1 )
CV_Error( Error::StsBadArg, "com_mask is empty or has incorrect type (not CV_8UC1)" );
if( bin_mask.empty() || bin_mask.rows!=com_mask.rows || bin_mask.cols!=com_mask.cols )
bin_mask.create( com_mask.size(), CV_8UC1 );
// GC_FGD and GC_PR_FGD become 1, GC_BGD and GC_PR_BGD become 0
bin_mask = com_mask & 1;
}