test_grabcut.cpp 6.6 KB

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