lenz_stitcher.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 2000 // 缩略图的最大尺寸,长或宽都不能超过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<KeyPoint> 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. };
  45. struct LinePara
  46. {
  47. float k;
  48. float b;
  49. };
  50. string test_image_path = "";
  51. class LenzStitcher
  52. {
  53. public:
  54. string task_id = "";
  55. string local_path = "";
  56. int stitcher_status = STITCH_STATUS_INIT; // 0: initialized, 1: checking, 2: stitching, 3: free
  57. int failed_count = 0; // 当前连续失败的帧数
  58. int failed_thresh = 5; // 连续5帧失败才返回失败
  59. float iou_thresh = 0.5; // iou低于该阈值的时候把这张图片存下来
  60. float area_thresh = 0.1; // area threshold for green box
  61. float angle_thresh = 45; // angle threshold for green box vertical edge
  62. int matches_thresh = 10; // 前后两帧基于光流的匹配点的个数的阈值,低于该值则无法进行拼接
  63. float check_match_conf = 0.2f;
  64. int frame_index = 0;
  65. vector<Point2f> last_warp_points; // 上一张存下来的图的四个点在拼接图上的坐标
  66. int big_frame_index = 0; // 存下来的帧的信息
  67. float total_shift_x = 0;
  68. float total_shift_y = 0;
  69. Mat last_stitch_image = Mat(); // 最后一张拼接缩略图
  70. int last_direction = 0; // 上一帧的手机移动方向
  71. vector<float> last_angles = vector<float>{90, 90, 90, 90};
  72. string last_stitch_image_path = ""; // 拼接缩略图的本地路径
  73. stitch_data last_check_data; // 上一帧的数据
  74. string big_stitch_image_path = ""; // 拼接大图的本地路径
  75. string test_image_path = ""; // test
  76. LenzStitcher(string input_id);
  77. int *ofcheck_stitch(Mat &frame, int const direction, int const is_last_one); // 输入每一帧实时产生拼接缩略图
  78. bool get_big_image(int get_big_image); // 在收到当前帧是最后一帧的时候运行,会用之前存下来的帧数据产生最后的拼接大图
  79. void remove_local_path();
  80. };
  81. bool is_good_green_box(const vector<Point2f> &corners, const Mat &cpu_frame, float area_thresh, float angle_thresh);
  82. bool is_convex(const vector<Point2f> &corners);
  83. float calc_area(const vector<Point2f> &corners, const Mat &frame);
  84. float min_axis(vector<float> input_list);
  85. float max_axis(vector<float> input_list);
  86. float calc_angle(const vector<cv::Point2f> &conners);
  87. float calc_vangle(const vector<cv::Point2f> &conners);
  88. float calc_hangle(const vector<cv::Point2f> &conners);
  89. void double_match(const Mat &query_des, const Mat &train_des, vector<DMatch> &good_matches, float check_match_conf);
  90. void single_match(const Mat &query_des, const Mat &train_des, vector<DMatch> &good_matches, float check_match_conf);
  91. void getFilePath(const char *path, const char *filename, char *filepath);
  92. void deleteFile(const char *path);
  93. bool isFloderexit(const char *path);
  94. void randomSelectKeyPoints(vector<KeyPoint> kpts, vector<Point2f> &pts, int max_num);
  95. void optimizeSeam(Mat &last_img, Mat &stitched_image, vector<Point2f> &corners);
  96. void getLinePara(float x1, float y1, float x2, float y2, LinePara &LP);
  97. float point_2_line(float k, float b, float k2, float x, float y);
  98. int get_direction(vector<Point2f> &frame_corners, vector<Point2f> &transpose_corners);
  99. int dcmp(float x);
  100. float cross(Point2f a, Point2f b, Point2f c);
  101. float CPIA(vector<Point2f> a, vector<Point2f> b, int na, int nb);
  102. float SPIA(vector<Point2f> a, vector<Point2f> b, int na, int nb);
  103. Point2f get_intersection_point(Point2f a, Point2f b, Point2f c, Point2f d);
  104. float PolygonArea(vector<Point2f> p, int n);
  105. float single_quadrangle_iou(vector<Point2f> pts1, vector<Point2f> pts2);
  106. float single_box_iou(vector<Point2f> pts1, vector<Point2f> pts2);
  107. void stitch_big_image(Mat frame, vector<float> angles, int direction, int big_frame_index);
  108. void cylindrical_projection(Mat &img, Mat &output, float angle);
  109. void fill_contours(vector<vector<Point>> contours, Mat &source_image, Mat &target_image);
  110. float angle_of_two_vector(Point2f &pt1, Point2f &pt2, Point2f &c);
  111. void calc_angles(vector<Point2f> &conners);
  112. #endif
  113. #endif