test_projection.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2009, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include "test_precomp.hpp"
  36. #include <opencv2/sfm/projection.hpp>
  37. namespace opencv_test { namespace {
  38. TEST(Sfm_projection, homogeneousToEuclidean)
  39. {
  40. Matx33f X(1, 2, 3,
  41. 4, 5, 6,
  42. 2, 1, 0);
  43. Matx23f XEuclidean;
  44. homogeneousToEuclidean(X,XEuclidean);
  45. EXPECT_EQ((int) X.rows-1,(int) XEuclidean.rows );
  46. for(int y=0;y<X.rows-1;++y)
  47. {
  48. for(int x=0;x<X.cols;++x)
  49. {
  50. if (X(X.rows-1,x)!=0)
  51. {
  52. EXPECT_LE( std::abs(X(y,x)/X(X.rows-1, x) - XEuclidean(y,x)), 1e-4 );
  53. }
  54. }
  55. }
  56. }
  57. TEST(Sfm_projection, euclideanToHomogeneous)
  58. {
  59. // Testing with floats
  60. Matx33f x(1, 2, 3,
  61. 4, 5, 6,
  62. 2, 1, 0);
  63. Matx43f XHomogeneous;
  64. euclideanToHomogeneous(x,XHomogeneous);
  65. EXPECT_EQ((int) x.rows+1,(int)XHomogeneous.rows );
  66. for(int i=0;i<x.cols;++i)
  67. EXPECT_EQ( 1,(int) XHomogeneous(x.rows,i) );
  68. // Testing with doubles
  69. Vec2d x2(4,3);
  70. Vec3d X2;
  71. euclideanToHomogeneous(x2,X2);
  72. EXPECT_EQ((int) x2.rows+1,(int)X2.rows );
  73. EXPECT_EQ( 4, X2(0) );
  74. EXPECT_EQ( 3, X2(1) );
  75. EXPECT_EQ( 1, X2(2) );
  76. }
  77. TEST(Sfm_projection, P_From_KRt)
  78. {
  79. Matx33d K, Kp;
  80. K << 10, 1, 30,
  81. 0, 20, 40,
  82. 0, 0, 1;
  83. Matx33d R, Rp;
  84. R << 1, 0, 0,
  85. 0, 1, 0,
  86. 0, 0, 1;
  87. Vec3d t, tp;
  88. t << 1, 2, 3;
  89. Matx34d P(3,4);
  90. projectionFromKRt(K, R, t, P);
  91. KRtFromProjection(P, Kp, Rp, tp);
  92. EXPECT_MATRIX_NEAR(K, Kp, 1e-8);
  93. EXPECT_MATRIX_NEAR(R, Rp, 1e-8);
  94. EXPECT_VECTOR_NEAR(t, tp, 1e-8);
  95. // TODO: Change the code to ensure det(R) == 1, which is not currently
  96. // the case. Also add a test for that here.
  97. }
  98. }} // namespace