camera_calibrator_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef DEPTHGUIDE_CAMERA_CALIBRATOR_IMPL_H
  2. #define DEPTHGUIDE_CAMERA_CALIBRATOR_IMPL_H
  3. #include "image_process/camera_calibrator.h"
  4. #include "core/image_utility_v2.h"
  5. #include "core/math_helper.hpp"
  6. #include <opencv2/core/mat.hpp>
  7. #include <nanovg.h>
  8. #include <BS_thread_pool.hpp>
  9. #include <atomic>
  10. #include <list>
  11. namespace camera_calibrator_impl {
  12. // TODO: make configurable
  13. static constexpr auto sharpness_threshold = 4.f;
  14. using corner_type = std::vector<cv::Point2f>;
  15. cv::Mat to_cv_mat(const glm::mat3 &mat);
  16. cv::Mat to_cv_mat(const glm::vec3 &vec);
  17. glm::mat3 rodrigues_to_mat(const cv::Mat &vec);
  18. cv::Mat to_cv_rodigues(const glm::mat3 &mat);
  19. glm::mat3 to_mat3(const cv::Mat &mat);
  20. glm::vec3 to_vec3(const cv::Mat &mat);
  21. struct hand_eye_calib {
  22. // fill before use
  23. transform_buffer track_pool;
  24. struct image_store_type {
  25. corner_type corner;
  26. timestamp_type sample_ts;
  27. };
  28. using image_pool_type =
  29. std::vector<image_store_type>;
  30. image_pool_type img_pool;
  31. cv::Size img_size;
  32. // fill by member functions
  33. using object_points_type =
  34. std::vector<cv::Vec3f>;
  35. object_points_type obj_ps;
  36. // result
  37. glm::mat3 intrinsic_mat; // intrinsic matrix
  38. using dist_coeffs_type = std::vector<float>;
  39. dist_coeffs_type dist_coeffs; // distortion coefficients
  40. glm::mat4 result_mat; // result matrix, camera in camera reference
  41. int result_ts_offset; // temporal latency between tracker and camera
  42. // result error
  43. float obj_reproj_err; // average error of spacial corners after hand-eye calibration, in mm
  44. float img_reproj_err; // average error of image corners after hand-eye calibration, in pixel
  45. void set_object_points(cv::Size size, float dis); // dis: in mm
  46. void calc();
  47. camera_intrinsic_v0 intrinsic_v0();
  48. private:
  49. // interpolate track matrix
  50. glm::mat4 calc_track_mat(timestamp_type t);
  51. // p: in the normalized plane
  52. glm::vec2 distort_point(glm::vec2 p);
  53. // p: in camera coordinate
  54. // result: in pixel
  55. // duplicate cv::projectPoints
  56. glm::vec2 project_point(glm::vec3 p);
  57. // average distance between two sets of corner points
  58. static float corner_distance(const corner_type &c1,
  59. const corner_type &c2);
  60. // do hand-eye calibration
  61. // return reference in camera
  62. glm::mat4 calib_hand_eye();
  63. // evaluate hand-eye calibration result
  64. cv::Scalar evaluate_hand_eye(const glm::mat4 &ret_mat);
  65. size_t sample_num, corner_num;
  66. cv::Mat intrinsic_cv_mat, dist_coeffs_mat;
  67. int ts_offset = 0; // timestamp offset
  68. std::vector<cv::Mat> cam_r_vec; // chess board in camera
  69. std::vector<cv::Mat> cam_t_vec;
  70. cv::Mat aux_r, aux_t; // tracker in chess board
  71. };
  72. }
  73. using namespace camera_calibrator_impl;
  74. struct camera_calibrator::impl {
  75. create_config conf;
  76. using conn_type = boost::signals2::connection;
  77. conn_type img_conn;
  78. struct img_store_type {
  79. image_ptr img;
  80. cv::Mat img_mat;
  81. timestamp_type sample_ts;
  82. corner_type corners;
  83. float corner_sharpness;
  84. bool process_finished: 1 = false;
  85. bool corners_detected: 1 = false;
  86. };
  87. using img_pool_type = std::list<img_store_type>;
  88. img_pool_type img_pool;
  89. std::atomic_int img_ok_cnt = 0;
  90. struct track_store_type {
  91. glm::mat4 ref_mat;
  92. timestamp_type sample_ts;
  93. };
  94. using track_pool_type = std::vector<track_store_type>;
  95. track_pool_type track_pool;
  96. std::atomic<img_store_type *> last_finish = nullptr;
  97. NVGcontext *vg = nullptr;
  98. std::unique_ptr<BS::thread_pool> tp; // thread pool
  99. cv::Size img_size;
  100. std::unique_ptr<hand_eye_calib> calib;
  101. explicit impl(const create_config &conf);
  102. ~impl();
  103. void per_image_process(img_store_type *st);
  104. void img_callback(obj_name_type name);
  105. void save_track_data();
  106. void load_track_data(const std::string &path);
  107. void process(); // do calibration
  108. void simulate_process(const simulate_info_type &info);
  109. void start();
  110. void stop(bool on_exit = false);
  111. void render();
  112. void show();
  113. };
  114. #endif //DEPTHGUIDE_CAMERA_CALIBRATOR_IMPL_H