lenz_stitcher.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // lenz_stitcher.hpp
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by lr on 2023/2/8.
  6. //
  7. #ifndef lenz_stitcher_hpp
  8. #define lenz_stitcher_hpp
  9. #if __cplusplus && __has_include(<opencv2/imgcodecs/ios.h>)
  10. #import <opencv2/core/ocl.hpp>
  11. #import <opencv2/opencv.hpp>
  12. #include <iostream>
  13. #include <thread>
  14. #include <vector>
  15. #include <string>
  16. #include <random>
  17. #include <math.h>
  18. #include <dirent.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. using namespace cv;
  23. using namespace cv::detail;
  24. using namespace cv::ocl;
  25. using namespace std;
  26. #define STITCH_STATUS_INIT 0
  27. #define STITCH_STATUS_CHECKING 1
  28. #define STITCH_STATUS_STITCHING 2
  29. #define STITCH_STATUS_FREE 3
  30. #define STITCH_STATUS_FAILED 4
  31. #define FEATURE_HEIGHT 300
  32. #define STITCH_HEIGHT 300
  33. #define BIG_STITCH_HEIGHT 1000
  34. #define BIG_FEATURE_HEIGHT 800
  35. #define MAX_SIZE 4000 // 缩略图的最大尺寸,长或宽都不能超过MAX_SIZE*MAX_SIZE,否则直接报错
  36. #define BIG_MAX_SIZE 10000 // 拼接大图的最大尺寸, 长或宽都不能超过BIG_MAX_SIZE
  37. struct stitch_data
  38. {
  39. Mat scaled_gray_frame;
  40. float work_scale;
  41. vector<Point2f> good_keypoints;
  42. Mat descriptors;
  43. Mat homo = (Mat_<double>(3, 3) << 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0); // store homography matrix to base image
  44. Mat base_homo = (Mat_<double>(3, 3) << 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0); // store homography matrix to last base image
  45. };
  46. struct LinePara
  47. {
  48. float k;
  49. float b;
  50. };
  51. string test_image_path = "";
  52. class LenzStitcher
  53. {
  54. public:
  55. string task_id = "";
  56. string local_path = "";
  57. int stitcher_status = STITCH_STATUS_INIT; // 0: initialized, 1: checking, 2: stitching, 3: free
  58. int failed_count = 0; // 当前连续失败的帧数
  59. int failed_thresh = 10; // 连续5帧失败才返回失败
  60. vector<float> last_move_distances; // 记录存下来的最近10帧的移动距离,算均值,大于阈值的时候提示速度过快
  61. float iou_thresh = 0.7; // iou低于该阈值的时候把这张图片存下来
  62. float area_thresh = 0.5; // area threshold for green box
  63. float angle_thresh = 10; // angle threshold for green box
  64. float inline_thresh = 0.1; // 光流法匹配的点的比例至少要大于这个值,才认为是正确的映射矩阵
  65. int matches_thresh = 10; // 前后两帧基于光流的匹配点的个数的阈值,低于该值则无法进行拼接
  66. float check_match_conf = 0.2f;
  67. int frame_index = 0;
  68. vector<Point2f> last_warp_points; // 上一张存下来的图的四个点在拼接图上的坐标
  69. int big_frame_index = 0; // 存下来的帧的信息
  70. float total_shift_x = 0;
  71. float total_shift_y = 0;
  72. Mat last_frame;
  73. Mat last_stitch_image = Mat(); // 最后一张拼接缩略图
  74. int last_direction = 0; // 上一帧的手机移动方向
  75. vector<float> last_angles = vector<float>{90, 90, 90, 90};
  76. vector<float> last_scales = vector<float>{1, 1, 1, 1};
  77. string last_stitch_image_path = ""; // 拼接缩略图的本地路径
  78. stitch_data last_check_data; // 上一帧的数据
  79. string big_stitch_image_path = ""; // 拼接大图的本地路径
  80. string test_image_path = ""; // test
  81. LenzStitcher(string input_id);
  82. int *ofcheck_stitch(Mat &frame, int const direction, int const is_last_one); // 输入每一帧实时产生拼接缩略图
  83. bool get_big_image(int get_big_image); // 在收到当前帧是最后一帧的时候运行,会用之前存下来的帧数据产生最后的拼接大图
  84. void remove_local_path();
  85. };
  86. bool is_good_homo(const vector<Point2f> &corners, const Mat &frame, float area_thresh, float angle_thresh);
  87. bool is_convex(const vector<Point2f> &corners);
  88. float calc_area(const vector<Point2f> &corners, const Mat &frame);
  89. float min_axis(vector<float> input_list);
  90. float max_axis(vector<float> input_list);
  91. float calc_angle(const vector<cv::Point2f> &conners);
  92. float calc_vangle(const vector<cv::Point2f> &conners);
  93. float calc_hangle(const vector<cv::Point2f> &conners);
  94. void double_match(const Mat &query_des, const Mat &train_des, vector<DMatch> &good_matches, float check_match_conf);
  95. void single_match(const Mat &query_des, const Mat &train_des, vector<DMatch> &good_matches, float check_match_conf);
  96. void getFilePath(const char *path, const char *filename, char *filepath);
  97. void deleteFile(const char *path);
  98. bool isFloderexit(const char *path);
  99. void randomSelectKeyPoints(vector<KeyPoint> kpts, vector<Point2f> &pts, int max_num);
  100. void optimizeSeam(Mat &last_img, Mat &stitched_image, vector<Point2f> &corners);
  101. void getLinePara(float x1, float y1, float x2, float y2, LinePara &LP);
  102. float point_2_line(float k, float b, float k2, float x, float y);
  103. int get_direction(vector<Point2f> &frame_corners, vector<Point2f> &transpose_corners);
  104. float get_move_distance(vector<Point2f> &frame_center, vector<Point2f> &transpose_center);
  105. int dcmp(float x);
  106. float cross(Point2f a, Point2f b, Point2f c);
  107. float CPIA(vector<Point2f> a, vector<Point2f> b, int na, int nb);
  108. float SPIA(vector<Point2f> a, vector<Point2f> b, int na, int nb);
  109. Point2f get_intersection_point(Point2f a, Point2f b, Point2f c, Point2f d);
  110. float PolygonArea(vector<Point2f> p, int n);
  111. float single_quadrangle_iou(vector<Point2f> pts1, vector<Point2f> pts2);
  112. float single_box_iou(vector<Point2f> pts1, vector<Point2f> pts2);
  113. void stitch_big_image(Mat frame, vector<float> angles, vector<float> scales, int direction, int big_frame_index);
  114. void cylindrical_projection(Mat &img, Mat &output, float angle);
  115. void SphericalProjection(Mat& imgMat, Mat& output, float angle);
  116. void fill_contours(vector<vector<Point>> contours, Mat &source_image, Mat &target_image);
  117. float angle_of_two_vector(Point2f &pt1, Point2f &pt2, Point2f &c);
  118. void calc_angles(vector<Point2f> &conners);
  119. #endif
  120. #endif