RotatedRect也是表示一个矩形框,但是与Rect不同的是,RotatedRect可以带倾斜角的矩形,如下图所示:
RotatedRect结构中包括三个变量:
Point2f center:为矩形的中心点位置
Size2f size:矩形框width和height
float angle:为左上角与X轴夹角。与X轴的平行方向角度为0,逆时针旋转角度为负,顺时针旋转角度为正。当角度为0, 90, 180, 270,则和Rect一样
RotatedRect与其他数据结构不一样,数据类型都是固定的,没有其他数据类型。
Learning OpenCV3中指出了RotatedRect与Rect一个重要不同:
One very important difference between cv::RotatedRect and cv::Rect is the convention that a cv::RotatedRect is located in "space" relative to its center, while the cv::Rect is located relative to its upper-left corner.
RotatedRect类
RotatedRect类定义如下:
使用总结如下:
Medthod | Description |
RotatedRect() | 默认构造函数 |
RotatedRect(const Point2f& center, const Size2f& size, float angle) | 带参数构造函数: center:为矩形中心位置 size:为矩形宽度和高度 angle:为与x轴夹角 |
RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3) | 带参数构造函数: 根据矩形框的三个点确定一个RotatedRect point1、point2、point3矩形三个点 |
void points(Point2f pts[]) | 返回矩阵的四个角点的左边,顺序为:bottomLeft, topLeft, topRight, bottomRight. |
Rect boundingRect() | 返回能够包含旋转矩形框的最小矩形,返回的是Rect没有带角度 |
Rect_<float> boundingRect2f() | 返回能够包含旋转矩形框的最小矩形,返回的是Rect没有带角度,其数据类型为float |
Point2f center | 矩形中心点位置 |
Size2f size | 矩形的宽度和高度 |
float angle | 矩形夹角 |
来自于opencv官网的RotatedRect用例:https://docs.opencv.org/3.2.0/db/dd6/classcv_1_1RotatedRect.html#ae1be388780b8d5faf450be18cbbf30f1
#include <stdio.h>
#include "opencv2/opencv.hpp"
using namespace cv;
void main()
{
Mat image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100, 100), Size2f(100, 50), 30);
Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0));
Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255, 0, 0));
imshow("rectangles", image);
waitKey(0);
}
运行结果:
RotatedRect没有对operator进行重构,不支持+,-,*,/以及&,|等操作符