应用背景
目标检测应用落地时,经常会有区域入侵的判断需求,本文主要记录一种简单高效的判断方法。不过区域划分目前只能是矩形,还不支持不规则区域。
思路
判断目标框是否与矩形区域有重叠。
区域入侵代码
代码片
.
#include<iostream>
#include<cmath>
using namespace std;
typedef struct rectangle
{
float centerX;
float centerY;
float width;
float height;
}Rectangle;
bool areTwoRectsOverlapped(Rectangle rect1, Rectangle rect2)
{
float verticalDistance; //垂直距离
float horizontalDistance; //水平距离
verticalDistance = fabs(rect1.centerX - rect2.centerX);
horizontalDistance = fabs(rect1.centerY - rect2.centerY);
float verticalThreshold; //两矩形分离的垂直临界值
float horizontalThreshold; //两矩形分离的水平临界值
verticalThreshold = (rect1.height + rect2.height)/2;
horizontalThreshold = (rect1.width + rect2.width )/2;
if(verticalDistance > verticalThreshold || horizontalDistance > horizontalThreshold)
return false;
return true;
}
int main()
{
Rectangle rect1 = {0.0, 35.0, 200.0, 100};
Rectangle rect2 = {65.0, 50.0, 63.1, 29};
if(areTwoRectsOverlapped(rect1, rect2))
cout << "overlapped" << endl;
else
cout << "not overlapped" << endl;
return 0;
}
参考:如何判断两个矩形是否有重叠部分?(某公司校园招聘笔试试题)
版权声明:本文为CSDN博主「雨果先生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/DeepLearning_/article/details/122978468
暂无评论