intrinsics.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #ifndef __OPENCV_RGBD_INTRINSICS_HPP__
  5. #define __OPENCV_RGBD_INTRINSICS_HPP__
  6. #include "opencv2/core/matx.hpp"
  7. namespace cv
  8. {
  9. namespace kinfu
  10. {
  11. struct Intr
  12. {
  13. /** @brief Camera intrinsics */
  14. /** Reprojects screen point to camera space given z coord. */
  15. struct Reprojector
  16. {
  17. Reprojector() {}
  18. inline Reprojector(Intr intr)
  19. {
  20. fxinv = 1.f/intr.fx, fyinv = 1.f/intr.fy;
  21. cx = intr.cx, cy = intr.cy;
  22. }
  23. template<typename T>
  24. inline cv::Point3_<T> operator()(cv::Point3_<T> p) const
  25. {
  26. T x = p.z * (p.x - cx) * fxinv;
  27. T y = p.z * (p.y - cy) * fyinv;
  28. return cv::Point3_<T>(x, y, p.z);
  29. }
  30. float fxinv, fyinv, cx, cy;
  31. };
  32. /** Projects camera space vector onto screen */
  33. struct Projector
  34. {
  35. inline Projector(Intr intr) : fx(intr.fx), fy(intr.fy), cx(intr.cx), cy(intr.cy) { }
  36. template<typename T>
  37. inline cv::Point_<T> operator()(cv::Point3_<T> p) const
  38. {
  39. T invz = T(1)/p.z;
  40. T x = fx*(p.x*invz) + cx;
  41. T y = fy*(p.y*invz) + cy;
  42. return cv::Point_<T>(x, y);
  43. }
  44. template<typename T>
  45. inline cv::Point_<T> operator()(cv::Point3_<T> p, cv::Point3_<T>& pixVec) const
  46. {
  47. T invz = T(1)/p.z;
  48. pixVec = cv::Point3_<T>(p.x*invz, p.y*invz, 1);
  49. T x = fx*pixVec.x + cx;
  50. T y = fy*pixVec.y + cy;
  51. return cv::Point_<T>(x, y);
  52. }
  53. float fx, fy, cx, cy;
  54. };
  55. Intr() : fx(), fy(), cx(), cy() { }
  56. Intr(float _fx, float _fy, float _cx, float _cy) : fx(_fx), fy(_fy), cx(_cx), cy(_cy) { }
  57. Intr(cv::Matx33f m) : fx(m(0, 0)), fy(m(1, 1)), cx(m(0, 2)), cy(m(1, 2)) { }
  58. // scale intrinsics to pyramid level
  59. inline Intr scale(int pyr) const
  60. {
  61. float factor = (1.f /(1 << pyr));
  62. return Intr(fx*factor, fy*factor, cx*factor, cy*factor);
  63. }
  64. inline Reprojector makeReprojector() const { return Reprojector(*this); }
  65. inline Projector makeProjector() const { return Projector(*this); }
  66. inline cv::Matx33f getMat() const { return Matx33f(fx, 0, cx, 0, fy, cy, 0, 0, 1); }
  67. float fx, fy, cx, cy;
  68. };
  69. } // namespace rgbd
  70. } // namespace cv
  71. #endif