test_inpaint.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_InpaintTest : public cvtest::BaseTest
  45. {
  46. public:
  47. CV_InpaintTest();
  48. ~CV_InpaintTest();
  49. protected:
  50. void run(int);
  51. };
  52. CV_InpaintTest::CV_InpaintTest()
  53. {
  54. }
  55. CV_InpaintTest::~CV_InpaintTest() {}
  56. void CV_InpaintTest::run( int )
  57. {
  58. string folder = string(ts->get_data_path()) + "inpaint/";
  59. Mat orig = imread(folder + "orig.png");
  60. Mat exp1 = imread(folder + "exp1.png");
  61. Mat exp2 = imread(folder + "exp2.png");
  62. Mat mask = imread(folder + "mask.png");
  63. if (orig.empty() || exp1.empty() || exp2.empty() || mask.empty())
  64. {
  65. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  66. return;
  67. }
  68. Mat inv_mask;
  69. mask.convertTo(inv_mask, CV_8UC3, -1.0, 255.0);
  70. Mat mask1ch;
  71. cv::cvtColor(mask, mask1ch, COLOR_BGR2GRAY);
  72. Mat test = orig.clone();
  73. test.setTo(Scalar::all(255), mask1ch);
  74. Mat res1, res2;
  75. inpaint( test, mask1ch, res1, 5, INPAINT_NS );
  76. inpaint( test, mask1ch, res2, 5, INPAINT_TELEA );
  77. Mat diff1, diff2;
  78. absdiff( orig, res1, diff1 );
  79. absdiff( orig, res2, diff2 );
  80. double n1 = cvtest::norm(diff1.reshape(1), NORM_INF, inv_mask.reshape(1));
  81. double n2 = cvtest::norm(diff2.reshape(1), NORM_INF, inv_mask.reshape(1));
  82. if (n1 != 0 || n2 != 0)
  83. {
  84. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  85. return;
  86. }
  87. absdiff( exp1, res1, diff1 );
  88. absdiff( exp2, res2, diff2 );
  89. n1 = cvtest::norm(diff1.reshape(1), NORM_INF, mask.reshape(1));
  90. n2 = cvtest::norm(diff2.reshape(1), NORM_INF, mask.reshape(1));
  91. const int jpeg_thres = 3;
  92. if (n1 > jpeg_thres || n2 > jpeg_thres)
  93. {
  94. ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
  95. return;
  96. }
  97. ts->set_failed_test_info(cvtest::TS::OK);
  98. }
  99. TEST(Photo_Inpaint, regression) { CV_InpaintTest test; test.safe_run(); }
  100. typedef testing::TestWithParam<tuple<int> > formats;
  101. TEST_P(formats, 1c)
  102. {
  103. const int type = get<0>(GetParam());
  104. Mat src(100, 100, type);
  105. src.setTo(Scalar::all(128));
  106. Mat ref = src.clone();
  107. Mat dst, mask = Mat::zeros(src.size(), CV_8U);
  108. circle(src, Point(50, 50), 5, Scalar(200), 6);
  109. circle(mask, Point(50, 50), 5, Scalar(200), 6);
  110. inpaint(src, mask, dst, 10, INPAINT_NS);
  111. Mat dst2;
  112. inpaint(src, mask, dst2, 10, INPAINT_TELEA);
  113. ASSERT_LE(cv::norm(dst, ref, NORM_INF), 3.);
  114. ASSERT_LE(cv::norm(dst2, ref, NORM_INF), 3.);
  115. }
  116. INSTANTIATE_TEST_CASE_P(Photo_Inpaint, formats, testing::Values(CV_32F, CV_16U, CV_8U));
  117. TEST(Photo_InpaintBorders, regression)
  118. {
  119. Mat img(64, 64, CV_8U);
  120. img = 128;
  121. img(Rect(0, 0, 16, 64)) = 0;
  122. Mat mask(64, 64, CV_8U);
  123. mask = 0;
  124. mask(Rect(0, 0, 16, 64)) = 255;
  125. Mat inpainted;
  126. inpaint(img, mask, inpainted, 1, INPAINT_TELEA);
  127. Mat diff;
  128. cv::absdiff(inpainted, 128*Mat::ones(inpainted.size(), inpainted.type()), diff);
  129. ASSERT_TRUE(countNonZero(diff) == 0);
  130. }
  131. }} // namespace