gpt4 book ai didi

python - 如何溶解 numpy 数组中的模式?

转载 作者:IT老高 更新时间:2023-10-28 20:59:50 26 4
gpt4 key购买 nike

请原谅这个奇怪的标题,我真的想不出合适的措辞。

假设我有一个类似的数组:

arr = [[0 1 1 1 1 1 1 1 0],
[0 0 1 1 1 1 1 0 0],
[0 0 0 1 1 1 0 0 0],
[0 0 0 0 1 0 0 0 0],
[0 0 0 0 0 0 0 0 0]]

我希望“ eclipse 刻”掉接触 01,这将导致:

arr = [[0 0 1 1 1 1 1 0 0],
[0 0 0 1 1 1 0 0 0],
[0 0 0 0 1 0 0 0 0],
[0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0]] .

我已经尝试了一些类似 np.roll 的方法,但它似乎效率低下(并且有边缘效应)。有没有一个很好的简短方法来做到这一点?

最佳答案

Morpholocial erosion可以在这里使用。

形态侵 eclipse 将 (i, j) 处的像素设置为以 (i, j) 为中心的邻域中所有像素的最小值。 source

data
Out[39]:
array([[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])

structure
Out[40]:
array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]])

eroded = binary_erosion(data, structure, border_value=1).astype(int)

eroded
Out[42]:
array([[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])

关于python - 如何溶解 numpy 数组中的模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27989374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com