test_cornerssubpix.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "opencv2/imgproc/imgproc_c.h"
  43. #include "test_chessboardgenerator.hpp"
  44. namespace opencv_test { namespace {
  45. class CV_ChessboardSubpixelTest : public cvtest::BaseTest
  46. {
  47. public:
  48. CV_ChessboardSubpixelTest();
  49. protected:
  50. Mat intrinsic_matrix_;
  51. Mat distortion_coeffs_;
  52. Size image_size_;
  53. void run(int);
  54. void generateIntrinsicParams();
  55. };
  56. int calcDistance(const vector<Point2f>& set1, const vector<Point2f>& set2, double& mean_dist)
  57. {
  58. if(set1.size() != set2.size())
  59. {
  60. return 0;
  61. }
  62. std::vector<int> indices;
  63. double sum_dist = 0.0;
  64. for(size_t i = 0; i < set1.size(); i++)
  65. {
  66. double min_dist = std::numeric_limits<double>::max();
  67. int min_idx = -1;
  68. for(int j = 0; j < (int)set2.size(); j++)
  69. {
  70. double dist = cv::norm(set1[i] - set2[j]); // TODO cvtest
  71. if(dist < min_dist)
  72. {
  73. min_idx = j;
  74. min_dist = dist;
  75. }
  76. }
  77. // check validity of min_idx
  78. if(min_idx == -1)
  79. {
  80. return 0;
  81. }
  82. std::vector<int>::iterator it = std::find(indices.begin(), indices.end(), min_idx);
  83. if(it != indices.end())
  84. {
  85. // there are two points in set1 corresponding to the same point in set2
  86. return 0;
  87. }
  88. indices.push_back(min_idx);
  89. // printf("dist %d = %f\n", (int)i, min_dist);
  90. sum_dist += min_dist*min_dist;
  91. }
  92. mean_dist = sqrt(sum_dist/set1.size());
  93. // printf("sum_dist = %f, set1.size() = %d, mean_dist = %f\n", sum_dist, (int)set1.size(), mean_dist);
  94. return 1;
  95. }
  96. CV_ChessboardSubpixelTest::CV_ChessboardSubpixelTest() :
  97. intrinsic_matrix_(Size(3, 3), CV_64FC1), distortion_coeffs_(Size(1, 4), CV_64FC1),
  98. image_size_(640, 480)
  99. {
  100. }
  101. /* ///////////////////// chess_corner_test ///////////////////////// */
  102. void CV_ChessboardSubpixelTest::run( int )
  103. {
  104. int code = cvtest::TS::OK;
  105. int progress = 0;
  106. RNG& rng = ts->get_rng();
  107. const int runs_count = 20;
  108. const int max_pattern_size = 8;
  109. const int min_pattern_size = 5;
  110. Mat bg(image_size_, CV_8UC1);
  111. bg = Scalar(0);
  112. double sum_dist = 0.0;
  113. int count = 0;
  114. for(int i = 0; i < runs_count; i++)
  115. {
  116. const int pattern_width = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
  117. const int pattern_height = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
  118. Size pattern_size;
  119. if(pattern_width > pattern_height)
  120. {
  121. pattern_size = Size(pattern_height, pattern_width);
  122. }
  123. else
  124. {
  125. pattern_size = Size(pattern_width, pattern_height);
  126. }
  127. ChessBoardGenerator gen_chessboard(Size(pattern_size.width + 1, pattern_size.height + 1));
  128. // generates intrinsic camera and distortion matrices
  129. generateIntrinsicParams();
  130. vector<Point2f> corners;
  131. Mat chessboard_image = gen_chessboard(bg, intrinsic_matrix_, distortion_coeffs_, corners);
  132. vector<Point2f> test_corners;
  133. bool result = findChessboardCorners(chessboard_image, pattern_size, test_corners, 15);
  134. if (!result && cvtest::debugLevel > 0)
  135. {
  136. ts->printf(cvtest::TS::LOG, "Warning: chessboard was not detected! Writing image to test.png\n");
  137. ts->printf(cvtest::TS::LOG, "Size = %d, %d\n", pattern_size.width, pattern_size.height);
  138. ts->printf(cvtest::TS::LOG, "Intrinsic params: fx = %f, fy = %f, cx = %f, cy = %f\n",
  139. intrinsic_matrix_.at<double>(0, 0), intrinsic_matrix_.at<double>(1, 1),
  140. intrinsic_matrix_.at<double>(0, 2), intrinsic_matrix_.at<double>(1, 2));
  141. ts->printf(cvtest::TS::LOG, "Distortion matrix: %f, %f, %f, %f, %f\n",
  142. distortion_coeffs_.at<double>(0, 0), distortion_coeffs_.at<double>(0, 1),
  143. distortion_coeffs_.at<double>(0, 2), distortion_coeffs_.at<double>(0, 3),
  144. distortion_coeffs_.at<double>(0, 4));
  145. imwrite("test.png", chessboard_image);
  146. }
  147. if (!result)
  148. {
  149. continue;
  150. }
  151. double dist1 = 0.0;
  152. int ret = calcDistance(corners, test_corners, dist1);
  153. if(ret == 0)
  154. {
  155. ts->printf(cvtest::TS::LOG, "findChessboardCorners returns invalid corner coordinates!\n");
  156. code = cvtest::TS::FAIL_INVALID_OUTPUT;
  157. break;
  158. }
  159. IplImage chessboard_image_header = cvIplImage(chessboard_image);
  160. cvFindCornerSubPix(&chessboard_image_header, (CvPoint2D32f*)&test_corners[0],
  161. (int)test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));
  162. find4QuadCornerSubpix(chessboard_image, test_corners, Size(5, 5));
  163. double dist2 = 0.0;
  164. ret = calcDistance(corners, test_corners, dist2);
  165. if(ret == 0)
  166. {
  167. ts->printf(cvtest::TS::LOG, "findCornerSubpix returns invalid corner coordinates!\n");
  168. code = cvtest::TS::FAIL_INVALID_OUTPUT;
  169. break;
  170. }
  171. ts->printf(cvtest::TS::LOG, "Error after findChessboardCorners: %f, after findCornerSubPix: %f\n",
  172. dist1, dist2);
  173. sum_dist += dist2;
  174. count++;
  175. const double max_reduce_factor = 0.8;
  176. if(dist1 < dist2*max_reduce_factor)
  177. {
  178. ts->printf(cvtest::TS::LOG, "findCornerSubPix increases average error!\n");
  179. code = cvtest::TS::FAIL_INVALID_OUTPUT;
  180. break;
  181. }
  182. progress = update_progress( progress, i-1, runs_count, 0 );
  183. }
  184. ASSERT_NE(0, count);
  185. sum_dist /= count;
  186. ts->printf(cvtest::TS::LOG, "Average error after findCornerSubpix: %f\n", sum_dist);
  187. if( code < 0 )
  188. ts->set_failed_test_info( code );
  189. }
  190. void CV_ChessboardSubpixelTest::generateIntrinsicParams()
  191. {
  192. RNG& rng = ts->get_rng();
  193. const double max_focus_length = 1000.0;
  194. const double max_focus_diff = 5.0;
  195. double fx = cvtest::randReal(rng)*max_focus_length;
  196. double fy = fx + cvtest::randReal(rng)*max_focus_diff;
  197. double cx = image_size_.width/2;
  198. double cy = image_size_.height/2;
  199. double k1 = 0.5*cvtest::randReal(rng);
  200. double k2 = 0.05*cvtest::randReal(rng);
  201. double p1 = 0.05*cvtest::randReal(rng);
  202. double p2 = 0.05*cvtest::randReal(rng);
  203. double k3 = 0.0;
  204. intrinsic_matrix_ = (Mat_<double>(3, 3) << fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
  205. distortion_coeffs_ = (Mat_<double>(1, 5) << k1, k2, p1, p2, k3);
  206. }
  207. TEST(Calib3d_ChessboardSubPixDetector, accuracy) { CV_ChessboardSubpixelTest test; test.safe_run(); }
  208. TEST(Calib3d_CornerSubPix, regression_7204)
  209. {
  210. cv::Mat image(cv::Size(70, 38), CV_8UC1, cv::Scalar::all(0));
  211. image(cv::Rect(65, 26, 5, 5)).setTo(cv::Scalar::all(255));
  212. image(cv::Rect(55, 31, 8, 1)).setTo(cv::Scalar::all(255));
  213. image(cv::Rect(56, 35, 14, 2)).setTo(cv::Scalar::all(255));
  214. image(cv::Rect(66, 24, 4, 2)).setTo(cv::Scalar::all(255));
  215. image.at<uchar>(24, 69) = 0;
  216. std::vector<cv::Point2f> corners;
  217. corners.push_back(cv::Point2f(65, 30));
  218. cv::cornerSubPix(image, corners, cv::Size(3, 3), cv::Size(-1, -1),
  219. cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
  220. }
  221. }} // namespace
  222. /* End of file. */