Conscience Core
geom_opencv.h
Go to the documentation of this file.
1 //
2 // geom_opencv.h
3 // ConscienceRobotManager
4 //
5 // Created by Louis Grignon on 19/08/2025.
6 // Copyright © 2025 IliesZaoui. All rights reserved.
7 //
8 
9 #ifndef geom_opencv_h
10 #define geom_opencv_h
11 
12 #include <cmath>
13 
14 #include "geom.h"
15 #include <opencv2/opencv.hpp>
16 
17 namespace conscience_utils::geom {
18 
19 double getIntersectionOverUnion(const cv::Rect2d &bb1, const cv::Rect2d &bb2);
20 
24 template <typename T>
25 bool isIncludedInOther(const cv::Rect_<T> &rect1, const cv::Rect_<T> &rect2) {
26  cv::Point_<T> tl1 = rect1.tl();
27  cv::Point_<T> br1 = rect1.br();
28  if (tl1.inside(rect2) && br1.inside(rect2)) {
29  return true;
30  }
31 
32  cv::Point_<T> tl2 = rect2.tl();
33  cv::Point_<T> br2 = rect2.br();
34  if (tl2.inside(rect1) && br2.inside(rect1)) {
35  return true;
36  }
37 
38  return false;
39 }
40 
41 template <typename T>
42 cv::Point_<T> rectangleCenter(const cv::Rect_<T> &rect) {
43  return cv::Point_<T>(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0);
44 }
45 
46 template <typename T>
47 T distanceBetween(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
48  return sqrt(pow(point2.x - point.x, 2) + pow(point2.y - point.y, 2));
49 }
50 
51 template <typename T>
52 inline cv::Point_<T> pointsCenter(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
53  return cv::Point_<T>((point.x + point2.x) / 2.0, (point.y + point2.y) / 2.0);
54 }
55 
59 template <typename T>
60 T distanceBetween(const cv::Rect_<T> &rectangle1, const cv::Rect_<T> &rectangle2) {
61  return distanceBetween(rectangleCenter(rectangle1), rectangleCenter(rectangle2));
62 }
63 
64 template <typename T>
65 string rectToString(const cv::Rect_<T> &rect) {
66  return "[" + to_string(rect.x) + ", " + to_string(rect.y) + " w=" + to_string(rect.width) + " h=" + to_string(rect.height) + "]";
67 }
68 
69 template <typename T>
70 string pointToString(const cv::Point_<T> &point) {
71  return "[" + to_string(point.x) + ", " + to_string(point.y) + "]";
72 }
73 
74 template <typename T>
75 string sizeToString(const cv::Size_<T> &size) {
76  return "[w=" + to_string(size.width) + ",h=" + to_string(size.height) + "]";
77 }
78 
79 template <typename T>
80 cv::Rect_<T> averageRectangle(const vector<cv::Rect_<T>> &rectangles) {
81  T x = 0;
82  T y = 0;
83  T width = 0;
84  T height = 0;
85 
86  for (auto rectangle : rectangles) {
87  x += rectangle.x;
88  y += rectangle.y;
89  width += rectangle.width;
90  height += rectangle.height;
91  }
92 
93  x /= rectangles.size();
94  y /= rectangles.size();
95  width /= rectangles.size();
96  height /= rectangles.size();
97 
98  return cv::Rect_<T>(x, y, width, height);
99 }
100 
101 template <typename T>
102 bool equals(const cv::Size_<T> &size1, const cv::Size_<T> &size2) {
103  return size1.width == size2.width && size1.height == size2.height;
104 }
105 
106 bool isRotationMatrix(const cv::Mat &R);
107 cv::Vec3f rotationMatrixToEulerAngles(const cv::Mat &R);
108 
109 void rotateMatrix(const cv::Mat &src, double angleDegrees, cv::Mat &target, cv::Scalar fillColor = cv::Scalar(255, 255, 255));
110 
114 template <typename T>
115 double anglePointsInRad(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
116  double hypotenuse = distanceBetween(point, point2);
117  double adjacent = point2.x - point.x;
118  double angle = acos(adjacent / hypotenuse);
119  return angle;
120 }
121 
122 bool matIsEqual(const cv::Mat &img1, const cv::Mat &img2);
123 
124 }
125 
126 #endif
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:26470
conscience_utils::geom::getIntersectionOverUnion
double getIntersectionOverUnion(const cv::Rect2d &bb1, const cv::Rect2d &bb2)
Definition: geom_opencv.cpp:17
conscience_utils::geom::averageRectangle
cv::Rect_< T > averageRectangle(const vector< cv::Rect_< T >> &rectangles)
Definition: geom_opencv.h:80
conscience_utils::geom::rectToString
string rectToString(const cv::Rect_< T > &rect)
Definition: geom_opencv.h:65
conscience_utils::geom::anglePointsInRad
double anglePointsInRad(const cv::Point_< T > &point, const cv::Point_< T > &point2)
Definition: geom_opencv.h:115
conscience_utils::geom::equals
bool equals(const cv::Size_< T > &size1, const cv::Size_< T > &size2)
Definition: geom_opencv.h:102
conscience_utils::geom::rectangleCenter
cv::Point_< T > rectangleCenter(const cv::Rect_< T > &rect)
Definition: geom_opencv.h:42
conscience_utils::geom
Definition: geom.cpp:14
conscience_utils::geom::sizeToString
string sizeToString(const cv::Size_< T > &size)
Definition: geom_opencv.h:75
conscience_utils::geom::pointToString
string pointToString(const cv::Point_< T > &point)
Definition: geom_opencv.h:70
conscience_utils::geom::pointsCenter
cv::Point_< T > pointsCenter(const cv::Point_< T > &point, const cv::Point_< T > &point2)
Definition: geom_opencv.h:52
conscience_utils::geom::isIncludedInOther
bool isIncludedInOther(const cv::Rect_< T > &rect1, const cv::Rect_< T > &rect2)
Definition: geom_opencv.h:25
geom.h
conscience_utils::geom::rotateMatrix
void rotateMatrix(const cv::Mat &src, double angleDegrees, cv::Mat &target, cv::Scalar fillColor)
Definition: geom_opencv.cpp:63
conscience_utils::geom::distanceBetween
double distanceBetween(double x1, double y1, double x2, double y2)
Definition: geom.cpp:136
conscience_utils::geom::rotationMatrixToEulerAngles
cv::Vec3f rotationMatrixToEulerAngles(const cv::Mat &R)
Definition: geom_opencv.cpp:74
conscience_utils::geom::matIsEqual
bool matIsEqual(const cv::Mat &img1, const cv::Mat &img2)
Definition: geom_opencv.cpp:94
conscience_utils::geom::isRotationMatrix
bool isRotationMatrix(const cv::Mat &R)
Definition: geom_opencv.cpp:54