【OpenCV 4.5】RotatedRect旋转矩形 各参数研究

【OpenCV 4.5】RotatedRect各参数研究

class CV_EXPORTS RotatedRect
{
public:
    //构造函数
    RotatedRect();
    RotatedRect(const Point2f& center, const Size2f& size, float angle);
	//size宽x高
    RotatedRect(const CvBox2D& box);
    void points(Point2f pts[]) const;//返回矩形的4个顶点
    Rect boundingRect() const; //返回包含旋转矩形的最小矩形(横平竖直)
    Point2f center; //矩形的质心
    Size2f size;   //矩形的边长
    float angle;  //旋转角度,当角度为0、90、180、270等时,矩形就成了一个直立的矩形
};

三个重要参数

中心点center:为矩形对角线的交点
Width与Height
角度angle
四个顶点p[0]-p[3]

四个顶点p[0]-p[3]

x坐标最小的点为p[0]
先看x坐标最小 再看y坐标最小
p[0]~p[3]按顺时针方向依次排列

Width与Height

p[0]-p[1]是Height
p[1]-p[2]是Width
不管长度大小

角度angle

Height(p[0]-p[1])和y轴的夹角

实验分析

正正好的一定是90度
image
image

p[0]一定是x最小的点
先看x再看y 都看小
image
image

Height一定是p[0]-p[1]
Width一定是p[1]-p[2]

image
image

代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
	Mat test_image(200, 200, CV_8UC3, Scalar(0));
	RotatedRect rRect = RotatedRect(Point2f(100, 100), Size2f(100, 50), 30);

	/*获得角度*/
	cout << rRect.angle << endl;
	//角度30度

	/*获得中心点*/
	cout << rRect.center << endl;
	//中心点[100, 100]

	/*获得宽×高*/
	cout << rRect.size << endl;
	//宽×高[100 x 50]

	/*获得宽*/※
	cout << rRect.size.width << endl;
	// 100

	/*获得高*/※
	cout << rRect.size.height << endl;
	// 50

	/*获得面积*/※
	cout << rRect.size.area() << endl;
	//5000

	//绘制旋转矩形
	Point2f vertices[4];
	rRect.points(vertices);
	for (int i = 0; i < 4; i++)
		line(test_image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 2);

	//圈中这个旋转矩形的外矩形
	Rect brect = rRect.boundingRect();
	rectangle(test_image, brect, Scalar(255, 0, 0), 2);

	imshow("rectangles", test_image);
	waitKey(0);
	return 0;
}

装甲板识别注意事项:瘦长型

Height < Width
p[0]都是最左上角的点
image
Angle 90度

image
Angle 0-90度

需要转化

Height > Width

p[0] 左下角的点

Angle 0-90度
image

【判断条件】
p[0].x<p[1].x
p[0].y>p[1].y

【操作】
Height 和 Width进行对调
image

如何初筛矩形

长度
Height > (1/10) * Width
Height < ( ) * Width
角度
要么90度
要么abs<30度****

(_rect.size.height * 10 > _rect.size.width)//不能太细
&& (_rect.size.width * 1 < _rect.size.height)//不能太粗
&& ((abs(_rect.angle) < 30 || _rect.angle == 90))//角度
posted @ 2024-11-26 20:20  White_ink  阅读(1186)  评论(1)    收藏  举报