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)
相关推荐
极智视界7 分钟前
分类场景数据集大全「包含数据标注+训练脚本」 (持续原地更新)
人工智能·yolo·数据集·分类算法·数据标注·classification·分类数据集
翻滚的小@强23 分钟前
自动驾驶科普(百度Apollo)学习笔记
人工智能·自动驾驶·百度apollo
从零开始学习人工智能24 分钟前
从游戏到自动驾驶:互联网时代强化学习如何让机器学会自主决策?
人工智能·游戏·自动驾驶
幼稚园的山代王39 分钟前
Prompt Enginering(提示工程)先进技术
java·人工智能·ai·chatgpt·langchain·prompt
dfsj6601139 分钟前
LLMs 系列科普文(14)
人工智能·深度学习·算法
摘取一颗天上星️1 小时前
深入解析机器学习的心脏:损失函数及其背后的奥秘
人工智能·深度学习·机器学习·损失函数·梯度下降
远方16091 小时前
20-Oracle 23 ai free Database Sharding-特性验证
数据库·人工智能·oracle
znhy60581 小时前
智能终端与边缘计算按章复习
人工智能·边缘计算
__Benco1 小时前
OpenHarmony平台驱动使用(十五),SPI
人工智能·驱动开发·harmonyos
Listennnn1 小时前
AI系统的构建
人工智能·系统架构