当前位置: 首页 > article >正文

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)

http://www.kler.cn/news/309320.html

相关文章:

  • 网络安全学习(五)Burpsuite实战
  • 解决Matlab报错:MEX 文件 ‘D:\MATLAB\toolbox\maple\maplemex.mexw64‘ 无效: 缺少依赖共享库
  • 2024年某大厂HW蓝队面试题分享
  • Github打不开解决方法
  • 产品经理有必要学习大模型技术吗?
  • 【Elasticsearch系列十一】聚合 DSL API
  • 地平线秋招2025
  • 大数据处理技术:企业岗位需求决策
  • 【机器学习】--- 深度学习中的注意力机制
  • bootstrap application nacos环境配置失效
  • C++——stack和queue的模拟实现
  • 重学SpringBoot3-SpringApplicationRunListener
  • GORM查询指南:高效检索数据
  • 认识数学建模,什么是数学建模
  • 小红书热门系列,风口副业项目AI宠物壁纸号,玩法分享
  • AI教你学Python 第10天 :参数与返回值
  • TAPD卓越版的全面评测:超强的功能与用户体验优势
  • linux下日志系统setvbuf接口及结构体 handle_file_t成员介绍
  • 学习之git的团队协作
  • Qt问题笔记
  • Selenium之下拉框操作详解
  • MySQL5.7-虚拟列
  • Rust 所有权 借用与引用
  • Android 车联网——汽车模块介绍(附1)
  • 【SpringCloud】Spring Cloud 开发环境搭建与基础工程构建
  • TaskingAI实践(一)快速上手
  • 【Java】基础语法介绍
  • 【自动驾驶】决策规划算法 | 数学基础(三)直角坐标与自然坐标转换Ⅱ
  • 论文速递 | 基于MIC-ICEEMD-RIME-DHKELM的碳排放预测模型研究
  • Linux系统上搭建Vulhub靶场