test_modelest.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, 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 Intel Corporation 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. #if 0
  43. #include "_modelest.h"
  44. using namespace std;
  45. using namespace cv;
  46. class BareModelEstimator : public CvModelEstimator2
  47. {
  48. public:
  49. BareModelEstimator(int modelPoints, CvSize modelSize, int maxBasicSolutions);
  50. virtual int runKernel( const CvMat*, const CvMat*, CvMat* );
  51. virtual void computeReprojError( const CvMat*, const CvMat*,
  52. const CvMat*, CvMat* );
  53. bool checkSubsetPublic( const CvMat* ms1, int count, bool checkPartialSubset );
  54. };
  55. BareModelEstimator::BareModelEstimator(int _modelPoints, CvSize _modelSize, int _maxBasicSolutions)
  56. :CvModelEstimator2(_modelPoints, _modelSize, _maxBasicSolutions)
  57. {
  58. }
  59. int BareModelEstimator::runKernel( const CvMat*, const CvMat*, CvMat* )
  60. {
  61. return 0;
  62. }
  63. void BareModelEstimator::computeReprojError( const CvMat*, const CvMat*,
  64. const CvMat*, CvMat* )
  65. {
  66. }
  67. bool BareModelEstimator::checkSubsetPublic( const CvMat* ms1, int count, bool checkPartialSubset )
  68. {
  69. checkPartialSubsets = checkPartialSubset;
  70. return checkSubset(ms1, count);
  71. }
  72. class CV_ModelEstimator2_Test : public cvtest::ArrayTest
  73. {
  74. public:
  75. CV_ModelEstimator2_Test();
  76. protected:
  77. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  78. void fill_array( int test_case_idx, int i, int j, Mat& arr );
  79. double get_success_error_level( int test_case_idx, int i, int j );
  80. void run_func();
  81. void prepare_to_validation( int test_case_idx );
  82. bool checkPartialSubsets;
  83. int usedPointsCount;
  84. bool checkSubsetResult;
  85. int generalPositionsCount;
  86. int maxPointsCount;
  87. };
  88. CV_ModelEstimator2_Test::CV_ModelEstimator2_Test()
  89. {
  90. generalPositionsCount = get_test_case_count() / 2;
  91. maxPointsCount = 100;
  92. test_array[INPUT].push_back(NULL);
  93. test_array[OUTPUT].push_back(NULL);
  94. test_array[REF_OUTPUT].push_back(NULL);
  95. }
  96. void CV_ModelEstimator2_Test::get_test_array_types_and_sizes( int /*test_case_idx*/,
  97. vector<vector<Size> > &sizes, vector<vector<int> > &types )
  98. {
  99. RNG &rng = ts->get_rng();
  100. checkPartialSubsets = (cvtest::randInt(rng) % 2 == 0);
  101. int pointsCount = cvtest::randInt(rng) % maxPointsCount;
  102. usedPointsCount = pointsCount == 0 ? 0 : cvtest::randInt(rng) % pointsCount;
  103. sizes[INPUT][0] = cvSize(1, pointsCount);
  104. types[INPUT][0] = CV_64FC2;
  105. sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(1, 1);
  106. types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_8UC1;
  107. }
  108. void CV_ModelEstimator2_Test::fill_array( int test_case_idx, int i, int j, Mat& arr )
  109. {
  110. if( i != INPUT )
  111. {
  112. cvtest::ArrayTest::fill_array( test_case_idx, i, j, arr );
  113. return;
  114. }
  115. if (test_case_idx < generalPositionsCount)
  116. {
  117. //generate points in a general position (i.e. no three points can lie on the same line.)
  118. bool isGeneralPosition;
  119. do
  120. {
  121. ArrayTest::fill_array(test_case_idx, i, j, arr);
  122. //a simple check that the position is general:
  123. // for each line check that all other points don't belong to it
  124. isGeneralPosition = true;
  125. for (int startPointIndex = 0; startPointIndex < usedPointsCount && isGeneralPosition; startPointIndex++)
  126. {
  127. for (int endPointIndex = startPointIndex + 1; endPointIndex < usedPointsCount && isGeneralPosition; endPointIndex++)
  128. {
  129. for (int testPointIndex = 0; testPointIndex < usedPointsCount && isGeneralPosition; testPointIndex++)
  130. {
  131. if (testPointIndex == startPointIndex || testPointIndex == endPointIndex)
  132. {
  133. continue;
  134. }
  135. CV_Assert(arr.type() == CV_64FC2);
  136. Point2d tangentVector_1 = arr.at<Point2d>(endPointIndex) - arr.at<Point2d>(startPointIndex);
  137. Point2d tangentVector_2 = arr.at<Point2d>(testPointIndex) - arr.at<Point2d>(startPointIndex);
  138. const float eps = 1e-4f;
  139. //TODO: perhaps it is better to normalize the cross product by norms of the tangent vectors
  140. if (fabs(tangentVector_1.cross(tangentVector_2)) < eps)
  141. {
  142. isGeneralPosition = false;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. while(!isGeneralPosition);
  149. }
  150. else
  151. {
  152. //create points in a degenerate position (there are at least 3 points belonging to the same line)
  153. ArrayTest::fill_array(test_case_idx, i, j, arr);
  154. if (usedPointsCount <= 2)
  155. {
  156. return;
  157. }
  158. RNG &rng = ts->get_rng();
  159. int startPointIndex, endPointIndex, modifiedPointIndex;
  160. do
  161. {
  162. startPointIndex = cvtest::randInt(rng) % usedPointsCount;
  163. endPointIndex = cvtest::randInt(rng) % usedPointsCount;
  164. modifiedPointIndex = checkPartialSubsets ? usedPointsCount - 1 : cvtest::randInt(rng) % usedPointsCount;
  165. }
  166. while (startPointIndex == endPointIndex || startPointIndex == modifiedPointIndex || endPointIndex == modifiedPointIndex);
  167. double startWeight = cvtest::randReal(rng);
  168. CV_Assert(arr.type() == CV_64FC2);
  169. arr.at<Point2d>(modifiedPointIndex) = startWeight * arr.at<Point2d>(startPointIndex) + (1.0 - startWeight) * arr.at<Point2d>(endPointIndex);
  170. }
  171. }
  172. double CV_ModelEstimator2_Test::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  173. {
  174. return 0;
  175. }
  176. void CV_ModelEstimator2_Test::prepare_to_validation( int test_case_idx )
  177. {
  178. test_mat[OUTPUT][0].at<uchar>(0) = checkSubsetResult;
  179. test_mat[REF_OUTPUT][0].at<uchar>(0) = test_case_idx < generalPositionsCount || usedPointsCount <= 2;
  180. }
  181. void CV_ModelEstimator2_Test::run_func()
  182. {
  183. //make the input continuous
  184. Mat input = test_mat[INPUT][0].clone();
  185. CvMat _input = input;
  186. RNG &rng = ts->get_rng();
  187. int modelPoints = cvtest::randInt(rng);
  188. CvSize modelSize = cvSize(2, modelPoints);
  189. int maxBasicSolutions = cvtest::randInt(rng);
  190. BareModelEstimator modelEstimator(modelPoints, modelSize, maxBasicSolutions);
  191. checkSubsetResult = modelEstimator.checkSubsetPublic(&_input, usedPointsCount, checkPartialSubsets);
  192. }
  193. TEST(Calib3d_ModelEstimator2, accuracy) { CV_ModelEstimator2_Test test; test.safe_run(); }
  194. #endif