1

我正在尝试获取此图像轮廓的填充二进制掩码。图像的轮廓

我看了一下这个问题SciPy Create 2D Polygon Mask;但是它似乎不喜欢我的数据集。

import numpy as np
from matplotlib.nxutils import points_inside_poly

nx, ny = 10, 10
poly_verts = [(1,1), (5,1), (5,9),(3,2),(1,1)]

# Create vertex coordinates for each grid cell...
# (<0,0> is at the top left of the grid in this system)
x, y = np.meshgrid(np.arange(nx), np.arange(ny))
x, y = x.flatten(), y.flatten()

points = np.vstack((x,y)).T

grid = points_inside_poly(points, poly_verts)
grid = grid.reshape((ny,nx))

print grid

我想知道是否有另一种方法可以尝试返回二进制掩码或有人来解释 points_inside_poly 的局限性

因为它似乎最终会变成这样badMaskScatter

4

1 回答 1

2

我不确定您最后要绘制什么,但是您的示例对我有用:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.nxutils import points_inside_poly
from itertools import product, compress

pv = [(1,1),(5,1),(5,9),(3,2),(1,1)]

x, y = np.meshgrid(np.arange(10),np.arange(10))
x, y = x.flatten(), y.flatten()

xy = np.vstack((x,y)).T

grid = points_inside_poly(xy,pv)

xv, yv = zip(*pv)
xp, yp = zip(*compress(xy,grid))

plt.plot(xp,yp,'o',color='red',label='points')
plt.plot(xv,yv,'o',color='blue',label='vertices')
plt.xlim((0,10))
plt.ylim((0,10))
plt.legend()
plt.show()

points_inside_poly

于 2012-06-28T04:15:39.860 回答