我将一个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)
这是点的图
我想要的是用一些随机形状填充数组,并得到如下结果(仅黄色形状)
看起来您正在寻找给定一组点的convex hull,即包围所有点的最小形状。为此,您可以使用^{} 。由于只有一个形状,因此可以将搜索减少到点集的边界框。对于多个对象,您可能对^{} 感兴趣
下面是使用您的代码生成的示例:
现在,获取切片图像的凸包,并使用以下参数重新分配给阵列:
导致:
相关问题 更多 >
编程相关推荐