英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论