python Shapely 使用指南
刚从学习了Shapely包使用,怕忘记,在这里记录一下。
阅读目录
1、引入包
from shapely.geometry import Point
from shapely.geometry import LineString
2、共有的变量和方法
object.area
Returns the area (float) of the object.
object.bounds
返回对象的(minx,miny,maxx,maxy)元组(float类型)
object.length
返回对象的长度
object.geom_type
返回对象类型
object.distance(other)
返回本对象和另一个对象的距离
object.representative_point()
Returns a cheaply computed point that is guaranteed to be within the geometric object.
>>> from shapely.geometry import Point
>>> print Point(0,0).distance(Point(0,1))
1.0
>>> from shapely.geometry import LineString
>>> line = LineString([(0,0), (1,1), (1,2)])
>>> line.area
0.0
>>> line.bounds
(0.0, 0.0, 1.0, 2.0)
>>> line.length
2.414213562373095
>>> line.geom_type
'LineString'
3、Point
class Point(coordinates)
三种赋值方式
>>> point = Point(0,0)
>>> point_2 = Point((0,0))
>>> point_3 = Point(point)
一个点对象有area和长度都为0
>>> point.area
0.0
>>> point.length
0.0
坐标可以通过coords或x、y、z得到
>>> p = Point(2,3)
>>> p.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d60dd0>
>>> list(p.coords)
[(2.0, 3.0)]
>>> p.x
2.0
>>> p.y
3.0
coords可以被切片
>>> p.coords[:]
[(2.0, 3.0)]
4、LineStrings
LineStrings构造函数传入参数是2个或多个点序列
一个LineStrings对象area为0,长度非0
>>> line = LineString([(0,0), (0,1), (1,2)])
>>> line.area
0.0
>>> line.length
2.414213562373095
获得坐标
>>> line.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
>>> list(line.coords)
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
LineString依然可以接受一个同类型对象
>>> line2 = LineString(line)
>>> line2.coords[:]
[(0.0, 0.0), (0.0, 1.0), (1.0, 2.0)]
5、常见格式转换
wkt: Well Know Text
wkb: Well Kown Binary
>>> Point(1,1).wkt
'POINT (1 1)'
>>> Point(1,1).wkb
'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'
>>>
>>> Point(1,1).wkb.encode('hex')
'0101000000000000000000f03f000000000000f03f'
两者都有loads和dumps方法
对于wkt
>>> from shapely.wkt import dumps, loads
>>> s = dumps(Point(1,2))
>>> s
'POINT (1.0000000000000000 2.0000000000000000)'
>>> ss = loads(s)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]
对于wkb
>>> from shapely.wkb import dumps, loads
>>> s = dumps(Point(1,2), hex=True)
>>> s
'0101000000000000000000F03F0000000000000040'
>>> ss = loads(s, hex=True)
>>> ss
<shapely.geometry.point.Point object at 0x7ffbc3d78790>
>>> ss.coords
<shapely.coords.CoordinateSequence object at 0x7ffbc3d783d0>
>>> ss.coords[:]
[(1.0, 2.0)]
补充代码:
# ------------------------------------------------------------------------------------------------------------------
# 在目标检测中一个很重要的问题就是NMS及IOU计算,而一般所说的目标检测检测的box是规则矩形框,计算IOU也非常简单,有两种方法:
# 1. 两个矩形的宽之和减去组合后的矩形的宽就是重叠矩形的宽,同比重叠矩形的高
# IOU = 交集部分/包含两个四边形最小多边形的面积
# 2. 右下角的minx减去左上角的maxx就是重叠矩形的宽,同比高
# IOU = 重叠面积 / (两矩形面积和—重叠面积)
# 不规则四边形就不能通过这种方式来计算,python的shapely包可以直接做到,下面给出的代码和注释
# 来自:白翔老师的textBoxes++论文源码,
# ------------------------------------------------------------------------------------------------------------------
import numpy as np
import shapely
from shapely.geometry import Polygon, MultiPoint # 多边形
line1 = [2, 0, 2, 2, 0, 0, 0, 2] # 四边形四个点坐标的一维数组表示,[x,y,x,y....];随意分别放入框的四个角坐标
a = np.array(line1).reshape(4, 2) # 四边形二维坐标表示
poly1 = Polygon(a).convex_hull # python四边形对象,会自动计算四个点,最后四个点顺序为:左上 左下 右下 右上 左上
print(Polygon(a).convex_hull) # 可以打印看看是不是这样子(0 0, 0 2, 2 2, 2 0, 0 0)
line2 = [1, 1, 4, 1, 4, 4, 1, 4]
b = np.array(line2).reshape(4, 2)
poly2 = Polygon(b).convex_hull
print(Polygon(b).convex_hull)
union_poly = np.concatenate((a, b)) # 合并两个box坐标,变为8*2
print(union_poly)
print(MultiPoint(union_poly).convex_hull) # 包含两四边形最小的多边形点;(0 0, 0 2, 1 4, 4 4, 4 1, 2 0, 0 0)
if not poly1.intersects(poly2): # 如果两四边形不相交
iou = 0
else:
try:
inter_area = poly1.intersection(poly2).area # 相交面积
print(inter_area)
# union_area = poly1.area + poly2.area - inter_area
union_area = MultiPoint(union_poly).convex_hull.area # 最小多边形点面积
print(union_area)
if union_area == 0:
iou = 0
# iou = float(inter_area) / (union_area-inter_area) #错了
iou = float(inter_area) / union_area
# iou=float(inter_area) /(poly1.area+poly2.area-inter_area)
# 源码中给出了两种IOU计算方式,第一种计算的是: 交集部分/包含两个四边形最小多边形的面积
# 第二种: 交集 / 并集(常见矩形框IOU计算方式)
except shapely.geos.TopologicalError:
print('shapely.geos.TopologicalError occured, iou set to 0')
iou = 0
print(a)
print(iou)