Path: blob/master/modules/imgproc/test/test_grabcut.cpp
16344 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546class CV_GrabcutTest : public cvtest::BaseTest47{48public:49CV_GrabcutTest();50~CV_GrabcutTest();51protected:52bool verify(const Mat& mask, const Mat& exp);53void run(int);54};5556CV_GrabcutTest::CV_GrabcutTest() {}57CV_GrabcutTest::~CV_GrabcutTest() {}5859bool CV_GrabcutTest::verify(const Mat& mask, const Mat& exp)60{61const float maxDiffRatio = 0.005f;62int expArea = countNonZero( exp );63int nonIntersectArea = countNonZero( mask != exp );6465float curRatio = (float)nonIntersectArea / (float)expArea;66ts->printf( cvtest::TS::LOG, "nonIntersectArea/expArea = %f\n", curRatio );67return curRatio < maxDiffRatio;68}6970void CV_GrabcutTest::run( int /* start_from */)71{72cvtest::DefaultRngAuto defRng;7374Mat img = imread(string(ts->get_data_path()) + "shared/airplane.png");75Mat mask_prob = imread(string(ts->get_data_path()) + "grabcut/mask_prob.png", 0);76Mat exp_mask1 = imread(string(ts->get_data_path()) + "grabcut/exp_mask1.png", 0);77Mat exp_mask2 = imread(string(ts->get_data_path()) + "grabcut/exp_mask2.png", 0);7879if (img.empty() || (!mask_prob.empty() && img.size() != mask_prob.size()) ||80(!exp_mask1.empty() && img.size() != exp_mask1.size()) ||81(!exp_mask2.empty() && img.size() != exp_mask2.size()) )82{83ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);84return;85}8687Rect rect(Point(24, 126), Point(483, 294));88Mat exp_bgdModel, exp_fgdModel;8990Mat mask;91Mat bgdModel, fgdModel;92grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT );93bgdModel.copyTo(exp_bgdModel);94fgdModel.copyTo(exp_fgdModel);95grabCut( img, mask, rect, bgdModel, fgdModel, 2, GC_EVAL_FREEZE_MODEL );9697// Multiply images by 255 for more visuality of test data.98if( mask_prob.empty() )99{100mask.copyTo( mask_prob );101imwrite(string(ts->get_data_path()) + "grabcut/mask_prob.png", mask_prob);102}103if( exp_mask1.empty() )104{105exp_mask1 = (mask & 1) * 255;106imwrite(string(ts->get_data_path()) + "grabcut/exp_mask1.png", exp_mask1);107}108if (!verify((mask & 1) * 255, exp_mask1))109{110ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);111return;112}113// The model should not be changed after calling with GC_EVAL_FREEZE_MODEL114double sumBgdModel = cv::sum(cv::abs(bgdModel) - cv::abs(exp_bgdModel))[0];115double sumFgdModel = cv::sum(cv::abs(fgdModel) - cv::abs(exp_fgdModel))[0];116if (sumBgdModel >= 0.1 || sumFgdModel >= 0.1)117{118ts->printf(cvtest::TS::LOG, "sumBgdModel = %f, sumFgdModel = %f\n", sumBgdModel, sumFgdModel);119ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);120return;121}122123mask = mask_prob;124bgdModel.release();125fgdModel.release();126rect = Rect();127grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_MASK );128grabCut( img, mask, rect, bgdModel, fgdModel, 1, GC_EVAL );129130if( exp_mask2.empty() )131{132exp_mask2 = (mask & 1) * 255;133imwrite(string(ts->get_data_path()) + "grabcut/exp_mask2.png", exp_mask2);134}135if (!verify((mask & 1) * 255, exp_mask2))136{137ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);138return;139}140ts->set_failed_test_info(cvtest::TS::OK);141}142143TEST(Imgproc_GrabCut, regression) { CV_GrabcutTest test; test.safe_run(); }144145TEST(Imgproc_GrabCut, repeatability)146{147cvtest::TS& ts = *cvtest::TS::ptr();148149Mat image_1 = imread(string(ts.get_data_path()) + "grabcut/image1652.ppm", IMREAD_COLOR);150Mat mask_1 = imread(string(ts.get_data_path()) + "grabcut/mask1652.ppm", IMREAD_GRAYSCALE);151Rect roi_1(0, 0, 150, 150);152153Mat image_2 = image_1.clone();154Mat mask_2 = mask_1.clone();155Rect roi_2 = roi_1;156157Mat image_3 = image_1.clone();158Mat mask_3 = mask_1.clone();159Rect roi_3 = roi_1;160161Mat bgdModel_1, fgdModel_1;162Mat bgdModel_2, fgdModel_2;163Mat bgdModel_3, fgdModel_3;164165theRNG().state = 12378213;166grabCut(image_1, mask_1, roi_1, bgdModel_1, fgdModel_1, 1, GC_INIT_WITH_MASK);167theRNG().state = 12378213;168grabCut(image_2, mask_2, roi_2, bgdModel_2, fgdModel_2, 1, GC_INIT_WITH_MASK);169theRNG().state = 12378213;170grabCut(image_3, mask_3, roi_3, bgdModel_3, fgdModel_3, 1, GC_INIT_WITH_MASK);171172EXPECT_EQ(0, countNonZero(mask_1 != mask_2));173EXPECT_EQ(0, countNonZero(mask_1 != mask_3));174EXPECT_EQ(0, countNonZero(mask_2 != mask_3));175}176177}} // namespace178179180