Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_grabcut.cpp
16344 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
class CV_GrabcutTest : public cvtest::BaseTest
48
{
49
public:
50
CV_GrabcutTest();
51
~CV_GrabcutTest();
52
protected:
53
bool verify(const Mat& mask, const Mat& exp);
54
void run(int);
55
};
56
57
CV_GrabcutTest::CV_GrabcutTest() {}
58
CV_GrabcutTest::~CV_GrabcutTest() {}
59
60
bool CV_GrabcutTest::verify(const Mat& mask, const Mat& exp)
61
{
62
const float maxDiffRatio = 0.005f;
63
int expArea = countNonZero( exp );
64
int nonIntersectArea = countNonZero( mask != exp );
65
66
float curRatio = (float)nonIntersectArea / (float)expArea;
67
ts->printf( cvtest::TS::LOG, "nonIntersectArea/expArea = %f\n", curRatio );
68
return curRatio < maxDiffRatio;
69
}
70
71
void CV_GrabcutTest::run( int /* start_from */)
72
{
73
cvtest::DefaultRngAuto defRng;
74
75
Mat img = imread(string(ts->get_data_path()) + "shared/airplane.png");
76
Mat mask_prob = imread(string(ts->get_data_path()) + "grabcut/mask_prob.png", 0);
77
Mat exp_mask1 = imread(string(ts->get_data_path()) + "grabcut/exp_mask1.png", 0);
78
Mat exp_mask2 = imread(string(ts->get_data_path()) + "grabcut/exp_mask2.png", 0);
79
80
if (img.empty() || (!mask_prob.empty() && img.size() != mask_prob.size()) ||
81
(!exp_mask1.empty() && img.size() != exp_mask1.size()) ||
82
(!exp_mask2.empty() && img.size() != exp_mask2.size()) )
83
{
84
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
85
return;
86
}
87
88
Rect rect(Point(24, 126), Point(483, 294));
89
Mat exp_bgdModel, exp_fgdModel;
90
91
Mat mask;
92
Mat bgdModel, fgdModel;
93
grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT );
94
bgdModel.copyTo(exp_bgdModel);
95
fgdModel.copyTo(exp_fgdModel);
96
grabCut( img, mask, rect, bgdModel, fgdModel, 2, GC_EVAL_FREEZE_MODEL );
97
98
// Multiply images by 255 for more visuality of test data.
99
if( mask_prob.empty() )
100
{
101
mask.copyTo( mask_prob );
102
imwrite(string(ts->get_data_path()) + "grabcut/mask_prob.png", mask_prob);
103
}
104
if( exp_mask1.empty() )
105
{
106
exp_mask1 = (mask & 1) * 255;
107
imwrite(string(ts->get_data_path()) + "grabcut/exp_mask1.png", exp_mask1);
108
}
109
if (!verify((mask & 1) * 255, exp_mask1))
110
{
111
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
112
return;
113
}
114
// The model should not be changed after calling with GC_EVAL_FREEZE_MODEL
115
double sumBgdModel = cv::sum(cv::abs(bgdModel) - cv::abs(exp_bgdModel))[0];
116
double sumFgdModel = cv::sum(cv::abs(fgdModel) - cv::abs(exp_fgdModel))[0];
117
if (sumBgdModel >= 0.1 || sumFgdModel >= 0.1)
118
{
119
ts->printf(cvtest::TS::LOG, "sumBgdModel = %f, sumFgdModel = %f\n", sumBgdModel, sumFgdModel);
120
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
121
return;
122
}
123
124
mask = mask_prob;
125
bgdModel.release();
126
fgdModel.release();
127
rect = Rect();
128
grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_MASK );
129
grabCut( img, mask, rect, bgdModel, fgdModel, 1, GC_EVAL );
130
131
if( exp_mask2.empty() )
132
{
133
exp_mask2 = (mask & 1) * 255;
134
imwrite(string(ts->get_data_path()) + "grabcut/exp_mask2.png", exp_mask2);
135
}
136
if (!verify((mask & 1) * 255, exp_mask2))
137
{
138
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
139
return;
140
}
141
ts->set_failed_test_info(cvtest::TS::OK);
142
}
143
144
TEST(Imgproc_GrabCut, regression) { CV_GrabcutTest test; test.safe_run(); }
145
146
TEST(Imgproc_GrabCut, repeatability)
147
{
148
cvtest::TS& ts = *cvtest::TS::ptr();
149
150
Mat image_1 = imread(string(ts.get_data_path()) + "grabcut/image1652.ppm", IMREAD_COLOR);
151
Mat mask_1 = imread(string(ts.get_data_path()) + "grabcut/mask1652.ppm", IMREAD_GRAYSCALE);
152
Rect roi_1(0, 0, 150, 150);
153
154
Mat image_2 = image_1.clone();
155
Mat mask_2 = mask_1.clone();
156
Rect roi_2 = roi_1;
157
158
Mat image_3 = image_1.clone();
159
Mat mask_3 = mask_1.clone();
160
Rect roi_3 = roi_1;
161
162
Mat bgdModel_1, fgdModel_1;
163
Mat bgdModel_2, fgdModel_2;
164
Mat bgdModel_3, fgdModel_3;
165
166
theRNG().state = 12378213;
167
grabCut(image_1, mask_1, roi_1, bgdModel_1, fgdModel_1, 1, GC_INIT_WITH_MASK);
168
theRNG().state = 12378213;
169
grabCut(image_2, mask_2, roi_2, bgdModel_2, fgdModel_2, 1, GC_INIT_WITH_MASK);
170
theRNG().state = 12378213;
171
grabCut(image_3, mask_3, roi_3, bgdModel_3, fgdModel_3, 1, GC_INIT_WITH_MASK);
172
173
EXPECT_EQ(0, countNonZero(mask_1 != mask_2));
174
EXPECT_EQ(0, countNonZero(mask_1 != mask_3));
175
EXPECT_EQ(0, countNonZero(mask_2 != mask_3));
176
}
177
178
}} // namespace
179
180