【问题标题】:How to plot lines around images in Matplotlib如何在 Matplotlib 中的图像周围绘制线条
【发布时间】:2017-12-06 23:21:06
【问题描述】:

我有两个列表,其中包含要使用 matplotlib 绘制的点的坐标:hx 和 hy,并且我有一个将这些点绘制为图像的函数:plotImage。

现在我想在这些点之间画一条线,但这条线应该以某种方式绕过图片。它不必完全通过图像的边界,重要的是它不会与图片重叠。

这是一个示例,绿线是我现在拥有的(它的凸包),红线是该线应该是什么样子的示例。

这是我的代码:

def plotImage(xData, yData, im):
    for x, y in zip(xData, yData):
        bb = Bbox.from_bounds(x,y,10,10)
        bb2 = TransformedBbox(bb,ax.transData)
        bbox_image = BboxImage(bb2,
                            norm = None,
                            origin=None,
                            clip_on=False)

        bbox_image.set_data(im)
        ax.add_artist(bbox_image)

fig=plt.figure()
ax=fig.add_subplot(111)
img = plt.imread('pic.png')

gx = list(map(lambda x: x - 5, hx)) #  so the center of the picture is in the point I want to plot 
gy = list(map(lambda y: y - 5, hy)) #(without this, the image is drawn so that the picture's left bottom corner is in my point)

plotImage(gx,gy,img)
plt.plot(hx, hy, 'g-', markersize=10)

所以我的问题是:如何计算这个红色的船体?

【问题讨论】:

标签: python matplotlib convex-hull


【解决方案1】:

可以将使用Incrementing area of convex hull 的示例应用于您的案例。

from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
import numpy as np

gx = [-2, 2, -2, 2, 1, -0.5]
gy = [3, 4, -2, -1, -1, 0.5]

points = np.array((gx, gy)).T
convh = ConvexHull(points)
stretchCoef = 1.2
pointsStretched = points[convh.vertices]*stretchCoef

gx_new = pointsStretched[..., 0]
gy_new = pointsStretched[..., 1]

【讨论】:

  • 这对点非常有效,但是当我用图片尝试它时,它通常仍然与其中一些重叠:i.imgur.com/RFYksbc.jpg 我试图从船体内部的一个点获取向量并添加一个该方向的向量到所有船体点
猜你喜欢
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多