求IOU

def isPointinPolygon(point, rangelist):  # [[0,0],[1,1],[0,1],[0,0]] [1,0.8]
    # 判断是否在外包矩形内,如果不在,直接返回false
    lnglist = []
    latlist = []
    for i in range(len(rangelist) - 1):
        lnglist.append(rangelist[i][0])
        latlist.append(rangelist[i][1])
    # print(lnglist, latlist)
    maxlng = max(lnglist)
    minlng = min(lnglist)
    maxlat = max(latlist)
    minlat = min(latlist)
    # print(maxlng, minlng, maxlat, minlat)
    if (point[0] > maxlng or point[0] < minlng or
            point[1] > maxlat or point[1] < minlat):
        return False
    count = 0
    point1 = rangelist[0]
    for i in range(1, len(rangelist)):
        point2 = rangelist[i]
        # 点与多边形顶点重合
        if (point[0] == point1[0] and point[1] == point1[1]) or (point[0] == point2[0] and point[1] == point2[1]):
            # print("在顶点上")
            return True
        # 判断线段两端点是否在射线两侧 不在肯定不相交 射线(-∞,lat)(lng,lat)
        if (point1[1] < point[1] <= point2[1]) or (point1[1] >= point[1] > point2[1]):
            # 求线段与射线交点 再和lat比较
            point12lng = point2[0] - (point2[1] - point[1]) * (point2[0] - point1[0]) / (point2[1] - point1[1])
            # print(point12lng)
            # 点在多边形边上
            if point12lng == point[0]:
                # print("点在多边形边上")
                return True
            if point12lng < point[0]:
                count += 1
        point1 = point2
    # print(count)
    if count % 2 == 0:
        return False
    else:
        return True


if __name__ == '__main__':
    area1 = 0
    area2 = 0
    areaAll = 0
    x = 0
    s1 = [[339, 357],
          [352, 201],
          [587, 208],
          [586, 367],
          [339, 357]
          ]

    s2 = [[337, 358],
          [349, 201],
          [584, 209],
          [583, 367],
          [337, 358]
          ]

    for i in range(960):
        for j in range(540):

            if isPointinPolygon([i, j], s1):
                area1 += 1
            if isPointinPolygon([i, j], s2):
                area2 += 1
            if isPointinPolygon([i, j], s1) and isPointinPolygon([i, j], s2):
                x += 1
            if isPointinPolygon([i, j], s1) or isPointinPolygon([i, j], s2):
                areaAll += 1
    print(f"areaAll是:{areaAll}")
    print(f"area1是:{area1}")
    print(f"area2是:{area2}")
    print(f"x是:{x}")
    print(f"重合率是{x / areaAll}")

  

上一篇:内置函数——hasattr() 函数


下一篇:qdoc 写法