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
import numpy as np
from matplotlib.path import Path
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
path = Path(poly_verts)
grid = path.contains_points(points)
grid = grid.reshape((ny,nx))
print grid
from PIL import Image, ImageDraw
img = Image.new('L', (width, height), 0) # The Zero is to Specify Background Color
draw = ImageDraw.Draw(img)
for vertex in range(len(vertexlist)):
startpoint = vertexlist[vertex]
try: endpoint = vertexlist[vertex+1]
except IndexError: endpoint = vertexlist[0]
# The exception means We have reached the end and need to complete the polygon
draw.line((startpoint[0], startpoint[1], endpoint[0], endpoint[1]), fill=1)
# If you want the result as a single list
# You can make a two dimensional list or dictionary by iterating over the height and width variable
list(img.getdata())
# If you want the result as an actual Image
img.save('polgon.jpg', 'JPEG')
def poly_mask(shape, *vertices, value=np.nan):
"""
Create a mask array filled with 1s inside the polygon and 0s outside.
The polygon is a list of vertices defined as a sequence of (column, line) number, where the start values (0, 0) are in the
upper left corner. Multiple polygon lists can be passed in input to have multiple,eventually not connected, ROIs.
column, line # x, y
vertices = [(x0, y0), (x1, y1), ..., (xn, yn), (x0, y0)] or [x0, y0, x1, y1, ..., xn, yn, x0, y0]
Note: the polygon can be open, that is it doesn't have to have x0,y0 as last element.
adapted from: https://stackoverflow.com/questions/3654289/scipy-create-2d-polygon-mask/64876117#64876117
:param shape: (tuple) shape of the output array (height, width)
:param vertices: (list of tuples of int): sequence of vertices defined as
[(x0, y0), (x1, y1), ..., (xn, yn), (x0, y0)] or
[x0, y0, x1, y1, ..., xn, yn, x0, y0]
Multiple lists (for multiple polygons) can be passed in input
:param value: (float or NAN) The masking value to use (e.g. a very small number). Default: np.nan
:return: (ndarray) the mask array
"""
width, height = shape[::-1]
# create a binary image
img = Image.new(mode='L', size=(width, height), color=0) # mode L = 8-bit pixels, black and white
draw = ImageDraw.Draw(img)
# draw polygons
for polygon in vertices:
draw.polygon(polygon, outline=1, fill=1)
# replace 0 with 'value'
mask = np.array(img).astype('float32')
mask[np.where(mask == 0)] = value
return mask
而不是(width,height),我更喜欢直接使用shape作为输入,这样我就可以像这样使用它:
polygon_lists = [
[(x0, y0), (x1, y1), ..., (xn, yn), (x0, y0)],
[# ... another sequence of coordinates...],
[# ...yet another sequence of coordinates...],
]
my_mask = poly_mask(my_array.shape, *polygon_lists)
7条答案
按热度按时间ivqmmu1c1#
答案很简单:
ycggw6v22#
作为@Anil的答案的一个稍微更直接的替代方案,matplotlib有
matplotlib.nxutils.points_inside_poly
,可以用来快速绘制任意多边形。例如这会产生(一个布尔numpy数组):
您应该能够很好地将
grid
传递给任何scipy.ndimage.morphology函数。hrysbysz3#
关于乔的评论Matplotlib API在评论发布后已经更改,现在您需要使用子模块
matplotlib.path
提供的方法。工作代码在下面。
gjmwrych4#
作为 *@Yusuke N. * 答案的一个轻微替代,考虑使用
matplotlib.path
,它与from PIL import Image, ImageDraw
的效率一样高(不需要安装Pillow
,不需要考虑integer
或float
。有用的我?)工作代码如下:
结果图像如下,其中暗区为
False
,亮区为True
。pieyvz9o5#
你可以尝试使用python的图像库PIL。首先初始化画布。然后创建一个绘图对象,并开始绘制线条。这是假设多边形位于R^2中,并且输入的顶点列表顺序正确。
输入= [(x1,y1),(x2,y2),.,(xn,yn)],(width,height)
这是你要找的东西吗,还是别的问题?
8i9zcol26#
下面是一个实现@IsaacSutherland方法(公认的答案)的函数,我发现一些有用的修改。欢迎评论!
poly_mask()
接受多个多边形作为输入,以便输出掩码可以由多个最终不连接的多边形区域组成。此外,因为在某些情况下0不是用于掩蔽的好值(例如,如果0是要应用掩码的数组的有效值),我添加了一个value
关键字,用于设置实际的掩码值(例如,一个非常小/大的数字或NAN):为了实现这一点,掩码被转换为浮点数组。而不是(width,height),我更喜欢直接使用
shape
作为输入,这样我就可以像这样使用它:其中
my_array
是必须应用掩模的阵列(当然,或者具有相同形状的另一个阵列)。0h4hbjxa7#
下面是一个cv2版本: