非矩形四边形的并集交集
Intersection over union on non rectangular quadrilaterals
我正在处理停车 space 检测的问题。为了检测空车位 space,我使用的是交集而不是并集。但是,停车位 space 并不总是矩形的。所以,我做了一个标注工具,可以绘制各种形状的多边形。现在,我想知道是否有任何 python 库提供 IOU 功能?如果没有,还有其他选择吗?
您应该使用 shapely
Python 库:
from shapely.geometry import box, Polygon
# Define Each polygon
pol1_xy = [[130, 27], [129.52, 27], [129.45, 27.1], [130.13, 26]]
pol2_xy = [[30, 27.200001], [129.52, 27.34], [129.45, 27.1], [130.13, 26.950001]]
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate Intersection and union, and tne IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.union(polygon2_shape).area
IOU = polygon_intersection / polygon_union
可以稍作改进。找到我们两个四边形的并集是一项代价高昂的操作。相反,我们可以通过首先将两个四边形的面积相加来找到并集的面积。这将 over-count 四边形重叠的面积,但我们可以通过减去我们已经计算出的相交面积来纠正此问题:
def IOU(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and the IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.area + polygon2_shape.area - polygon_intersection
return polygon_intersection / polygon_union
以下代码验证此改进是否提供相同的结果,然后对两个版本进行计时。函数 IOU1
包含 ibarrond 的代码,函数 IOU2
包含改进。
from shapely.geometry import Polygon
from timeit import timeit
def IOU1(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and the IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.union(polygon2_shape).area
return polygon_intersection / polygon_union
def IOU2(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and tne IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.area + polygon2_shape.area - polygon_intersection
return polygon_intersection / polygon_union
if __name__ == '__main__':
# Define test coordinates:
pol1_xy = [[130, 27], [129.52, 27], [129.45, 27.1], [130.13, 26]]
pol2_xy = [[30, 27.200001], [129.52, 27.34], [129.45, 27.1], [130.13, 26.950001]]
# Test that results are the same (except for minor rounding differences):
assert abs(IOU1(pol1_xy, pol2_xy) - IOU2(pol1_xy, pol2_xy)) < 1e-16
# Determine speeds of both functions:
t1=timeit('IOU1(pol1_xy, pol2_xy)', number=100000,
setup='from __main__ import IOU1, pol1_xy, pol2_xy')
t2=timeit('IOU2(pol1_xy, pol2_xy)', number=100000,
setup='from __main__ import IOU2, pol1_xy, pol2_xy')
print('time for IOU1: %s' %t1)
print('time for IOU2: %s' %t2)
这是我得到的结果:
time for IOU1: 20.0208661
time for IOU2: 11.0288122
请注意,具体时间会因硬件和当前背景而异activity。
我正在处理停车 space 检测的问题。为了检测空车位 space,我使用的是交集而不是并集。但是,停车位 space 并不总是矩形的。所以,我做了一个标注工具,可以绘制各种形状的多边形。现在,我想知道是否有任何 python 库提供 IOU 功能?如果没有,还有其他选择吗?
您应该使用 shapely
Python 库:
from shapely.geometry import box, Polygon
# Define Each polygon
pol1_xy = [[130, 27], [129.52, 27], [129.45, 27.1], [130.13, 26]]
pol2_xy = [[30, 27.200001], [129.52, 27.34], [129.45, 27.1], [130.13, 26.950001]]
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate Intersection and union, and tne IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.union(polygon2_shape).area
IOU = polygon_intersection / polygon_union
def IOU(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and the IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.area + polygon2_shape.area - polygon_intersection
return polygon_intersection / polygon_union
以下代码验证此改进是否提供相同的结果,然后对两个版本进行计时。函数 IOU1
包含 ibarrond 的代码,函数 IOU2
包含改进。
from shapely.geometry import Polygon
from timeit import timeit
def IOU1(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and the IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.union(polygon2_shape).area
return polygon_intersection / polygon_union
def IOU2(pol1_xy, pol2_xy):
# Define each polygon
polygon1_shape = Polygon(pol1_xy)
polygon2_shape = Polygon(pol2_xy)
# Calculate intersection and union, and tne IOU
polygon_intersection = polygon1_shape.intersection(polygon2_shape).area
polygon_union = polygon1_shape.area + polygon2_shape.area - polygon_intersection
return polygon_intersection / polygon_union
if __name__ == '__main__':
# Define test coordinates:
pol1_xy = [[130, 27], [129.52, 27], [129.45, 27.1], [130.13, 26]]
pol2_xy = [[30, 27.200001], [129.52, 27.34], [129.45, 27.1], [130.13, 26.950001]]
# Test that results are the same (except for minor rounding differences):
assert abs(IOU1(pol1_xy, pol2_xy) - IOU2(pol1_xy, pol2_xy)) < 1e-16
# Determine speeds of both functions:
t1=timeit('IOU1(pol1_xy, pol2_xy)', number=100000,
setup='from __main__ import IOU1, pol1_xy, pol2_xy')
t2=timeit('IOU2(pol1_xy, pol2_xy)', number=100000,
setup='from __main__ import IOU2, pol1_xy, pol2_xy')
print('time for IOU1: %s' %t1)
print('time for IOU2: %s' %t2)
这是我得到的结果:
time for IOU1: 20.0208661
time for IOU2: 11.0288122
请注意,具体时间会因硬件和当前背景而异activity。