4p-在一张图片中根据矩形四个点的坐标计算两个矩形是否相交

使用场景:勾选框提取字段值等。

def mat_inter(box1, box2):
    # box1,box2是否相交
    x01, y01, x02, y02 = box1  # (x01,y01),(x02,y02) 对角线的点坐标
    x11, y11, x12, y12 = box2

    lx = abs((x01 + x02)/2 - (x11 + x12)/2)
    ly = abs((y01 + y02)/2 - (y11 + y12)/2)
    sax = abs(x01 - x02)
    sbx = abs(x11 - x12)
    say = abs(y01 - y02)
    sby = abs(y11 - y12)

    if lx <= (sax + sbx)/2 and ly <= (say + sby)/2:
        return 1
    else:
        return 0

 

posted @ 2021-10-13 16:33  清风_Z  阅读(99)  评论(0编辑  收藏  举报