test_templmatchmask.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. CV_ENUM(MatchTemplType, CV_TM_CCORR, CV_TM_CCORR_NORMED,
  7. CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED,
  8. CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)
  9. class Imgproc_MatchTemplateWithMask : public TestWithParam<std::tuple<MatType,MatType>>
  10. {
  11. protected:
  12. // Member functions inherited from ::testing::Test
  13. void SetUp() override;
  14. // Matrices for test calculations (always CV_32)
  15. Mat img_;
  16. Mat templ_;
  17. Mat mask_;
  18. Mat templ_masked_;
  19. Mat img_roi_masked_;
  20. // Matrices for call to matchTemplate (have test type)
  21. Mat img_testtype_;
  22. Mat templ_testtype_;
  23. Mat mask_testtype_;
  24. Mat result_;
  25. // Constants
  26. static const Size IMG_SIZE;
  27. static const Size TEMPL_SIZE;
  28. static const Point TEST_POINT;
  29. };
  30. // Arbitraryly chosen test constants
  31. const Size Imgproc_MatchTemplateWithMask::IMG_SIZE(160, 100);
  32. const Size Imgproc_MatchTemplateWithMask::TEMPL_SIZE(21, 13);
  33. const Point Imgproc_MatchTemplateWithMask::TEST_POINT(8, 9);
  34. void Imgproc_MatchTemplateWithMask::SetUp()
  35. {
  36. int type = std::get<0>(GetParam());
  37. int type_mask = std::get<1>(GetParam());
  38. // Matrices are created with the depth to test (for the call to matchTemplate()), but are also
  39. // converted to CV_32 for the test calculations, because matchTemplate() also only operates on
  40. // and returns CV_32.
  41. img_testtype_.create(IMG_SIZE, type);
  42. templ_testtype_.create(TEMPL_SIZE, type);
  43. mask_testtype_.create(TEMPL_SIZE, type_mask);
  44. randu(img_testtype_, 0, 10);
  45. randu(templ_testtype_, 0, 10);
  46. randu(mask_testtype_, 0, 5);
  47. img_testtype_.convertTo(img_, CV_32F);
  48. templ_testtype_.convertTo(templ_, CV_32F);
  49. mask_testtype_.convertTo(mask_, CV_32F);
  50. if (CV_MAT_DEPTH(type_mask) == CV_8U)
  51. {
  52. // CV_8U masks are interpreted as binary masks
  53. mask_.setTo(Scalar::all(1), mask_ != 0);
  54. }
  55. if (mask_.channels() != templ_.channels())
  56. {
  57. std::vector<Mat> mask_channels(templ_.channels(), mask_);
  58. merge(mask_channels.data(), templ_.channels(), mask_);
  59. }
  60. Rect roi(TEST_POINT, TEMPL_SIZE);
  61. img_roi_masked_ = img_(roi).mul(mask_);
  62. templ_masked_ = templ_.mul(mask_);
  63. }
  64. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplSQDIFF)
  65. {
  66. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_SQDIFF, mask_testtype_);
  67. // Naive implementation for one point
  68. Mat temp = img_roi_masked_ - templ_masked_;
  69. Scalar temp_s = sum(temp.mul(temp));
  70. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  71. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  72. }
  73. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplSQDIFF_NORMED)
  74. {
  75. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_SQDIFF_NORMED, mask_testtype_);
  76. // Naive implementation for one point
  77. Mat temp = img_roi_masked_ - templ_masked_;
  78. Scalar temp_s = sum(temp.mul(temp));
  79. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  80. // Normalization
  81. temp_s = sum(templ_masked_.mul(templ_masked_));
  82. double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  83. temp_s = sum(img_roi_masked_.mul(img_roi_masked_));
  84. norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  85. norm = sqrt(norm);
  86. val /= norm;
  87. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  88. }
  89. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCORR)
  90. {
  91. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCORR, mask_testtype_);
  92. // Naive implementation for one point
  93. Scalar temp_s = sum(templ_masked_.mul(img_roi_masked_));
  94. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  95. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  96. }
  97. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCORR_NORMED)
  98. {
  99. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCORR_NORMED, mask_testtype_);
  100. // Naive implementation for one point
  101. Scalar temp_s = sum(templ_masked_.mul(img_roi_masked_));
  102. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  103. // Normalization
  104. temp_s = sum(templ_masked_.mul(templ_masked_));
  105. double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  106. temp_s = sum(img_roi_masked_.mul(img_roi_masked_));
  107. norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  108. norm = sqrt(norm);
  109. val /= norm;
  110. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  111. }
  112. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCOEFF)
  113. {
  114. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCOEFF, mask_testtype_);
  115. // Naive implementation for one point
  116. Scalar temp_s = sum(mask_);
  117. for (int i = 0; i < 4; i++)
  118. {
  119. if (temp_s[i] != 0.0)
  120. temp_s[i] = 1.0 / temp_s[i];
  121. else
  122. temp_s[i] = 1.0;
  123. }
  124. Mat temp = mask_.clone(); temp = temp_s; // Workaround to multiply Mat by Scalar
  125. Mat temp2 = mask_.clone(); temp2 = sum(templ_masked_); // Workaround to multiply Mat by Scalar
  126. Mat templx = templ_masked_ - mask_.mul(temp).mul(temp2);
  127. temp2 = sum(img_roi_masked_); // Workaround to multiply Mat by Scalar
  128. Mat imgx = img_roi_masked_ - mask_.mul(temp).mul(temp2);
  129. temp_s = sum(templx.mul(imgx));
  130. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  131. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  132. }
  133. TEST_P(Imgproc_MatchTemplateWithMask, CompareNaiveImplCCOEFF_NORMED)
  134. {
  135. matchTemplate(img_testtype_, templ_testtype_, result_, CV_TM_CCOEFF_NORMED, mask_testtype_);
  136. // Naive implementation for one point
  137. Scalar temp_s = sum(mask_);
  138. for (int i = 0; i < 4; i++)
  139. {
  140. if (temp_s[i] != 0.0)
  141. temp_s[i] = 1.0 / temp_s[i];
  142. else
  143. temp_s[i] = 1.0;
  144. }
  145. Mat temp = mask_.clone(); temp = temp_s; // Workaround to multiply Mat by Scalar
  146. Mat temp2 = mask_.clone(); temp2 = sum(templ_masked_); // Workaround to multiply Mat by Scalar
  147. Mat templx = templ_masked_ - mask_.mul(temp).mul(temp2);
  148. temp2 = sum(img_roi_masked_); // Workaround to multiply Mat by Scalar
  149. Mat imgx = img_roi_masked_ - mask_.mul(temp).mul(temp2);
  150. temp_s = sum(templx.mul(imgx));
  151. double val = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  152. // Normalization
  153. temp_s = sum(templx.mul(templx));
  154. double norm = temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  155. temp_s = sum(imgx.mul(imgx));
  156. norm *= temp_s[0] + temp_s[1] + temp_s[2] + temp_s[3];
  157. norm = sqrt(norm);
  158. val /= norm;
  159. EXPECT_NEAR(val, result_.at<float>(TEST_POINT), TEMPL_SIZE.area()*abs(val)*FLT_EPSILON);
  160. }
  161. INSTANTIATE_TEST_CASE_P(SingleChannelMask, Imgproc_MatchTemplateWithMask,
  162. Combine(
  163. Values(CV_32FC1, CV_32FC3, CV_8UC1, CV_8UC3),
  164. Values(CV_32FC1, CV_8UC1)));
  165. INSTANTIATE_TEST_CASE_P(MultiChannelMask, Imgproc_MatchTemplateWithMask,
  166. Combine(
  167. Values(CV_32FC3, CV_8UC3),
  168. Values(CV_32FC3, CV_8UC3)));
  169. class Imgproc_MatchTemplateWithMask2 : public TestWithParam<std::tuple<MatType,MatType,
  170. MatchTemplType>>
  171. {
  172. protected:
  173. // Member functions inherited from ::testing::Test
  174. void SetUp() override;
  175. // Data members
  176. Mat img_;
  177. Mat templ_;
  178. Mat mask_;
  179. Mat result_withoutmask_;
  180. Mat result_withmask_;
  181. // Constants
  182. static const Size IMG_SIZE;
  183. static const Size TEMPL_SIZE;
  184. };
  185. // Arbitraryly chosen test constants
  186. const Size Imgproc_MatchTemplateWithMask2::IMG_SIZE(160, 100);
  187. const Size Imgproc_MatchTemplateWithMask2::TEMPL_SIZE(21, 13);
  188. void Imgproc_MatchTemplateWithMask2::SetUp()
  189. {
  190. int type = std::get<0>(GetParam());
  191. int type_mask = std::get<1>(GetParam());
  192. img_.create(IMG_SIZE, type);
  193. templ_.create(TEMPL_SIZE, type);
  194. mask_.create(TEMPL_SIZE, type_mask);
  195. randu(img_, 0, 100);
  196. randu(templ_, 0, 100);
  197. if (CV_MAT_DEPTH(type_mask) == CV_8U)
  198. {
  199. // CV_8U implies binary mask, so all nonzero values should work
  200. randu(mask_, 1, 255);
  201. }
  202. else
  203. {
  204. mask_ = Scalar(1, 1, 1, 1);
  205. }
  206. }
  207. TEST_P(Imgproc_MatchTemplateWithMask2, CompareWithAndWithoutMask)
  208. {
  209. int method = std::get<2>(GetParam());
  210. matchTemplate(img_, templ_, result_withmask_, method, mask_);
  211. matchTemplate(img_, templ_, result_withoutmask_, method);
  212. // Get maximum result for relative error calculation
  213. double min_val, max_val;
  214. minMaxLoc(abs(result_withmask_), &min_val, &max_val);
  215. // Get maximum of absolute diff for comparison
  216. double mindiff, maxdiff;
  217. minMaxLoc(abs(result_withmask_ - result_withoutmask_), &mindiff, &maxdiff);
  218. EXPECT_LT(maxdiff, max_val*TEMPL_SIZE.area()*FLT_EPSILON);
  219. }
  220. INSTANTIATE_TEST_CASE_P(SingleChannelMask, Imgproc_MatchTemplateWithMask2,
  221. Combine(
  222. Values(CV_32FC1, CV_32FC3, CV_8UC1, CV_8UC3),
  223. Values(CV_32FC1, CV_8UC1),
  224. Values(CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED,
  225. CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)));
  226. INSTANTIATE_TEST_CASE_P(MultiChannelMask, Imgproc_MatchTemplateWithMask2,
  227. Combine(
  228. Values(CV_32FC3, CV_8UC3),
  229. Values(CV_32FC3, CV_8UC3),
  230. Values(CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED,
  231. CV_TM_CCOEFF, CV_TM_CCOEFF_NORMED)));
  232. }} // namespace