test_brute_force_matcher.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
  14. // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
  15. // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // @Authors
  19. // Niko Li, newlife20080214@gmail.com
  20. // Jia Haipeng, jiahaipeng95@gmail.com
  21. // Zero Lin, Zero.Lin@amd.com
  22. // Zhang Ying, zhangying913@gmail.com
  23. // Yao Wang, bitwangyaoyao@gmail.com
  24. //
  25. // Redistribution and use in source and binary forms, with or without modification,
  26. // are permitted provided that the following conditions are met:
  27. //
  28. // * Redistribution's of source code must retain the above copyright notice,
  29. // this list of conditions and the following disclaimer.
  30. //
  31. // * Redistribution's in binary form must reproduce the above copyright notice,
  32. // this list of conditions and the following disclaimer in the documentation
  33. // and/or other materials provided with the distribution.
  34. //
  35. // * The name of the copyright holders may not be used to endorse or promote products
  36. // derived from this software without specific prior written permission.
  37. //
  38. // This software is provided by the copyright holders and contributors "as is" and
  39. // any express or implied warranties, including, but not limited to, the implied
  40. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  41. // In no event shall the Intel Corporation or contributors be liable for any direct,
  42. // indirect, incidental, special, exemplary, or consequential damages
  43. // (including, but not limited to, procurement of substitute goods or services;
  44. // loss of use, data, or profits; or business interruption) however caused
  45. // and on any theory of liability, whether in contract, strict liability,
  46. // or tort (including negligence or otherwise) arising in any way out of
  47. // the use of this software, even if advised of the possibility of such damage.
  48. //
  49. //M*/
  50. #include "../test_precomp.hpp"
  51. #include "cvconfig.h"
  52. #include "opencv2/ts/ocl_test.hpp"
  53. #ifdef HAVE_OPENCL
  54. namespace opencv_test {
  55. namespace ocl {
  56. PARAM_TEST_CASE(BruteForceMatcher, int, int)
  57. {
  58. int distType;
  59. int dim;
  60. int queryDescCount;
  61. int countFactor;
  62. Mat query, train;
  63. UMat uquery, utrain;
  64. virtual void SetUp()
  65. {
  66. distType = GET_PARAM(0);
  67. dim = GET_PARAM(1);
  68. queryDescCount = 300; // must be even number because we split train data in some cases in two
  69. countFactor = 4; // do not change it
  70. cv::Mat queryBuf, trainBuf;
  71. // Generate query descriptors randomly.
  72. // Descriptor vector elements are integer values.
  73. queryBuf.create(queryDescCount, dim, CV_32SC1);
  74. rng.fill(queryBuf, cv::RNG::UNIFORM, cv::Scalar::all(0), cv::Scalar::all(3));
  75. queryBuf.convertTo(queryBuf, CV_32FC1);
  76. // Generate train descriptors as follows:
  77. // copy each query descriptor to train set countFactor times
  78. // and perturb some one element of the copied descriptors in
  79. // in ascending order. General boundaries of the perturbation
  80. // are (0.f, 1.f).
  81. trainBuf.create(queryDescCount * countFactor, dim, CV_32FC1);
  82. float step = 1.f / countFactor;
  83. for (int qIdx = 0; qIdx < queryDescCount; qIdx++)
  84. {
  85. cv::Mat queryDescriptor = queryBuf.row(qIdx);
  86. for (int c = 0; c < countFactor; c++)
  87. {
  88. int tIdx = qIdx * countFactor + c;
  89. cv::Mat trainDescriptor = trainBuf.row(tIdx);
  90. queryDescriptor.copyTo(trainDescriptor);
  91. int elem = rng(dim);
  92. float diff = rng.uniform(step * c, step * (c + 1));
  93. trainDescriptor.at<float>(0, elem) += diff;
  94. }
  95. }
  96. queryBuf.convertTo(query, CV_32F);
  97. trainBuf.convertTo(train, CV_32F);
  98. query.copyTo(uquery);
  99. train.copyTo(utrain);
  100. }
  101. };
  102. #ifdef __ANDROID__
  103. OCL_TEST_P(BruteForceMatcher, DISABLED_Match_Single)
  104. #else
  105. OCL_TEST_P(BruteForceMatcher, Match_Single)
  106. #endif
  107. {
  108. BFMatcher matcher(distType);
  109. std::vector<cv::DMatch> matches;
  110. matcher.match(uquery, utrain, matches);
  111. ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
  112. int badCount = 0;
  113. for (size_t i = 0; i < matches.size(); i++)
  114. {
  115. cv::DMatch match = matches[i];
  116. if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
  117. badCount++;
  118. }
  119. ASSERT_EQ(0, badCount);
  120. }
  121. #ifdef __ANDROID__
  122. OCL_TEST_P(BruteForceMatcher, DISABLED_KnnMatch_2_Single)
  123. #else
  124. OCL_TEST_P(BruteForceMatcher, KnnMatch_2_Single)
  125. #endif
  126. {
  127. const int knn = 2;
  128. BFMatcher matcher(distType);
  129. std::vector< std::vector<cv::DMatch> > matches;
  130. matcher.knnMatch(uquery, utrain, matches, knn);
  131. ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
  132. int badCount = 0;
  133. for (size_t i = 0; i < matches.size(); i++)
  134. {
  135. if ((int)matches[i].size() != knn)
  136. badCount++;
  137. else
  138. {
  139. int localBadCount = 0;
  140. for (int k = 0; k < knn; k++)
  141. {
  142. cv::DMatch match = matches[i][k];
  143. if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor + k) || (match.imgIdx != 0))
  144. localBadCount++;
  145. }
  146. badCount += localBadCount > 0 ? 1 : 0;
  147. }
  148. }
  149. ASSERT_EQ(0, badCount);
  150. }
  151. #ifdef __ANDROID__
  152. OCL_TEST_P(BruteForceMatcher, DISABLED_RadiusMatch_Single)
  153. #else
  154. OCL_TEST_P(BruteForceMatcher, RadiusMatch_Single)
  155. #endif
  156. {
  157. float radius = 1.f / countFactor;
  158. BFMatcher matcher(distType);
  159. std::vector< std::vector<cv::DMatch> > matches;
  160. matcher.radiusMatch(uquery, utrain, matches, radius);
  161. ASSERT_EQ(static_cast<size_t>(queryDescCount), matches.size());
  162. int badCount = 0;
  163. for (size_t i = 0; i < matches.size(); i++)
  164. {
  165. if ((int)matches[i].size() != 1)
  166. {
  167. badCount++;
  168. }
  169. else
  170. {
  171. cv::DMatch match = matches[i][0];
  172. if ((match.queryIdx != (int)i) || (match.trainIdx != (int)i * countFactor) || (match.imgIdx != 0))
  173. badCount++;
  174. }
  175. }
  176. ASSERT_EQ(0, badCount);
  177. }
  178. OCL_INSTANTIATE_TEST_CASE_P(Matcher, BruteForceMatcher, Combine( Values((int)NORM_L1, (int)NORM_L2),
  179. Values(57, 64, 83, 128, 179, 256, 304) ) );
  180. }//ocl
  181. }//cvtest
  182. #endif //HAVE_OPENCL