test_cloning.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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) 2013, OpenCV Foundation, 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 documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // 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 damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. #define OUTPUT_SAVING 0
  44. #if OUTPUT_SAVING
  45. #define SAVE(x) std::vector<int> params;\
  46. params.push_back(16);\
  47. params.push_back(0);\
  48. imwrite(folder + "output.png", x ,params);
  49. #else
  50. #define SAVE(x)
  51. #endif
  52. static const double numerical_precision = 0.05; // 95% of pixels should have exact values
  53. TEST(Photo_SeamlessClone_normal, regression)
  54. {
  55. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/Normal_Cloning/";
  56. string original_path1 = folder + "source1.png";
  57. string original_path2 = folder + "destination1.png";
  58. string original_path3 = folder + "mask.png";
  59. string reference_path = folder + "reference.png";
  60. Mat source = imread(original_path1, IMREAD_COLOR);
  61. Mat destination = imread(original_path2, IMREAD_COLOR);
  62. Mat mask = imread(original_path3, IMREAD_COLOR);
  63. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  64. ASSERT_FALSE(destination.empty()) << "Could not load destination image " << original_path2;
  65. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path3;
  66. Mat result;
  67. Point p;
  68. p.x = destination.size().width/2;
  69. p.y = destination.size().height/2;
  70. seamlessClone(source, destination, mask, p, result, 1);
  71. Mat reference = imread(reference_path);
  72. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  73. SAVE(result);
  74. double errorINF = cvtest::norm(reference, result, NORM_INF);
  75. EXPECT_LE(errorINF, 1);
  76. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  77. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  78. mask = Scalar(0, 0, 0);
  79. seamlessClone(source, destination, mask, p, result, 1);
  80. reference = destination;
  81. errorINF = cvtest::norm(reference, result, NORM_INF);
  82. EXPECT_LE(errorINF, 1);
  83. errorL1 = cvtest::norm(reference, result, NORM_L1);
  84. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  85. }
  86. TEST(Photo_SeamlessClone_mixed, regression)
  87. {
  88. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/Mixed_Cloning/";
  89. string original_path1 = folder + "source1.png";
  90. string original_path2 = folder + "destination1.png";
  91. string original_path3 = folder + "mask.png";
  92. string reference_path = folder + "reference.png";
  93. Mat source = imread(original_path1, IMREAD_COLOR);
  94. Mat destination = imread(original_path2, IMREAD_COLOR);
  95. Mat mask = imread(original_path3, IMREAD_COLOR);
  96. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  97. ASSERT_FALSE(destination.empty()) << "Could not load destination image " << original_path2;
  98. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path3;
  99. Mat result;
  100. Point p;
  101. p.x = destination.size().width/2;
  102. p.y = destination.size().height/2;
  103. seamlessClone(source, destination, mask, p, result, 2);
  104. SAVE(result);
  105. Mat reference = imread(reference_path);
  106. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  107. double errorINF = cvtest::norm(reference, result, NORM_INF);
  108. EXPECT_LE(errorINF, 1);
  109. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  110. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  111. }
  112. TEST(Photo_SeamlessClone_featureExchange, regression)
  113. {
  114. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/Monochrome_Transfer/";
  115. string original_path1 = folder + "source1.png";
  116. string original_path2 = folder + "destination1.png";
  117. string original_path3 = folder + "mask.png";
  118. string reference_path = folder + "reference.png";
  119. Mat source = imread(original_path1, IMREAD_COLOR);
  120. Mat destination = imread(original_path2, IMREAD_COLOR);
  121. Mat mask = imread(original_path3, IMREAD_COLOR);
  122. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  123. ASSERT_FALSE(destination.empty()) << "Could not load destination image " << original_path2;
  124. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path3;
  125. Mat result;
  126. Point p;
  127. p.x = destination.size().width/2;
  128. p.y = destination.size().height/2;
  129. seamlessClone(source, destination, mask, p, result, 3);
  130. SAVE(result);
  131. Mat reference = imread(reference_path);
  132. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  133. double errorINF = cvtest::norm(reference, result, NORM_INF);
  134. EXPECT_LE(errorINF, 1);
  135. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  136. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  137. }
  138. TEST(Photo_SeamlessClone_colorChange, regression)
  139. {
  140. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/color_change/";
  141. string original_path1 = folder + "source1.png";
  142. string original_path2 = folder + "mask.png";
  143. string reference_path = folder + "reference.png";
  144. Mat source = imread(original_path1, IMREAD_COLOR);
  145. Mat mask = imread(original_path2, IMREAD_COLOR);
  146. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  147. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path2;
  148. Mat result;
  149. colorChange(source, mask, result, 1.5, .5, .5);
  150. SAVE(result);
  151. Mat reference = imread(reference_path);
  152. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  153. double errorINF = cvtest::norm(reference, result, NORM_INF);
  154. EXPECT_LE(errorINF, 1);
  155. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  156. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  157. }
  158. TEST(Photo_SeamlessClone_illuminationChange, regression)
  159. {
  160. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/Illumination_Change/";
  161. string original_path1 = folder + "source1.png";
  162. string original_path2 = folder + "mask.png";
  163. string reference_path = folder + "reference.png";
  164. Mat source = imread(original_path1, IMREAD_COLOR);
  165. Mat mask = imread(original_path2, IMREAD_COLOR);
  166. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  167. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path2;
  168. Mat result;
  169. illuminationChange(source, mask, result, 0.2f, 0.4f);
  170. SAVE(result);
  171. Mat reference = imread(reference_path);
  172. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  173. double errorINF = cvtest::norm(reference, result, NORM_INF);
  174. EXPECT_LE(errorINF, 1);
  175. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  176. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  177. }
  178. TEST(Photo_SeamlessClone_textureFlattening, regression)
  179. {
  180. string folder = string(cvtest::TS::ptr()->get_data_path()) + "cloning/Texture_Flattening/";
  181. string original_path1 = folder + "source1.png";
  182. string original_path2 = folder + "mask.png";
  183. string reference_path = folder + "reference.png";
  184. Mat source = imread(original_path1, IMREAD_COLOR);
  185. Mat mask = imread(original_path2, IMREAD_COLOR);
  186. ASSERT_FALSE(source.empty()) << "Could not load source image " << original_path1;
  187. ASSERT_FALSE(mask.empty()) << "Could not load mask image " << original_path2;
  188. Mat result;
  189. textureFlattening(source, mask, result, 30, 45, 3);
  190. SAVE(result);
  191. Mat reference = imread(reference_path);
  192. ASSERT_FALSE(reference.empty()) << "Could not load reference image " << reference_path;
  193. double errorINF = cvtest::norm(reference, result, NORM_INF);
  194. EXPECT_LE(errorINF, 1);
  195. double errorL1 = cvtest::norm(reference, result, NORM_L1);
  196. EXPECT_LE(errorL1, reference.total() * numerical_precision) << "size=" << reference.size();
  197. }
  198. }} // namespace