def rectangle_to_polygon(rect):
print(rect)
resBoxes = np.empty([1, 8], dtype='int32')
resBoxes[0, 0] = int(rect[0])
resBoxes[0, 4] = int(rect[3])
resBoxes[0, 1] = int(rect[0])
resBoxes[0, 5] = int(rect[1])
resBoxes[0, 2] = int(rect[2])
resBoxes[0, 6] = int(rect[1])
resBoxes[0, 3] = int(rect[2])
resBoxes[0, 7] = int(rect[3])
pointMat = resBoxes.reshape([2, 4]).T
print(pointMat)
return plg.Polygon(pointMat)
def get_intersection(pD, pG):
pInt = pD & pG
if len(pInt) == 0:
return 0
return pInt.area()
def get_union(pD, pG):
# print("pd=",pD)
# print("pg=",pG)
areaA = pD.area()
areaB = pG.area()
# input()
return areaA + areaB - get_intersection(pD, pG)
def get_intersection_over_union(pD, pG):
return get_intersection(pD, pG) / get_union(pD, pG)
boxes = (i.data.cpu().numpy() * 224).astype(np.int32) # 还原预测坐标并将其转化为无符号整型
target_box = (target[j, 0:4].data.cpu().numpy() * 224).astype(np.int32) # 还原目标坐标并将其转化为无符号整型
img = trans.ToPILImage()(x[j]) # 转换图片
de = rectangle_to_polygon(boxes.tolist()) # 坐标集合
gt = rectangle_to_polygon(target_box.tolist()) # 坐标集合
iou = get_intersection_over_union(de, gt)
total_iou += iou
rectangle_to_polygon 功能
如下:
将[35, 54, 186, 184]
转化为:
[[ 35 184]
[ 35 54]
[186 54]
[186 184]]
【程序】IOU 方法2 利用python内部实现的函数求面积、长度、交叉面积等
最新推荐文章于 2023-06-01 21:00:56 发布