用法
polygon(r, c, shape=None)
生成多边形内的像素坐标。
参数
r:(N,) ndarray。
多边形顶点的行坐标。
c:(N,) ndarray。
多边形顶点的列坐标。
shape:元组,可选。
图像形状,用于确定输出像素坐标的最大范围。对于超出图像大小的多边形,这很有用。 如果为 None,则使用多边形的完整范围。必须至少为长度 2。只使用前两个值来确定输入图像的范围。
返回
rr、cc:ndarray 整数。
多边形的像素坐标。
可用于直接索引数组,例如 img[rr, cc] = 1
。
示例
from skimage.draw import polygon
import numpy as np
img = np.zeros((10, 10), dtype=np.uint8)
r = np.array([1, 2, 8])
c = np.array([1, 7, 4])
rr, cc = polygon(r, c)
img[rr, cc] = 1
print(img)
print(rr)
print(cc)
运行结果:
[[0 0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0 0]
[0 0 1 1 1 1 1 1 0 0]
[0 0 1 1 1 1 1 0 0 0]
[0 0 0 1 1 1 1 0 0 0]
[0 0 0 1 1 1 0 0 0 0]
[0 0 0 0 1 1 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
[1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5 5 5 6 6 7 8]
[1 2 3 4 5 6 7 2 3 4 5 6 3 4 5 6 3 4 5 4 5 4 4]