#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
void GetLongestEdgeLineEquation(cv::RotatedRect& minRect) {
// 获取矩形的中心坐标、尺寸和角度
cv::Point2f center = minRect.center;
cv::Size2f size = minRect.size;
float angle = minRect.angle;
// 判断哪个边是最长边
float angleOfLongestEdge = 0;
if (size.width > size.height) {
angleOfLongestEdge = angle; // 如果宽度是最长边
} else {
angleOfLongestEdge = angle + 90; // 如果高度是最长边
}
// 计算最长边的方向的斜率 (方向角转为斜率)
// 角度转弧度
float radian = angleOfLongestEdge * CV_PI / 180.0;
float slope = tan(radian);
// 直线方程:y = mx + b,其中 m 是斜率,b 是截距
// 计算 b (截距),使用矩形的中心点代入方程
float b = center.y - slope * center.x;
// 输出计算的直线方程
std::cout << "Line equation: y = " << slope << "x + " << b << std::endl;
}
int main() {
// 创建一个旋转矩形
cv::RotatedRect minRect(cv::Point2f(100, 100), cv::Size2f(80, 50), 30); // 中心 (100, 100),宽 80,高 50,角度 30°
// 调用函数获取最长边的直线方程
GetLongestEdgeLineEquation(minRect);
return 0;
}