SciPy 创建 2D 多边形遮罩 [英] SciPy Create 2D Polygon Mask

查看:37
本文介绍了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 2D 数组)

(更大的上下文:我想使用 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.)

谁能告诉我怎么做?

推荐答案

结果很简单:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆