opencv 中 获得RotatedRect rect 的参数
时间: 2024-03-13 09:45:17 浏览: 203
在 OpenCV 中,获取 RotatedRect 的参数可以使用以下代码:
```
RotatedRect rect; // 假设已经获取了 RotatedRect 对象
Point2f vertices[4];
rect.points(vertices); // 获取 RotatedRect 的四个顶点坐标
float angle = rect.angle; // 获取 RotatedRect 的旋转角度
Size2f size = rect.size; // 获取 RotatedRect 的尺寸(长宽)
Point2f center = rect.center; // 获取 RotatedRect 的中心点坐标
```
其中,`points()` 方法可以获取 RotatedRect 的四个顶点坐标,`angle` 获取旋转角度,`size` 获取尺寸,`center` 获取中心点坐标。
相关问题
opencv中如何将Rect转成RotatedRect
要将一个矩形转换成旋转矩形,可以使用OpenCV中的`cv::RotatedRect`类。`cv::RotatedRect`类可以用于表示一个旋转的矩形,包含四个参数:中心点的坐标、宽度、高度和旋转角度。
下面是将`cv::Rect`对象转换成`cv::RotatedRect`对象的示例代码:
```cpp
cv::Rect rect(100, 100, 200, 100); // 定义一个矩形
cv::Point2f center(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0); // 计算矩形中心点坐标
cv::RotatedRect rotatedRect(center, cv::Size2f(rect.width, rect.height), 0); // 将矩形转换成旋转矩形
```
在上述代码中,首先定义了一个`cv::Rect`对象`rect`,然后通过计算矩形中心点坐标和宽度、高度创建了一个`cv::Size2f`对象,最后用这些参数创建了一个`cv::RotatedRect`对象`rotatedRect`。
根据cv::RotatedRect里的参数,画出旋转矩形
好的,我可以为您提供一些示例代码来画出旋转矩形。以下是基于OpenCV库的C++代码示例:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// 创建一个旋转矩形对象
RotatedRect rect(Point2f(100, 100), Size2f(200, 100), 45);
// 创建一个空白的黑色图像
Mat image = Mat::zeros(Size(500, 500), CV_8UC3);
// 获取旋转矩形的四个顶点
Point2f vertices[4];
rect.points(vertices);
// 将旋转矩形的四个顶点连接起来
for (int i = 0; i < 4; i++)
{
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 2);
}
// 显示图像
imshow("Rotated Rectangle", image);
waitKey(0);
return 0;
}
```
在这个示例代码中,我们首先创建了一个`RotatedRect`对象,指定了旋转矩形的中心点、宽度、高度和旋转角度。然后,我们创建了一个黑色的图像,并使用`rect.points()`方法获取旋转矩形的四个顶点,并使用`line()`方法连接这些顶点。
最后,我们使用`imshow()`方法显示图像,并使用`waitKey()`方法等待任意键盘输入。当我们运行这个程序时,将会显示一个旋转矩形。
阅读全文
相关推荐














