使用C++版opencv,已知四个点可以组成一个带角度的矩形,如何通过这四个点构造cv::RotatedRect
时间: 2023-12-14 07:37:36 浏览: 244
可以使用以下代码来构造cv::RotatedRect:
```c++
#include <opencv2/opencv.hpp>
int main()
{
// 已知四个点,假设这四个点为 p1, p2, p3, p4
cv::Point2f p1(10, 10);
cv::Point2f p2(50, 10);
cv::Point2f p3(50, 30);
cv::Point2f p4(10, 30);
// 构造矩形的中心点和大小
cv::Point2f center((p1.x + p3.x) / 2, (p1.y + p3.y) / 2);
cv::Size2f size(cv::norm(p3 - p2), cv::norm(p2 - p1));
// 计算矩形的倾斜角度
float angle = std::atan2(p2.y - p1.y, p2.x - p1.x) * 180 / CV_PI;
// 构造cv::RotatedRect
cv::RotatedRect rect(center, size, angle);
return 0;
}
```
其中,cv::Point2f 表示二维坐标点,cv::Size2f 表示二维大小,cv::RotatedRect 表示带角度的矩形。函数 cv::norm(x) 可以计算向量 x 的模长。
相关问题
使用C++版opencv,已知四个点可以组成一个带角度的矩形,如何通过这四个点构造一个带角度矩形?
可以通过以下步骤构造一个带角度矩形:
1. 将四个点按照顺序连接成一条闭合的轮廓线。
2. 使用OpenCV中的 `minAreaRect()` 函数计算出该轮廓线所在的最小矩形。
3. 得到该最小矩形的中心点、宽度、高度、旋转角度等参数。
4. 使用这些参数构造一个自定义的矩形结构体,即可得到一个带角度的矩形。
下面是一个简单的示例代码,仅供参考:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
// 自定义矩形结构体
struct RotatedRectCustom {
Point2f center;
float width;
float height;
float angle; // 旋转角度,单位:度
};
int main() {
// 定义四个点
vector<Point2f> points = {Point2f(100, 100), Point2f(200, 100), Point2f(200, 200), Point2f(100, 200)};
// 构造轮廓线
vector<vector<Point2f>> contours = {points};
// 计算最小矩形
RotatedRect rect = minAreaRect(points);
// 构造自定义矩形结构体
RotatedRectCustom rect_custom;
rect_custom.center = rect.center;
rect_custom.width = rect.size.width;
rect_custom.height = rect.size.height;
rect_custom.angle = rect.angle;
// 打印结果
cout << "Center: " << rect_custom.center << endl;
cout << "Width: " << rect_custom.width << endl;
cout << "Height: " << rect_custom.height << endl;
cout << "Angle: " << rect_custom.angle << " degrees" << endl;
return 0;
}
```
opencv已知RotatedRect,使用C++绘制
可以使用OpenCV提供的cv::RotatedRect的成员函数来获取矩形的四个顶点,然后使用cv::line函数绘制。
以下是示例代码:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 创建一个RotatedRect
RotatedRect rect(Point2f(100, 100), Size2f(200, 100), 30);
// 获取矩形的四个顶点
Point2f vertices[4];
rect.points(vertices);
// 创建画布并绘制
Mat canvas(300, 300, CV_8UC3, Scalar(255, 255, 255));
for (int i = 0; i < 4; i++)
{
line(canvas, vertices[i], vertices[(i + 1) % 4], Scalar(0, 0, 255), 2);
}
// 显示结果
imshow("RotatedRect", canvas);
waitKey();
return 0;
}
```
运行结果如下图所示:
![image](https://user-images.githubusercontent.com/28896432/126113096-1a4d49c1-7b43-4a97-8a49-0fb3e9fba9b6.png)
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)