|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
|
|
class CV_GrabcutTest : public cvtest::BaseTest
|
|
|
{
|
|
|
public:
|
|
|
CV_GrabcutTest();
|
|
|
~CV_GrabcutTest();
|
|
|
protected:
|
|
|
bool verify(const Mat& mask, const Mat& exp);
|
|
|
void run(int);
|
|
|
};
|
|
|
|
|
|
CV_GrabcutTest::CV_GrabcutTest() {}
|
|
|
CV_GrabcutTest::~CV_GrabcutTest() {}
|
|
|
|
|
|
bool CV_GrabcutTest::verify(const Mat& mask, const Mat& exp)
|
|
|
{
|
|
|
const float maxDiffRatio = 0.005f;
|
|
|
int expArea = countNonZero( exp );
|
|
|
int nonIntersectArea = countNonZero( mask != exp );
|
|
|
|
|
|
float curRatio = (float)nonIntersectArea / (float)expArea;
|
|
|
ts->printf( cvtest::TS::LOG, "nonIntersectArea/expArea = %f\n", curRatio );
|
|
|
return curRatio < maxDiffRatio;
|
|
|
}
|
|
|
|
|
|
void CV_GrabcutTest::run( int )
|
|
|
{
|
|
|
cvtest::DefaultRngAuto defRng;
|
|
|
|
|
|
Mat img = imread(string(ts->get_data_path()) + "shared/airplane.png");
|
|
|
Mat mask_prob = imread(string(ts->get_data_path()) + "grabcut/mask_prob.png", 0);
|
|
|
Mat exp_mask1 = imread(string(ts->get_data_path()) + "grabcut/exp_mask1.png", 0);
|
|
|
Mat exp_mask2 = imread(string(ts->get_data_path()) + "grabcut/exp_mask2.png", 0);
|
|
|
|
|
|
if (img.empty() || (!mask_prob.empty() && img.size() != mask_prob.size()) ||
|
|
|
(!exp_mask1.empty() && img.size() != exp_mask1.size()) ||
|
|
|
(!exp_mask2.empty() && img.size() != exp_mask2.size()) )
|
|
|
{
|
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Rect rect(Point(24, 126), Point(483, 294));
|
|
|
Mat exp_bgdModel, exp_fgdModel;
|
|
|
|
|
|
Mat mask;
|
|
|
Mat bgdModel, fgdModel;
|
|
|
grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT );
|
|
|
bgdModel.copyTo(exp_bgdModel);
|
|
|
fgdModel.copyTo(exp_fgdModel);
|
|
|
grabCut( img, mask, rect, bgdModel, fgdModel, 2, GC_EVAL_FREEZE_MODEL );
|
|
|
|
|
|
|
|
|
if( mask_prob.empty() )
|
|
|
{
|
|
|
mask.copyTo( mask_prob );
|
|
|
imwrite(string(ts->get_data_path()) + "grabcut/mask_prob.png", mask_prob);
|
|
|
}
|
|
|
if( exp_mask1.empty() )
|
|
|
{
|
|
|
exp_mask1 = (mask & 1) * 255;
|
|
|
imwrite(string(ts->get_data_path()) + "grabcut/exp_mask1.png", exp_mask1);
|
|
|
}
|
|
|
if (!verify((mask & 1) * 255, exp_mask1))
|
|
|
{
|
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
double sumBgdModel = cv::sum(cv::abs(bgdModel) - cv::abs(exp_bgdModel))[0];
|
|
|
double sumFgdModel = cv::sum(cv::abs(fgdModel) - cv::abs(exp_fgdModel))[0];
|
|
|
if (sumBgdModel >= 0.1 || sumFgdModel >= 0.1)
|
|
|
{
|
|
|
ts->printf(cvtest::TS::LOG, "sumBgdModel = %f, sumFgdModel = %f\n", sumBgdModel, sumFgdModel);
|
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
mask = mask_prob;
|
|
|
bgdModel.release();
|
|
|
fgdModel.release();
|
|
|
rect = Rect();
|
|
|
grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_MASK );
|
|
|
grabCut( img, mask, rect, bgdModel, fgdModel, 1, GC_EVAL );
|
|
|
|
|
|
if( exp_mask2.empty() )
|
|
|
{
|
|
|
exp_mask2 = (mask & 1) * 255;
|
|
|
imwrite(string(ts->get_data_path()) + "grabcut/exp_mask2.png", exp_mask2);
|
|
|
}
|
|
|
if (!verify((mask & 1) * 255, exp_mask2))
|
|
|
{
|
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
|
|
return;
|
|
|
}
|
|
|
ts->set_failed_test_info(cvtest::TS::OK);
|
|
|
}
|
|
|
|
|
|
TEST(Imgproc_GrabCut, regression) { CV_GrabcutTest test; test.safe_run(); }
|
|
|
|
|
|
TEST(Imgproc_GrabCut, repeatability)
|
|
|
{
|
|
|
cvtest::TS& ts = *cvtest::TS::ptr();
|
|
|
|
|
|
Mat image_1 = imread(string(ts.get_data_path()) + "grabcut/image1652.ppm", IMREAD_COLOR);
|
|
|
Mat mask_1 = imread(string(ts.get_data_path()) + "grabcut/mask1652.ppm", IMREAD_GRAYSCALE);
|
|
|
Rect roi_1(0, 0, 150, 150);
|
|
|
|
|
|
Mat image_2 = image_1.clone();
|
|
|
Mat mask_2 = mask_1.clone();
|
|
|
Rect roi_2 = roi_1;
|
|
|
|
|
|
Mat image_3 = image_1.clone();
|
|
|
Mat mask_3 = mask_1.clone();
|
|
|
Rect roi_3 = roi_1;
|
|
|
|
|
|
Mat bgdModel_1, fgdModel_1;
|
|
|
Mat bgdModel_2, fgdModel_2;
|
|
|
Mat bgdModel_3, fgdModel_3;
|
|
|
|
|
|
theRNG().state = 12378213;
|
|
|
grabCut(image_1, mask_1, roi_1, bgdModel_1, fgdModel_1, 1, GC_INIT_WITH_MASK);
|
|
|
theRNG().state = 12378213;
|
|
|
grabCut(image_2, mask_2, roi_2, bgdModel_2, fgdModel_2, 1, GC_INIT_WITH_MASK);
|
|
|
theRNG().state = 12378213;
|
|
|
grabCut(image_3, mask_3, roi_3, bgdModel_3, fgdModel_3, 1, GC_INIT_WITH_MASK);
|
|
|
|
|
|
EXPECT_EQ(0, countNonZero(mask_1 != mask_2));
|
|
|
EXPECT_EQ(0, countNonZero(mask_1 != mask_3));
|
|
|
EXPECT_EQ(0, countNonZero(mask_2 != mask_3));
|
|
|
}
|
|
|
|
|
|
}}
|
|
|
|