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 #include <string>
46 #include <iostream>
47
48 using namespace std;
49 using namespace cv;
50
51 class CV_GrabcutTest : public cvtest::BaseTest
52 {
53 public:
54 CV_GrabcutTest();
55 ~CV_GrabcutTest();
56 protected:
57 bool verify(const Mat& mask, const Mat& exp);
58 void run(int);
59 };
60
CV_GrabcutTest()61 CV_GrabcutTest::CV_GrabcutTest() {}
~CV_GrabcutTest()62 CV_GrabcutTest::~CV_GrabcutTest() {}
63
verify(const Mat & mask,const Mat & exp)64 bool CV_GrabcutTest::verify(const Mat& mask, const Mat& exp)
65 {
66 const float maxDiffRatio = 0.005f;
67 int expArea = countNonZero( exp );
68 int nonIntersectArea = countNonZero( mask != exp );
69
70 float curRatio = (float)nonIntersectArea / (float)expArea;
71 ts->printf( cvtest::TS::LOG, "nonIntersectArea/expArea = %f\n", curRatio );
72 return curRatio < maxDiffRatio;
73 }
74
run(int)75 void CV_GrabcutTest::run( int /* start_from */)
76 {
77 cvtest::DefaultRngAuto defRng;
78
79 Mat img = imread(string(ts->get_data_path()) + "shared/airplane.png");
80 Mat mask_prob = imread(string(ts->get_data_path()) + "grabcut/mask_prob.png", 0);
81 Mat exp_mask1 = imread(string(ts->get_data_path()) + "grabcut/exp_mask1.png", 0);
82 Mat exp_mask2 = imread(string(ts->get_data_path()) + "grabcut/exp_mask2.png", 0);
83
84 if (img.empty() || (!mask_prob.empty() && img.size() != mask_prob.size()) ||
85 (!exp_mask1.empty() && img.size() != exp_mask1.size()) ||
86 (!exp_mask2.empty() && img.size() != exp_mask2.size()) )
87 {
88 ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
89 return;
90 }
91
92 Rect rect(Point(24, 126), Point(483, 294));
93 Mat exp_bgdModel, exp_fgdModel;
94
95 Mat mask;
96 mask = Scalar(0);
97 Mat bgdModel, fgdModel;
98 grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT );
99 grabCut( img, mask, rect, bgdModel, fgdModel, 2, GC_EVAL );
100
101 // Multiply images by 255 for more visuality of test data.
102 if( mask_prob.empty() )
103 {
104 mask.copyTo( mask_prob );
105 imwrite(string(ts->get_data_path()) + "grabcut/mask_prob.png", mask_prob);
106 }
107 if( exp_mask1.empty() )
108 {
109 exp_mask1 = (mask & 1) * 255;
110 imwrite(string(ts->get_data_path()) + "grabcut/exp_mask1.png", exp_mask1);
111 }
112
113 if (!verify((mask & 1) * 255, exp_mask1))
114 {
115 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
116 return;
117 }
118
119 mask = mask_prob;
120 bgdModel.release();
121 fgdModel.release();
122 rect = Rect();
123 grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_MASK );
124 grabCut( img, mask, rect, bgdModel, fgdModel, 1, GC_EVAL );
125
126 if( exp_mask2.empty() )
127 {
128 exp_mask2 = (mask & 1) * 255;
129 imwrite(string(ts->get_data_path()) + "grabcut/exp_mask2.png", exp_mask2);
130 }
131
132 if (!verify((mask & 1) * 255, exp_mask2))
133 {
134 ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
135 return;
136 }
137 ts->set_failed_test_info(cvtest::TS::OK);
138 }
139
TEST(Imgproc_GrabCut,regression)140 TEST(Imgproc_GrabCut, regression) { CV_GrabcutTest test; test.safe_run(); }
141
TEST(Imgproc_GrabCut,repeatability)142 TEST(Imgproc_GrabCut, repeatability)
143 {
144 cvtest::TS& ts = *cvtest::TS::ptr();
145
146 Mat image_1 = imread(string(ts.get_data_path()) + "grabcut/image1652.ppm", IMREAD_COLOR);
147 Mat mask_1 = imread(string(ts.get_data_path()) + "grabcut/mask1652.ppm", IMREAD_GRAYSCALE);
148 Rect roi_1(0, 0, 150, 150);
149
150 Mat image_2 = image_1.clone();
151 Mat mask_2 = mask_1.clone();
152 Rect roi_2 = roi_1;
153
154 Mat image_3 = image_1.clone();
155 Mat mask_3 = mask_1.clone();
156 Rect roi_3 = roi_1;
157
158 Mat bgdModel_1, fgdModel_1;
159 Mat bgdModel_2, fgdModel_2;
160 Mat bgdModel_3, fgdModel_3;
161
162 theRNG().state = 12378213;
163 grabCut(image_1, mask_1, roi_1, bgdModel_1, fgdModel_1, 1, GC_INIT_WITH_MASK);
164 theRNG().state = 12378213;
165 grabCut(image_2, mask_2, roi_2, bgdModel_2, fgdModel_2, 1, GC_INIT_WITH_MASK);
166 theRNG().state = 12378213;
167 grabCut(image_3, mask_3, roi_3, bgdModel_3, fgdModel_3, 1, GC_INIT_WITH_MASK);
168
169 EXPECT_EQ(0, countNonZero(mask_1 != mask_2));
170 EXPECT_EQ(0, countNonZero(mask_1 != mask_3));
171 EXPECT_EQ(0, countNonZero(mask_2 != mask_3));
172 }
173