一.利用shapely求polygon面积
import shapelyfrom shapely.geometry import Polygon, MultiPoint # 多边形# box1 = [2, 0, 4, 2, 2, 4, 0, 2, 0, 0]box1 = [2, 0, 4, 2, 2, 4, 0, 2, 2, 2]poly_box1 = Polygon(np.array(box1).reshape(-1,2))print(poly_box1)print(poly_box1.area)
二.逆时针调整为顺时针
1.四个点
def polygon_area1():'''compute area of a polygon:param poly::return:'''# # #顺时针# poly = np.array([[2, 0],# [4, 2],# [2, 4],# [0, 2]])# 逆时针poly = np.array([[2, 0],[0, 2],[2, 4],[4, 2]])poly_h = poly.shape[0]edge = []for i in range(poly_h - 1):edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))# edge = [# (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),# (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),# (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),# (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])# ]area = np.sum(edge) / 2.print('=====The first way======')print('area:', area)if area <0:index = [0] + [i for i in range(poly_h - 1, 0, -1)]fix_poly = poly[index, :]print('fix_poly:', fix_poly)
def polygon_area2():'''compute area of a polygon:param poly::return:'''# # #顺时针# poly = np.array([[2, 0],# [4, 2],# [2, 4],# [0, 2]])# 逆时针poly = np.array([[2, 0],[0, 2],[2, 4],[4, 2]])edge = []poly_h = poly.shape[0]for i in range(poly_h-1):edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])print('=====The second way======')# print('edge:', edge)area = np.sum(edge)/2.print('area:', area)#如果是逆时针 调整为顺时针if area < 0:index = [0] + [i for i in range(poly_h-1, 0, -1)]# print(index)fix_poly = poly[index, :]print('fix_poly:', fix_poly)
if __name__ == '__main__':# show_image()# compute_IOU_()polygon_area1()polygon_area2()
2.多个点
def polygon_area1():'''compute area of a polygon:param poly::return:'''# # #顺时针# poly = np.array([[2, 0],# [4, 2],# [2, 4],# [0, 2]])# 逆时针 凹多边形print('凹多边形')poly = np.array([[2, 0],[2, 2],[0, 2],[2, 4],[4, 2]])# 逆时针 凸多边形# print('凸多边形')# poly = np.array([[2, 0],# [0, 0],# [0, 2],# [2, 4],# [4, 2]])poly_h = poly.shape[0]edge = []for i in range(poly_h - 1):edge.append((poly[i][0] - poly[i+1][0])*(poly[i][1] + poly[i + 1][1]))edge.append((poly[poly_h-1][0] - poly[0][0])*(poly[poly_h-1][1]+poly[0][1]))# edge = [# (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),# (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),# (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),# (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])# ]area = np.sum(edge) / 2.print('=====The first way======')print('area:', area)if area <0:print('此为逆时针,调整为顺时针')index = [0] + [i for i in range(poly_h - 1, 0, -1)]fix_poly = poly[index, :]print('fix_poly:', fix_poly)
def polygon_area2():'''compute area of a polygon:param poly::return:'''# # #顺时针# poly = np.array([[2, 0],# [4, 2],# [2, 4],# [0, 2]])# 逆时针 凹多边形print('凹多边形')poly = np.array([[2, 0],[2, 2],[0, 2],[2, 4],[4, 2]])# 逆时针 凸多边形# print('凸多边形')# poly = np.array([[2, 0],# [0, 0],# [0, 2],# [2, 4],# [4, 2]])edge = []poly_h = poly.shape[0]for i in range(poly_h-1):edge.append(poly[i][0] * poly[i+1][1] - poly[i+1][0] * poly[i][1])edge.append(poly[poly_h-1][0] * poly[0][1] - poly[0][0] * poly[poly_h-1][1])print('=====The second way======')# print('edge:', edge)area = np.sum(edge)/2.print('area:', area)#如果是逆时针 调整为顺时针if area < 0:print('此为逆时针,调整为顺时针')index = [0] + [i for i in range(poly_h-1, 0, -1)]# print(index)fix_poly = poly[index, :]print('fix_poly:', fix_poly)
if __name__ == '__main__':# show_image()# compute_IOU_()polygon_area1()polygon_area2()
三.计算不规则多边形面积
分解成梯形,面积依次相加
import numpy as np
points = np.array([[0.72, 2.28],[2.66, 4.71],[5, 3.5],[3.63, 2.52],[4, 1.6],[1.9, 1]])
print(points.shape)
h, w = points.shape
areas = []
for i in range(0, points.shape[0]):area = (points[(i+1) % h][-1] + points[i][-1])*(points[(i+1) % h][0] - points[i][0])/2areas.append(area)print(points[i])print(points[(i+1) % h])
print('==areas:', areas)
print(sum(areas))
参考:https://www.cnblogs.com/TenosDoIt/p/4047211.html