如何从形状周边的坐标点列表中返回封闭区域的所有坐标点集合?

huangapple go评论65阅读模式
英文:

Given a list of coordinate points for the perimeter of a shape, how do I return the set of all coordinate points for the enclosed area?

问题

以下是翻译好的部分:

我有一个二维坐标列表,表示形状的周边坐标,格式如下:
[[x1, y1], [x2, y2], [x3, y3], ... ,[xn, yn]]

我想要一个函数,该函数将返回由该周边坐标包围的区域的坐标。

背景信息:
周边坐标是围绕图像部分的坐标。我之所以想要这个区域的坐标,是因为当我绘制图像部分时,我想要能够突出显示整个区域。是否有一些能够高效完成这个任务的Python库和函数?

我相信可以使用很多for循环来完成这个任务,但我更愿意使用更高效的方法。

英文:

Python.

I have a 2d list of the coordinates for the perimeter of a shape in the following format:
[[x1, y1], [x2, y2], [x3, y3], ... ,[xn, yn]]

I want a function that will return the coordinates of the area enclosed by that perimeter.

Context:
The perimeter coordinates are the coordinates enclosing a section of an image. The reason that I want the coordinates of the area is because I want to be able to highlight the full area of the section of the image when I plot it. Is there some python library and function that can efficiently do this?

I'm sure this can be done with a lot of for loops, but I'd rather use a more efficient way.

答案1

得分: 0

Here is the translated code snippet:

如果我理解正确您要么需要这些点坐标的凸包要么需要一个边界框所以请检查以下代码看看哪个适合您

```python
from shapely.geometry import Point, MultiPoint

test_coordinates = [[1,1], [2,5], [7, 4], [3, 6], [9, 1], [5,2]]

# 为列表中的每个2D坐标创建点:
point_coordinates = [Point(x[0], x[1]) for x in test_coordinates]

# 创建所有点的单个多点表示:
multi_point = MultiPoint(point_coordinates)

# 凸包:
convex_hull = multi_point.convex_hull

# 凸包的坐标 - 请参阅下面的输出/注释
print(convex_hull.exterior.coords.xy)
#Out[23]: 
#(array('d', [1.0, 2.0, 3.0, 7.0, 9.0, 1.0]),
# array('d', [1.0, 5.0, 6.0, 4.0, 1.0, 1.0]))

# 边界框:
bounding_box = multi_point.bounds
print(bounding_box) 
# (1.0, 1.0, 9.0, 6.0)

因此,在以下图中,坐标是蓝色点,凸包是红色多边形,边界框是蓝色矩形:
如何从形状周边的坐标点列表中返回封闭区域的所有坐标点集合?

要创建图表,以防您想查看您的数据:


import matplotlib.pyplot as plt
from shapely.geometry import box
# 绘制点
plt.scatter([point.x for point in point_coordinates], [point.y for point in point_coordinates])
# 绘制凸包
plt.plot(*convex_hull.exterior.xy, label='convex hull', color='red')
# 绘制边界框
plt.plot(*box(*bounding_box, ccw=True).exterior.xy, label='bounding_box', color='green')
# 将它们全部显示在一起!
plt.show()

This code is now translated into Chinese.

<details>
<summary>英文:</summary>

If I get it correctly, you either want a convex hull of those point coordinates, or a bounding box. So, check the following code and see which one suits you:

from shapely.geometry import Point, MultiPoint

test_coordinates = [[1,1], [2,5], [7, 4], [3, 6], [9, 1], [5,2]]

create points for each 2D coordinates in the list:

point_coordinates = [Point(x[0], x1) for x in test_coordinates]

create a single multipoint representation of all points:

multi_point = MultiPoint(point_coordinates)

convex hull:

convex_hull = multi_point.convex_hull

coordinates of the convex hull - see the output below / commented

print(convex_hull.exterior.coords.xy)
#Out[23]:
#(array('d', [1.0, 2.0, 3.0, 7.0, 9.0, 1.0]),

array('d', [1.0, 5.0, 6.0, 4.0, 1.0, 1.0]))

bounding box:

bounding_box = multi_point.bounds
print(bounding_box)

(1.0, 1.0, 9.0, 6.0)



So, in the following figure, the coordinates are the blue points, convex hull is the red polygon and bounding box is the blue rectangle:
[![enter image description here][1]][1]


To create the plots, in case you want to see for your data:

import matplotlib.pyplot as plt
from shapely. Geometry import box

plot points

plt.scatter([point.x for point in point_coordinates], [point.y for point in point_coordinates])

plot convex hull

plt.plot(*convex_hull.exterior.xy, label='convex hull', color='red')

plot bounding box

plt.plot(*box(*bounding_box, ccw=True).exterior.xy, label='bounding_box', color='green')

show it all together!

plt.show()



  [1]: https://i.stack.imgur.com/g7PMl.png

</details>



huangapple
  • 本文由 发表于 2023年6月29日 10:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定