计算两个矩形重叠面积的简单方法

实验需要,需要计算两个矩形重叠面积

想来想去觉得挺复杂,搜了下,看见一个超给力的方法

这里分享下:

function D = DecideOberlap(Reframe,GTframe)

x1 = Reframe(1);
y1 = Reframe(2);
width1 = Reframe(3);
height1 = Reframe(4);

x2 = GTframe(1);
y2 = GTframe(2);
width2 = GTframe(3);
height2 = GTframe(4);

endx = max(x1+width1,x2+width2);
startx = min(x1,x2);
width = width1+width2-(endx-startx);

endy = max(y1+height1,y2+height2);
starty = min(y1,y2);
height = height1+height2-(endy-starty);

if width<=0||height<=0
    D = 0;
else
    Area = width*height;
    Area1 = width1*height1;
    Area2 = width2*height2;
    ratio = Area/(Area1+Area2-Area);
    if ratio>=0.5
        D = 1;
    else
        D = 0;
    end
end
ratio就是计算出来的重叠率

函数中的两个参数分别是两个矩形的左上角点的坐标和矩形长,宽。

代码效率很高,写成c也很容易


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值