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

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

算法描述

测两个旋转矩形之间是否存在交集。

如果存在交集,则还返回交集区域的顶点。

下面是一些交集配置的例子。斜线图案表示交集区域,红色顶点是由函数返回的。

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

函数原型

cpp 复制代码
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中存储了交集区域的顶点。
cpp 复制代码
#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;
}

运行结果

bash 复制代码
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)
相关推荐
七月shi人25 分钟前
【AI编程工具IDE/CLI/插件专栏】-国外IDE与Cursor能力对比
ide·人工智能·ai编程·代码助手
2zcode3 小时前
基于Matlab的深度学习智能行人检测与统计系统
人工智能·深度学习·目标跟踪
WSSWWWSSW3 小时前
Jupyter Notebook 中高效处理和实时展示来自 OpenCV 和 Pillow 的图像数据探究
opencv·jupyter·pillow
weixin_464078073 小时前
机器学习sklearn:过滤
人工智能·机器学习·sklearn
weixin_464078073 小时前
机器学习sklearn:降维
人工智能·机器学习·sklearn
数据与人工智能律师3 小时前
智能合约漏洞导致的损失,法律责任应如何分配
大数据·网络·人工智能·算法·区块链
张艾拉 Fun AI Everyday4 小时前
小宿科技:AI Agent 的卖铲人
人工智能·aigc·创业创新·ai-native
zhongqu_3dnest4 小时前
三维火灾调查重建:科技赋能,探寻真相
人工智能
飞哥数智坊4 小时前
AI编程实战:写作助手进化,Trae+Kimi-K2两小时搞定“带样式复制”
人工智能·trae
木枷4 小时前
c2rust使用
人工智能·物联网·edge