test_chessboardgenerator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #include "test_chessboardgenerator.hpp"
  44. namespace cv {
  45. ChessBoardGenerator::ChessBoardGenerator(const Size& _patternSize) : sensorWidth(32), sensorHeight(24),
  46. squareEdgePointsNum(200), min_cos(std::sqrt(3.f)*0.5f), cov(0.5),
  47. patternSize(_patternSize), rendererResolutionMultiplier(4), tvec(Mat::zeros(1, 3, CV_32F))
  48. {
  49. rvec.create(3, 1, CV_32F);
  50. cvtest::Rodrigues(Mat::eye(3, 3, CV_32F), rvec);
  51. }
  52. void ChessBoardGenerator::generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const
  53. {
  54. Point3f step = (p2 - p1) * (1.f/squareEdgePointsNum);
  55. for(size_t n = 0; n < squareEdgePointsNum; ++n)
  56. out.push_back( p1 + step * (float)n);
  57. }
  58. Size ChessBoardGenerator::cornersSize() const
  59. {
  60. return Size(patternSize.width-1, patternSize.height-1);
  61. }
  62. struct Mult
  63. {
  64. float m;
  65. Mult(int mult) : m((float)mult) {}
  66. Point2f operator()(const Point2f& p)const { return p * m; }
  67. };
  68. void ChessBoardGenerator::generateBasis(Point3f& pb1, Point3f& pb2) const
  69. {
  70. RNG& rng = theRNG();
  71. Vec3f n;
  72. for(;;)
  73. {
  74. n[0] = rng.uniform(-1.f, 1.f);
  75. n[1] = rng.uniform(-1.f, 1.f);
  76. n[2] = rng.uniform(0.0f, 1.f);
  77. float len = (float)norm(n);
  78. if (len < 1e-3)
  79. continue;
  80. n[0]/=len;
  81. n[1]/=len;
  82. n[2]/=len;
  83. if (n[2] > min_cos)
  84. break;
  85. }
  86. Vec3f n_temp = n; n_temp[0] += 100;
  87. Vec3f b1 = n.cross(n_temp);
  88. Vec3f b2 = n.cross(b1);
  89. float len_b1 = (float)norm(b1);
  90. float len_b2 = (float)norm(b2);
  91. pb1 = Point3f(b1[0]/len_b1, b1[1]/len_b1, b1[2]/len_b1);
  92. pb2 = Point3f(b2[0]/len_b1, b2[1]/len_b2, b2[2]/len_b2);
  93. }
  94. Mat ChessBoardGenerator::generateChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
  95. const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
  96. float sqWidth, float sqHeight, const vector<Point3f>& whole,
  97. vector<Point2f>& corners) const
  98. {
  99. vector< vector<Point> > squares_black;
  100. for(int i = 0; i < patternSize.width; ++i)
  101. for(int j = 0; j < patternSize.height; ++j)
  102. if ( (i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0) )
  103. {
  104. vector<Point3f> pts_square3d;
  105. vector<Point2f> pts_square2d;
  106. Point3f p1 = zero + (i + 0) * sqWidth * pb1 + (j + 0) * sqHeight * pb2;
  107. Point3f p2 = zero + (i + 1) * sqWidth * pb1 + (j + 0) * sqHeight * pb2;
  108. Point3f p3 = zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2;
  109. Point3f p4 = zero + (i + 0) * sqWidth * pb1 + (j + 1) * sqHeight * pb2;
  110. generateEdge(p1, p2, pts_square3d);
  111. generateEdge(p2, p3, pts_square3d);
  112. generateEdge(p3, p4, pts_square3d);
  113. generateEdge(p4, p1, pts_square3d);
  114. projectPoints(pts_square3d, rvec, tvec, camMat, distCoeffs, pts_square2d);
  115. squares_black.resize(squares_black.size() + 1);
  116. vector<Point2f> temp;
  117. approxPolyDP(pts_square2d, temp, 1.0, true);
  118. transform(temp.begin(), temp.end(), back_inserter(squares_black.back()), Mult(rendererResolutionMultiplier));
  119. }
  120. /* calculate corners */
  121. corners3d.clear();
  122. for(int j = 0; j < patternSize.height - 1; ++j)
  123. for(int i = 0; i < patternSize.width - 1; ++i)
  124. corners3d.push_back(zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2);
  125. corners.clear();
  126. projectPoints(corners3d, rvec, tvec, camMat, distCoeffs, corners);
  127. vector<Point3f> whole3d;
  128. vector<Point2f> whole2d;
  129. generateEdge(whole[0], whole[1], whole3d);
  130. generateEdge(whole[1], whole[2], whole3d);
  131. generateEdge(whole[2], whole[3], whole3d);
  132. generateEdge(whole[3], whole[0], whole3d);
  133. projectPoints(whole3d, rvec, tvec, camMat, distCoeffs, whole2d);
  134. vector<Point2f> temp_whole2d;
  135. approxPolyDP(whole2d, temp_whole2d, 1.0, true);
  136. vector< vector<Point > > whole_contour(1);
  137. transform(temp_whole2d.begin(), temp_whole2d.end(),
  138. back_inserter(whole_contour.front()), Mult(rendererResolutionMultiplier));
  139. Mat result;
  140. if (rendererResolutionMultiplier == 1)
  141. {
  142. result = bg.clone();
  143. drawContours(result, whole_contour, -1, Scalar::all(255), FILLED, LINE_AA);
  144. drawContours(result, squares_black, -1, Scalar::all(0), FILLED, LINE_AA);
  145. }
  146. else
  147. {
  148. Mat tmp;
  149. resize(bg, tmp, bg.size() * rendererResolutionMultiplier, 0, 0, INTER_LINEAR_EXACT);
  150. drawContours(tmp, whole_contour, -1, Scalar::all(255), FILLED, LINE_AA);
  151. drawContours(tmp, squares_black, -1, Scalar::all(0), FILLED, LINE_AA);
  152. resize(tmp, result, bg.size(), 0, 0, INTER_AREA);
  153. }
  154. return result;
  155. }
  156. Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const
  157. {
  158. cov = std::min(cov, 0.8);
  159. double fovx, fovy, focalLen;
  160. Point2d principalPoint;
  161. double aspect;
  162. calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
  163. fovx, fovy, focalLen, principalPoint, aspect);
  164. RNG& rng = theRNG();
  165. float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
  166. float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180);
  167. float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
  168. Point3f p;
  169. p.z = cos(ah) * d1;
  170. p.x = sin(ah) * d1;
  171. p.y = p.z * tan(av);
  172. Point3f pb1, pb2;
  173. generateBasis(pb1, pb2);
  174. float cbHalfWidth = static_cast<float>(norm(p) * sin( std::min(fovx, fovy) * 0.5 * CV_PI / 180));
  175. float cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
  176. float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
  177. float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
  178. vector<Point3f> pts3d(4);
  179. vector<Point2f> pts2d(4);
  180. for(;;)
  181. {
  182. pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  183. pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  184. pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  185. pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  186. /* can remake with better perf */
  187. projectPoints(pts3d, rvec, tvec, camMat, distCoeffs, pts2d);
  188. bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0;
  189. bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0;
  190. bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0;
  191. bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0;
  192. if (inrect1 && inrect2 && inrect3 && inrect4)
  193. break;
  194. cbHalfWidth*=0.8f;
  195. cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
  196. cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
  197. cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
  198. }
  199. Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
  200. float sqWidth = 2 * cbHalfWidth/patternSize.width;
  201. float sqHeight = 2 * cbHalfHeight/patternSize.height;
  202. return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2, sqWidth, sqHeight, pts3d, corners);
  203. }
  204. Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
  205. const Size2f& squareSize, vector<Point2f>& corners) const
  206. {
  207. cov = std::min(cov, 0.8);
  208. double fovx, fovy, focalLen;
  209. Point2d principalPoint;
  210. double aspect;
  211. calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
  212. fovx, fovy, focalLen, principalPoint, aspect);
  213. RNG& rng = theRNG();
  214. float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
  215. float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180);
  216. float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
  217. Point3f p;
  218. p.z = cos(ah) * d1;
  219. p.x = sin(ah) * d1;
  220. p.y = p.z * tan(av);
  221. Point3f pb1, pb2;
  222. generateBasis(pb1, pb2);
  223. float cbHalfWidth = squareSize.width * patternSize.width * 0.5f;
  224. float cbHalfHeight = squareSize.height * patternSize.height * 0.5f;
  225. float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
  226. float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
  227. vector<Point3f> pts3d(4);
  228. vector<Point2f> pts2d(4);
  229. for(;;)
  230. {
  231. pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  232. pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  233. pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  234. pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  235. /* can remake with better perf */
  236. projectPoints(pts3d, rvec, tvec, camMat, distCoeffs, pts2d);
  237. bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0;
  238. bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0;
  239. bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0;
  240. bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0;
  241. if ( inrect1 && inrect2 && inrect3 && inrect4)
  242. break;
  243. p.z *= 1.1f;
  244. }
  245. Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
  246. return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2,
  247. squareSize.width, squareSize.height, pts3d, corners);
  248. }
  249. Mat ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
  250. const Size2f& squareSize, const Point3f& pos, vector<Point2f>& corners) const
  251. {
  252. cov = std::min(cov, 0.8);
  253. Point3f p = pos;
  254. Point3f pb1, pb2;
  255. generateBasis(pb1, pb2);
  256. float cbHalfWidth = squareSize.width * patternSize.width * 0.5f;
  257. float cbHalfHeight = squareSize.height * patternSize.height * 0.5f;
  258. float cbHalfWidthEx = cbHalfWidth * ( patternSize.width + 1) / patternSize.width;
  259. float cbHalfHeightEx = cbHalfHeight * (patternSize.height + 1) / patternSize.height;
  260. vector<Point3f> pts3d(4);
  261. vector<Point2f> pts2d(4);
  262. pts3d[0] = p + pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  263. pts3d[1] = p + pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  264. pts3d[2] = p - pb1 * cbHalfWidthEx - cbHalfHeightEx * pb2;
  265. pts3d[3] = p - pb1 * cbHalfWidthEx + cbHalfHeightEx * pb2;
  266. /* can remake with better perf */
  267. projectPoints(pts3d, rvec, tvec, camMat, distCoeffs, pts2d);
  268. Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2;
  269. return generateChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2,
  270. squareSize.width, squareSize.height, pts3d, corners);
  271. }
  272. } // namespace