You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python基于Shapely拆分重叠多边形为简单矩形的实现方法

多边形拆分为窄矩形的实现方案咨询

问题描述

是否存在方法可将多边形拆分为多个更简单的几何形状?

  • 待处理多边形示例:原始多边形样图
  • 拆分目标:将多边形拆分为多个窄矩形形状,效果参考:目标拆分效果样图
  • 待处理图形特征:所有形状均由相互重叠的近似矩形图形构成
  • 已尝试方案:分水岭算法、基于形状骨架化并分割骨架的拆分思路,但上述方法会将多边形拆分为过多细碎小多边形,无法匹配需求
  • 当前使用技术栈:shapely、Python、OpenCV、scikit-image

补充测试代码

基础多边形构造代码

from skimage import draw 
import numpy as np
from shapely.geometry import Polygon
import matplotlib.pyplot as plt

polygonCoords = np.array([[1876. , 1151.5],
       [1868.5, 1143. ],
       [1864.5, 1123. ],
       [1859.5, 1116. ],
       [1864. , 1116.5],
       [1868. , 1121.5],
       [1870. , 1119.5],
       [1871.5, 1121. ],
       [1871.5, 1141. ],
       [1872.5, 1144. ],
       [1877. , 1147.5],
       [1888.5, 1137. ],
       [1888.5, 1125. ],
       [1891. , 1117.5],
       [1892.5, 1119. ],
       [1891.5, 1130. ],
       [1893. , 1135.5],
       [1899.5, 1126. ],
       [1898.5, 1112. ],
       [1900.5, 1112. ],
       [1901. , 1121.5],
       [1904. , 1121.5],
       [1910. , 1116.5],
       [1909.5, 1120. ],
       [1902.5, 1129. ],
       [1906.5, 1145. ],
       [1904.5, 1146. ],
       [1900.5, 1133. ],
       [1899. , 1132.5],
       [1894. , 1137.5],
       [1892.5, 1137. ],
       [1891.5, 1143. ],
       [1893.5, 1146. ],
       [1890. , 1149.5],
       [1887.5, 1148. ],
       [1888.5, 1145. ],
       [1886. , 1143.5],
       [1882.5, 1148. ],
       [1883. , 1150.5],
       [1879. , 1149.5],
       [1876. , 1151.5]])


polygonExample = Polygon(polygonCoords)
xmin, ymin, xmax, ymax = polygonExample.bounds
height = ymax-ymin
width = xmax-xmin
shape = (int(np.ceil(width)), int(np.ceil(height)))


poly_coordinates = np.array(list(polygonExample.exterior.coords))
newPolyCoordinates = (poly_coordinates -[xmin, ymin]).astype(np.uint16)


a_mask = draw.polygon2mask(shape,  newPolyCoordinates)


plt.imshow(a_mask)
plt.show()

已尝试的骨架化实现代码

from skimage.morphology import medial_axis
from plantcv import plantcv as pcv

skel, distance = medial_axis(a_mask, return_distance=True)
plt.imshow(skel)
plt.show()
plt.imshow(distance)
plt.show()

骨架分支分割代码

pcv.params.line_thickness = 3
segmented_img, obj = pcv.morphology.segment_skeleton(skel_img=skel.astype(np.uint8))
plt.imshow(segmented_img)
plt.show()

当前待解决的核心问题

  • 分割后得到的栅格格式结果,如何转换回shapely多边形对象
  • 现有骨架拆分逻辑会生成过多细碎片段,无法得到符合要求的窄矩形拆分结果,需要可行的优化实现方案

内容的提问来源于stack exchange,提问作者Reyrem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 11:45:36