学习OpenCV:rotatedRectangleIntersection计算两个旋转矩形的交集面积

 如图所示,计算两个旋转矩形相交重合区域的顶点,最多返回8个顶点。通过contourArea可返回该顶点集合的面积。

学习OpenCV:rotatedRectangleIntersection计算两个旋转矩形的交集面积_第1张图片

void DrawPointSet(Mat& imgInoutput, vector vecPoint, int radius, const Scalar& color, int thickness = 1, int lineType = LINE_8);

void DrawRotatedRect(Mat& imgInoutput, RotatedRect rectInput, Scalar color, int thickness = 1, int lineType = LINE_8);

void DrawRotatedRect(Mat& imgInoutput, RotatedRect rectInput, Scalar color, int thickness, int lineType)
{
	Point2f* vertices = new cv::Point2f[4];
	rectInput.points(vertices);
	for (int j = 0; j < 4; j++)
		line(imgInoutput, vertices[j], vertices[(j + 1) % 4], color, thickness, lineType);
}

void DrawPointSet(Mat& imgInoutput, vector vecPoint, int radius, const Scalar& color, int thickness, int lineType)
{
	for (size_t i = 0; i < vecPoint.size(); i++)
		circle(imgInoutput, vecPoint[i], radius, color, thickness, lineType);
}

int main()
{
	Mat imgSrc = Mat::zeros(Size(500,500), CV_8UC3);
	RotatedRect rect1;
	rect1.center = Point2f(200,200);
	rect1.size = Size(100,100);
	rect1.angle = 3;
	RotatedRect rect2;
	rect2.center = Point2f(210, 180);
	rect2.size = Size(100, 100);
	rect2.angle = 10;
	vector intersectingRegion;
	rotatedRectangleIntersection(rect1, rect2, intersectingRegion);
	DrawRotatedRect(imgSrc, rect1, Scalar(255, 0, 0));
	DrawRotatedRect(imgSrc, rect2, Scalar(0, 0, 255));
	if (intersectingRegion.empty())
		return 0;
	DrawPointSet(imgSrc, vector(intersectingRegion.begin(), intersectingRegion.end()), 3, Scalar(0, 255, 0));
	double dbArea = contourArea(intersectingRegion);
}

 

你可能感兴趣的:(学习OpenCV)