SciPy创建2D多边形蒙版 [英] SciPy Create 2D Polygon Mask
本文介绍了SciPy创建2D多边形蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用标准Python包创建一个表示多边形的二进制掩码的numpy 2D数组.
I need to create a numpy 2D array which represents a binary mask of a polygon, using standard Python packages.
- 输入:多边形顶点,图像尺寸
- 输出:多边形(numpy二维数组)的二进制掩码
(更大的上下文:我想使用scipy.ndimage.morphology.distance_transform_edt获得该多边形的距离变换.)
(Larger context: I want to get the distance transform of this polygon using scipy.ndimage.morphology.distance_transform_edt.)
有人可以告诉我该怎么做吗?
Can anyone show me how to do this?
推荐答案
答案很简单:
import numpy
from PIL import Image, ImageDraw
# polygon = [(x1,y1),(x2,y2),...] or [x1,y1,x2,y2,...]
# width = ?
# height = ?
img = Image.new('L', (width, height), 0)
ImageDraw.Draw(img).polygon(polygon, outline=1, fill=1)
mask = numpy.array(img)
这篇关于SciPy创建2D多边形蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文