test_posit.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // POSIT is not exposed to C++ API yet, so the test is disabled
  43. #if 0
  44. namespace opencv_test { namespace {
  45. class CV_POSITTest : public cvtest::BaseTest
  46. {
  47. public:
  48. CV_POSITTest();
  49. protected:
  50. void run(int);
  51. };
  52. CV_POSITTest::CV_POSITTest()
  53. {
  54. test_case_count = 20;
  55. }
  56. void CV_POSITTest::run( int start_from )
  57. {
  58. int code = cvtest::TS::OK;
  59. /* fixed parameters output */
  60. /*float rot[3][3]={ 0.49010f, 0.85057f, 0.19063f,
  61. -0.56948f, 0.14671f, 0.80880f,
  62. 0.65997f, -0.50495f, 0.55629f };
  63. float trans[3] = { 0.0f, 0.0f, 40.02637f };
  64. */
  65. /* Some variables */
  66. int i, counter;
  67. CvTermCriteria criteria;
  68. CvPoint3D32f* obj_points;
  69. CvPoint2D32f* img_points;
  70. CvPOSITObject* object;
  71. float angleX, angleY, angleZ;
  72. RNG& rng = ts->get_rng();
  73. int progress = 0;
  74. CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );
  75. CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );
  76. CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );
  77. CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );
  78. CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );
  79. CvMat* rotation = cvCreateMat( 3, 3, CV_32F );
  80. CvMat* translation = cvCreateMat( 3, 1, CV_32F );
  81. CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );
  82. const float flFocalLength = 760.f;
  83. const float flEpsilon = 0.5f;
  84. /* Initialization */
  85. criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
  86. criteria.epsilon = flEpsilon;
  87. criteria.max_iter = 10000;
  88. /* Allocating source arrays; */
  89. obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
  90. img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );
  91. /* Fill points arrays with values */
  92. /* cube model with edge size 10 */
  93. obj_points[0].x = 0; obj_points[0].y = 0; obj_points[0].z = 0;
  94. obj_points[1].x = 10; obj_points[1].y = 0; obj_points[1].z = 0;
  95. obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;
  96. obj_points[3].x = 0; obj_points[3].y = 10; obj_points[3].z = 0;
  97. obj_points[4].x = 0; obj_points[4].y = 0; obj_points[4].z = 10;
  98. obj_points[5].x = 10; obj_points[5].y = 0; obj_points[5].z = 10;
  99. obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;
  100. obj_points[7].x = 0; obj_points[7].y = 10; obj_points[7].z = 10;
  101. /* Loop for test some random object positions */
  102. for( counter = start_from; counter < test_case_count; counter++ )
  103. {
  104. ts->update_context( this, counter, true );
  105. progress = update_progress( progress, counter, test_case_count, 0 );
  106. /* set all rotation matrix to zero */
  107. cvZero( true_rotationX );
  108. cvZero( true_rotationY );
  109. cvZero( true_rotationZ );
  110. /* fill random rotation matrix */
  111. angleX = (float)(cvtest::randReal(rng)*2*CV_PI);
  112. angleY = (float)(cvtest::randReal(rng)*2*CV_PI);
  113. angleZ = (float)(cvtest::randReal(rng)*2*CV_PI);
  114. true_rotationX->data.fl[0 *3+ 0] = 1;
  115. true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);
  116. true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];
  117. true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);
  118. true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];
  119. true_rotationY->data.fl[1 *3+ 1] = 1;
  120. true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);
  121. true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];
  122. true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);
  123. true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];
  124. true_rotationZ->data.fl[2 *3+ 2] = 1;
  125. true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);
  126. true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];
  127. true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);
  128. true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];
  129. cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
  130. cvMatMul( tmp_matrix, true_rotationZ, true_rotation);
  131. /* fill translation vector */
  132. true_translation->data.fl[2] = (float)(cvtest::randReal(rng)*(2*flFocalLength-40) + 60);
  133. true_translation->data.fl[0] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
  134. true_translation->data.fl[1] = (float)((cvtest::randReal(rng)*2-1)*true_translation->data.fl[2]);
  135. /* calculate perspective projection */
  136. for ( i = 0; i < 8; i++ )
  137. {
  138. float vec[3];
  139. CvMat Vec = cvMat( 3, 1, CV_32F, vec );
  140. CvMat Obj_point = cvMat( 3, 1, CV_32F, &obj_points[i].x );
  141. cvMatMul( true_rotation, &Obj_point, &Vec );
  142. vec[0] += true_translation->data.fl[0];
  143. vec[1] += true_translation->data.fl[1];
  144. vec[2] += true_translation->data.fl[2];
  145. img_points[i].x = flFocalLength * vec[0] / vec[2];
  146. img_points[i].y = flFocalLength * vec[1] / vec[2];
  147. }
  148. /*img_points[0].x = 0 ; img_points[0].y = 0;
  149. img_points[1].x = 80; img_points[1].y = -93;
  150. img_points[2].x = 245;img_points[2].y = -77;
  151. img_points[3].x = 185;img_points[3].y = 32;
  152. img_points[4].x = 32; img_points[4].y = 135;
  153. img_points[5].x = 99; img_points[5].y = 35;
  154. img_points[6].x = 247; img_points[6].y = 62;
  155. img_points[7].x = 195; img_points[7].y = 179;
  156. */
  157. object = cvCreatePOSITObject( obj_points, 8 );
  158. cvPOSIT( object, img_points, flFocalLength, criteria,
  159. rotation->data.fl, translation->data.fl );
  160. cvReleasePOSITObject( &object );
  161. Mat _rotation = cvarrToMat(rotation), _true_rotation = cvarrToMat(true_rotation);
  162. Mat _translation = cvarrToMat(translation), _true_translation = cvarrToMat(true_translation);
  163. code = cvtest::cmpEps2( ts, _rotation, _true_rotation, flEpsilon, false, "rotation matrix" );
  164. if( code < 0 )
  165. break;
  166. code = cvtest::cmpEps2( ts, _translation, _true_translation, flEpsilon, false, "translation vector" );
  167. if( code < 0 )
  168. break;
  169. }
  170. cvFree( &obj_points );
  171. cvFree( &img_points );
  172. cvReleaseMat( &true_rotationX );
  173. cvReleaseMat( &true_rotationY );
  174. cvReleaseMat( &true_rotationZ );
  175. cvReleaseMat( &tmp_matrix );
  176. cvReleaseMat( &true_rotation );
  177. cvReleaseMat( &rotation );
  178. cvReleaseMat( &translation );
  179. cvReleaseMat( &true_translation );
  180. if( code < 0 )
  181. ts->set_failed_test_info( code );
  182. }
  183. TEST(Calib3d_POSIT, accuracy) { CV_POSITTest test; test.safe_run(); }
  184. }} // namespace
  185. #endif
  186. /* End of file. */