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 
22 template <typename T>
23 bool isIncludedInOther(const cv::Rect_<T> &rect1, const cv::Rect_<T> &rect2) {
24  cv::Point_<T> tl1 = rect1.tl();
25  cv::Point_<T> br1 = rect1.br();
26  if (tl1.inside(rect2) && br1.inside(rect2)) {
27  return true;
28  }
29 
30  cv::Point_<T> tl2 = rect2.tl();
31  cv::Point_<T> br2 = rect2.br();
32  if (tl2.inside(rect1) && br2.inside(rect1)) {
33  return true;
34  }
35 
36  return false;
37 }
38 
39 template <typename T>
40 cv::Point_<T> rectangleCenter(const cv::Rect_<T> &rect) {
41  return cv::Point_<T>(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0);
42 }
43 
44 template <typename T>
45 T distanceBetween(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
46  return sqrt(pow(point2.x - point.x, 2) + pow(point2.y - point.y, 2));
47 }
48 
49 template <typename T>
50 inline cv::Point_<T> pointsCenter(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
51  return cv::Point_<T>((point.x + point2.x) / 2.0, (point.y + point2.y) / 2.0);
52 }
53 
57 template <typename T>
58 T distanceBetween(const cv::Rect_<T> &rectangle1, const cv::Rect_<T> &rectangle2) {
59  return distanceBetween(rectangleCenter(rectangle1), rectangleCenter(rectangle2));
60 }
61 
62 template <typename T>
63 string rectToString(const cv::Rect_<T> &rect) {
64  return "[" + to_string(rect.x) + ", " + to_string(rect.y) + " w=" + to_string(rect.width) + " h=" + to_string(rect.height) + "]";
65 }
66 
67 template <typename T>
68 string pointToString(const cv::Point_<T> &point) {
69  return "[" + to_string(point.x) + ", " + to_string(point.y) + "]";
70 }
71 
72 template <typename T>
73 string sizeToString(const cv::Size_<T> &size) {
74  return "[w=" + to_string(size.width) + ",h=" + to_string(size.height) + "]";
75 }
76 
77 template <typename T>
78 cv::Rect_<T> averageRectangle(const vector<cv::Rect_<T>> &rectangles) {
79  T x = 0;
80  T y = 0;
81  T width = 0;
82  T height = 0;
83 
84  for (auto rectangle : rectangles) {
85  x += rectangle.x;
86  y += rectangle.y;
87  width += rectangle.width;
88  height += rectangle.height;
89  }
90 
91  x /= rectangles.size();
92  y /= rectangles.size();
93  width /= rectangles.size();
94  height /= rectangles.size();
95 
96  return cv::Rect_<T>(x, y, width, height);
97 }
98 
99 template <typename T>
100 bool equals(const cv::Size_<T> &size1, const cv::Size_<T> &size2) {
101  return size1.width == size2.width && size1.height == size2.height;
102 }
103 
104 bool isRotationMatrix(const cv::Mat &R);
105 cv::Vec3f rotationMatrixToEulerAngles(const cv::Mat &R);
106 
107 void rotateMatrix(const cv::Mat &src, double angleDegrees, cv::Mat &target, cv::Scalar fillColor = cv::Scalar(255, 255, 255));
108 
112 template <typename T>
113 double anglePointsInRad(const cv::Point_<T> &point, const cv::Point_<T> &point2) {
114  double hypotenuse = distanceBetween(point, point2);
115  double adjacent = point2.x - point.x;
116  double angle = acos(adjacent / hypotenuse);
117  return angle;
118 }
119 
120 bool matIsEqual(const cv::Mat &img1, const cv::Mat &img2);
121 
122 }
123 
124 #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::averageRectangle
cv::Rect_< T > averageRectangle(const vector< cv::Rect_< T >> &rectangles)
Definition: geom_opencv.h:78
conscience_utils::geom::rectToString
string rectToString(const cv::Rect_< T > &rect)
Definition: geom_opencv.h:63
conscience_utils::geom::anglePointsInRad
double anglePointsInRad(const cv::Point_< T > &point, const cv::Point_< T > &point2)
Definition: geom_opencv.h:113
conscience_utils::geom::equals
bool equals(const cv::Size_< T > &size1, const cv::Size_< T > &size2)
Definition: geom_opencv.h:100
conscience_utils::geom::rectangleCenter
cv::Point_< T > rectangleCenter(const cv::Rect_< T > &rect)
Definition: geom_opencv.h:40
conscience_utils::geom
Definition: CscPointCloudPcdWriter.cpp:9
conscience_utils::geom::sizeToString
string sizeToString(const cv::Size_< T > &size)
Definition: geom_opencv.h:73
conscience_utils::geom::pointToString
string pointToString(const cv::Point_< T > &point)
Definition: geom_opencv.h:68
conscience_utils::geom::pointsCenter
cv::Point_< T > pointsCenter(const cv::Point_< T > &point, const cv::Point_< T > &point2)
Definition: geom_opencv.h:50
conscience_utils::geom::isIncludedInOther
bool isIncludedInOther(const cv::Rect_< T > &rect1, const cv::Rect_< T > &rect2)
Definition: geom_opencv.h:23
geom.h
conscience_utils::geom::rotateMatrix
void rotateMatrix(const cv::Mat &src, double angleDegrees, cv::Mat &target, cv::Scalar fillColor)
Definition: geom_opencv.cpp:27
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:38
conscience_utils::geom::matIsEqual
bool matIsEqual(const cv::Mat &img1, const cv::Mat &img2)
Definition: geom_opencv.cpp:58
src
const char * src
Definition: lz4.h:865
conscience_utils::geom::isRotationMatrix
bool isRotationMatrix(const cv::Mat &R)
Definition: geom_opencv.cpp:18