OpenCV结构分析与形状描述符(24)检测两个旋转矩形之间是否相交的一个函数rotatedRectangleIntersection()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

测两个旋转矩形之间是否存在交集。
如果存在交集,则还返回交集区域的顶点。
下面是一些交集配置的例子。斜线图案表示交集区域,红色顶点是由函数返回的。
在这里插入图片描述

rotatedRectangleIntersection() 这个函数看起来像是用于检测两个旋转矩形之间是否相交的一个方法。在计算机图形学和游戏开发中,检测两个旋转矩形是否相交是一个常见的需求,尤其是在物理引擎或碰撞检测系统中。

函数原型

int cv::rotatedRectangleIntersection	
(
	const RotatedRect & 	rect1,
	const RotatedRect & 	rect2,
	OutputArray 	intersectingRegion 
)		

参数

  • 参数rect1 第一个矩形
  • 参数rect2 第二个矩形
  • 参数intersectingRegion 交集区域的顶点输出数组。最多返回8个顶点。存储为 std::vectorcv::Point2f 或者类型为 CV_32FC2 的 cv::Mat 作为 Mx1。

代码示例

在这个示例中:

  • 如果numVertices为0,表示没有交集。
  • 如果numVertices大于0,表示有交集,并且intersectingRegion中存储了交集区域的顶点。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>

int main()
{
    // 创建两个旋转矩形实例
    cv::RotatedRect rect1( cv::Point2f( 100, 100 ), cv::Size2f( 50, 100 ), 30 );  // 中心点, 尺寸, 旋转角度
    cv::RotatedRect rect2( cv::Point2f( 120, 120 ), cv::Size2f( 100, 50 ), -20 );

    // 用于存储交集区域顶点的向量
    std::vector< cv::Point2f > intersectingRegion;

    // 调用函数检测两个旋转矩形是否相交
    int numVertices = cv::rotatedRectangleIntersection( rect1, rect2, intersectingRegion );

    // 检查返回值以确定交集情况
    if ( numVertices == 0 )
    {
        std::cout << "No intersection." << std::endl;
    }
    else if ( numVertices > 0 )
    {
        std::cout << "Intersection with " << numVertices << " vertices:" << std::endl;
        for ( const auto& pt : intersectingRegion )
        {
            std::cout << "Vertex: (" << pt.x << ", " << pt.y << ")" << std::endl;
        }
    }
    else
    {
        std::cout << "Error occurred during intersection calculation." << std::endl;
    }

    return 0;
}

运行结果

Intersection with 1 vertices:
Vertex: (136.054, 87.5523)
Vertex: (97.1611, 154.917)
Vertex: (95.9088, 155.373)
Vertex: (75.3445, 143.5)
Vertex: (64.4649, 113.609)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值