Shapely:分裂重叠的多边形Python

发布于 2025-02-13 03:27:37 字数 2503 浏览 1 评论 0原文

有什么方法可以将多边形分成几个更简单的形状?

我有这样的多边形: 链接到示例图像

我想分为几个狭窄的矩形形状,例如:

链接我需要的

形状总是由我需要分开的重叠的相当矩形形状的重叠。

我已经尝试使用基于骨架的形状并分割骨骼以能够将我的对象分为部分的水域技术和其他想法,但是它将多边形分为许多较小的多边形,这并不符合我的需求。我想知道是否有人想知道该怎么做?我暂时使用Shapely,Python和OpenCV和Scikit图像。

提前致谢 !

[编辑]:

示例:

from skimage import draw 


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)

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 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()

我只是不知道如何制作然后,转换回到外形多边形。您还可以看到骨骼被拆分了太多。

Is there any way to split a polygon into several simpler shapes?

I have polygons like these :
link to sample image

that I want to split into several narrow rectangular shapes like this :

link to what I would need

The shapes are always made of overlapping fairly rectangular shapes, that I need to split.

I already tried to use watershed techniques and other ideas based on skeletonising the shape and segmenting the skeleton to be able to split my object into parts, but it splits the polygon into many smaller polygons, it doesn't really fit with my needs. I wondered if anyone had an idea of how to do that ? I use shapely, python and opencv and scikit image for the moment.

Thanks in advance !

[EDIT] :

examples :

from skimage import draw 


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)

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()

Ok, now that we have our polygon, let's show what I tried to split it into more basic components :

I tried to use skeletonisation :

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()

and then splitting this skeleton into branches

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

I just don't know how to make the transformation back to shapely polygons then. You can also see that the skeleton is splitted in too many pieces.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文