AutoCar  v1.0.0
image_utility.h
1 #ifndef __UTILITY_H__
2 #define __UTILITY_H__
3 
4 #include <opencv2/opencv.hpp>
5 #include <functional>
6 #include <algorithm>
7 #include <fstream>
8 
9 #define if_less_equal(num, condition) if (num < condition) { num = condition; }
10 #define if_more_equal(num, condition) if (num > condition) { num = condition; }
11 #define if_more_swap(num1, num2) if (num1 > num2) { std::swap(num1, num2); }
12 
13 namespace autocar
14 {
15 namespace vision_mul
16 {
17  // 计算一条直线的角度, 范围 0-180 度
18  // 参数: firstPt 第一个点
19  // 参数: secondPt 第二个点
20  // 返回值: 角度 (0-180)
21  double calcLineDegree(const cv::Point2f& firstPt, const cv::Point2f& secondPt);
22 
23  // 将旋转矩形的角度转换为 0-180度
24  // 参数: box 矩形
25  // 返回值: 角度 (0-180)
26  double getRcDegree(const cv::RotatedRect &box);
27 
28  // 计算直线的k、b
29  // 参数: x1 横坐标1, x2 横坐标2
30  // 参数: y1 纵坐标1, y2 纵坐标2
31  // 返回值: 直线的参数k、b
32  template <typename T>
33  std::pair<double, double> get_linear_parameters(T x1, T y1, T x2, T y2);
34 
35  // 两点计算直线 k、b
36  // 参数: point1 点1
37  // 参数: point2 点2
38  // 返回值: 直线的参数 k、b
39  template <typename T>
40  std::pair<double, double> get_linear_parameters(T point1, T point2);
41 
42  // 已知直线 k、b, 给定纵坐标 y 计算出坐标 x
43  // 参数: coefficient 斜率 k
44  // 参数: intercept 截距 b
45  // 返回值: 横坐标 x 的值
46  template <typename T>
47  T get_linear_x(double coefficient, double intercept, T y);
48 
49  // 已知直线 k、b, 给定横坐标 x 计算出坐标 y
50  // 参数: coefficient 斜率 k
51  // 参数: intercept 截距 b
52  // 返回值: 纵坐标 y 的值
53  template <typename T>
54  T get_linear_y(double coefficient, double intercept, T x);
55 
56  // 遍历旋转矩形内部的所有像素点
57  // 参数: rect 旋转矩形
58  // 参数: func 回调函数
59  // 参数: xstep x坐标方向跳过点数
60  // 参数: ystep y坐标方向跳过点数
61  void for_each(const cv::RotatedRect &rect, const std::function<void(int, int)> &func, int xstep = 1, int ystep = 1);
62 
63  // 遍历旋转矩形内部的所有像素点
64  // 参数: rect 旋转矩形
65  // 参数: xmin x坐标最大值
66  // 参数: ymin y坐标最大值
67  // 参数: xmax x坐标最大值
68  // 参数: ymax y坐标最大值
69  // 参数: func 回调函数
70  // 参数: xstep x坐标方向跳过点数
71  // 参数: ystep y坐标方向跳过点数
72  void for_each(const cv::RotatedRect &rect, float xmin, float ymin, float xmax, float ymax, const std::function<void(int, int)> &func, int xstep = 1, int ystep = 1);
73 
74  // 计算两点距离的平方
75  // 参数: point1 第一个点
76  // 参数: point2 第二个点
77  // 返回值: 两点距离的平方
78  template <typename T>
79  double squaredist(const cv::Point_<T> &point1, const cv::Point_<T> &point2);
80 
81  // 计算两点距离的平方
82  // 参数: x1 第一个点的x坐标
83  // 参数: y1 第一个点的y坐标
84  // 参数: x2 第二个点的x坐标
85  // 参数: y2 第二个点的y坐标
86  // 返回值: 两点距离的平方
87  template <typename T>
88  double squaredist(T x1, T y1, T x2, T y2);
89 
90  // 计算两点距离
91  // 参数: point1 第一个点
92  // 参数: point2 第二个点
93  // 返回值: 两点距离
94  template <typename T>
95  double distance(const cv::Point_<T> &point1, const cv::Point_<T> &point2);
96 
97  // 计算两点距离
98  // 参数: x1 第一个点的x坐标
99  // 参数: y1 第一个点的y坐标
100  // 参数: x2 第二个点的x坐标
101  // 参数: y2 第二个点的y坐标
102  // 返回值: 两点距离
103  template <typename T>
104  double distance(T x1, T y1, T x2, T y2);
105 
106  // 计算两点的中心点坐标
107  // 参数: point1 第一个点
108  // 参数: point2 第二个点
109  // 返回值: 两点的中心点坐标
110  template <typename T>
111  T center_point(const T &point1, const T &point2);
112 
113  // 判断点与直线的距离
114  // 参数: coefficient 直线斜率 k
115  // 参数: intercept 直线截距 b
116  // 参数: point 需要计算的点
117  // 返回值: 点与直线的距离
118  template <typename T>
119  double point_to_line(double coefficient, double intercept, const T &point);
120 
121  // 判断点是否在另外两点构成的直线上
122  // 参数: point1 直线点 1
123  // 参数: point2 直线点 2
124  // 参数: point 将要判断的点
125  // 返回值: true or false
126  template <typename T>
127  bool point_in_line(const T &point1, const T &point2, const T &point);
128 }
129 }
130 
131 #endif // __UTILITY_H__
Definition: vel2odom.h:5