distortion_model.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP
  43. #define OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP
  44. //! @cond IGNORED
  45. namespace cv { namespace detail {
  46. /**
  47. Computes the matrix for the projection onto a tilted image sensor
  48. \param tauX angular parameter rotation around x-axis
  49. \param tauY angular parameter rotation around y-axis
  50. \param matTilt if not NULL returns the matrix
  51. \f[
  52. \vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}((\tau_x, \tau_y)}
  53. {0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)}
  54. {0}{0}{1} R(\tau_x, \tau_y)
  55. \f]
  56. where
  57. \f[
  58. R(\tau_x, \tau_y) =
  59. \vecthreethree{\cos(\tau_y)}{0}{-\sin(\tau_y)}{0}{1}{0}{\sin(\tau_y)}{0}{\cos(\tau_y)}
  60. \vecthreethree{1}{0}{0}{0}{\cos(\tau_x)}{\sin(\tau_x)}{0}{-\sin(\tau_x)}{\cos(\tau_x)} =
  61. \vecthreethree{\cos(\tau_y)}{\sin(\tau_y)\sin(\tau_x)}{-\sin(\tau_y)\cos(\tau_x)}
  62. {0}{\cos(\tau_x)}{\sin(\tau_x)}
  63. {\sin(\tau_y)}{-\cos(\tau_y)\sin(\tau_x)}{\cos(\tau_y)\cos(\tau_x)}.
  64. \f]
  65. \param dMatTiltdTauX if not NULL it returns the derivative of matTilt with
  66. respect to \f$\tau_x\f$.
  67. \param dMatTiltdTauY if not NULL it returns the derivative of matTilt with
  68. respect to \f$\tau_y\f$.
  69. \param invMatTilt if not NULL it returns the inverse of matTilt
  70. **/
  71. template <typename FLOAT>
  72. void computeTiltProjectionMatrix(FLOAT tauX,
  73. FLOAT tauY,
  74. Matx<FLOAT, 3, 3>* matTilt = 0,
  75. Matx<FLOAT, 3, 3>* dMatTiltdTauX = 0,
  76. Matx<FLOAT, 3, 3>* dMatTiltdTauY = 0,
  77. Matx<FLOAT, 3, 3>* invMatTilt = 0)
  78. {
  79. FLOAT cTauX = cos(tauX);
  80. FLOAT sTauX = sin(tauX);
  81. FLOAT cTauY = cos(tauY);
  82. FLOAT sTauY = sin(tauY);
  83. Matx<FLOAT, 3, 3> matRotX = Matx<FLOAT, 3, 3>(1,0,0,0,cTauX,sTauX,0,-sTauX,cTauX);
  84. Matx<FLOAT, 3, 3> matRotY = Matx<FLOAT, 3, 3>(cTauY,0,-sTauY,0,1,0,sTauY,0,cTauY);
  85. Matx<FLOAT, 3, 3> matRotXY = matRotY * matRotX;
  86. Matx<FLOAT, 3, 3> matProjZ = Matx<FLOAT, 3, 3>(matRotXY(2,2),0,-matRotXY(0,2),0,matRotXY(2,2),-matRotXY(1,2),0,0,1);
  87. if (matTilt)
  88. {
  89. // Matrix for trapezoidal distortion of tilted image sensor
  90. *matTilt = matProjZ * matRotXY;
  91. }
  92. if (dMatTiltdTauX)
  93. {
  94. // Derivative with respect to tauX
  95. Matx<FLOAT, 3, 3> dMatRotXYdTauX = matRotY * Matx<FLOAT, 3, 3>(0,0,0,0,-sTauX,cTauX,0,-cTauX,-sTauX);
  96. Matx<FLOAT, 3, 3> dMatProjZdTauX = Matx<FLOAT, 3, 3>(dMatRotXYdTauX(2,2),0,-dMatRotXYdTauX(0,2),
  97. 0,dMatRotXYdTauX(2,2),-dMatRotXYdTauX(1,2),0,0,0);
  98. *dMatTiltdTauX = (matProjZ * dMatRotXYdTauX) + (dMatProjZdTauX * matRotXY);
  99. }
  100. if (dMatTiltdTauY)
  101. {
  102. // Derivative with respect to tauY
  103. Matx<FLOAT, 3, 3> dMatRotXYdTauY = Matx<FLOAT, 3, 3>(-sTauY,0,-cTauY,0,0,0,cTauY,0,-sTauY) * matRotX;
  104. Matx<FLOAT, 3, 3> dMatProjZdTauY = Matx<FLOAT, 3, 3>(dMatRotXYdTauY(2,2),0,-dMatRotXYdTauY(0,2),
  105. 0,dMatRotXYdTauY(2,2),-dMatRotXYdTauY(1,2),0,0,0);
  106. *dMatTiltdTauY = (matProjZ * dMatRotXYdTauY) + (dMatProjZdTauY * matRotXY);
  107. }
  108. if (invMatTilt)
  109. {
  110. FLOAT inv = 1./matRotXY(2,2);
  111. Matx<FLOAT, 3, 3> invMatProjZ = Matx<FLOAT, 3, 3>(inv,0,inv*matRotXY(0,2),0,inv,inv*matRotXY(1,2),0,0,1);
  112. *invMatTilt = matRotXY.t()*invMatProjZ;
  113. }
  114. }
  115. }} // namespace detail, cv
  116. //! @endcond
  117. #endif // OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP