如何使用Python镜像多边形?

问题描述 投票:0回答:2

我有一组图像,用于绘制多边形。我有这些多边形的点,我使用Shapely绘制这些点并检查来自眼动仪的某些点是否落入多边形。

现在,其中一些图像是镜像的,但我没有绘制多边形的坐标。如何水平翻转多边形?有没有办法用Shapely做到这一点?

python shapely
2个回答
1
投票

如果你想相对于垂直轴反射一个多边形,即水平翻转它,一个选项是使用scale提供的shapely.affinity变换(使用负单位比例因子)或使用自定义变换:

from shapely.affinity import scale
from shapely.ops import transform
from shapely.geometry import Polygon

def reflection(x0):
    return lambda x, y: (2*x0 - x, y)

P = Polygon([[0, 0], [1, 1], [1, 2], [0, 1]])
print(P)
#POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0))

Q1 = scale(P, xfact = -1, origin = (1, 0))
Q2 = transform(reflection(1), P)

print(Q1)
#POLYGON ((2 0, 1 1, 1 2, 2 1, 2 0))
print(Q2)
#POLYGON ((2 0, 1 1, 1 2, 2 1, 2 0))

1
投票

通过乘以[[1,0], [0,-1]],您可以获得垂直翻转的形状。 (我在jupyter笔记本上测试过这个)

pts = np.array([[153, 347],
                [161, 323],
                [179, 305],
                [195, 315],
                [184, 331],
                [177, 357]])
display(Polygon(pts))
display(Polygon(pts.dot([[1,0],[0,-1]])))

jupyter_notebook_result

如果你乘以[[-1,0],[0,1]],你会得到水平翻转的形状。

请参阅linear transformation以了解其工作原理。

© www.soinside.com 2019 - 2024. All rights reserved.