Python 填充包含一组点的最小形状

Python 填充包含一组点的最小形状,python,arrays,numpy,Python,Arrays,Numpy,我将一个2D numpy数组初始化为0,并在该数组中拾取了4个点。我想在它们之间插入值以创建一个闭合形状,并用值1填充该形状。这是密码 a = np.zeros((256, 256)) depth = np.random.randint(50, 200) corner1 = np.random.randint(0, 100) corner2 = np.random.randint(150, 250) top_max = depth - np.random.randint(10, 25) bott

我将一个2D numpy数组初始化为0,并在该数组中拾取了4个点。我想在它们之间插入值以创建一个闭合形状,并用值1填充该形状。这是密码

a = np.zeros((256, 256))
depth = np.random.randint(50, 200)
corner1 = np.random.randint(0, 100)
corner2 = np.random.randint(150, 250)
top_max = depth - np.random.randint(10, 25)
bottom_max = depth + np.random.randint(10, 25)
top_max_loc = np.random.randint(corner1 + 10, corner2 - 10)
bottom_max_loc = np.random.randint(corner1 + 10, corner2 - 10)

left_point = (depth, corner1)
right_point = (depth, corner2)
top_point = (top_max, top_max_loc)
bottom_point = (bottom_max, bottom_max_loc)

a[left_point] = 1
a[right_point] = 1
a[top_point] = 1
a[bottom_point] = 1
print(left_point)
print(right_point)
print(top_point)
print(bottom_point)
这是点的图

我想要的是用一些随机形状填充数组,并得到如下结果(仅黄色形状)


看起来您正在寻找给定的一组点,即包围所有点的最小形状。你可以用它。由于只有一个形状,因此可以将搜索减少到点集的边界框。对于您可能感兴趣的多个对象

下面是使用您的代码生成的示例:

from skimage.morphology import convex_hull_image

x,y = np.where(a)
x_min, x_max = x.min(), x.max()
y_min, y_max = y.min(), y.max()

plt.figure(figsize=(10,10))
plt.imshow(a[x_min:x_max+1, y_min:y_max+1])

现在,获取切片图像的凸包,并使用以下参数重新分配给阵列:

a[x_min:x_max+1, y_min:y_max+1] = convex_hull_image(a[x_min:x_max+1, y_min:y_max+1])
导致:

plt.figure(figsize=(10,10))
plt.imshow(a)