我有一个二维数组,例如(101 行和 100 列(。现在我想创建一个搜索窗口或块(3 行 x 3 列(,它将在数组周围移动并确定最大值,选择它并使用 python 和 numpy 将所有其他值保留为零。例如
x = ([[1,2,3,4,5,6,7,8,9,10],
[2,5,4,5,3,4,6,7,5,3],
[3,3,4,5,6,7,3,4,5,8]]
例如,使用 2x2 搜索窗口 x.some函数从左上角开始将给出结果
Result = ([0,0...
[0,5... for the 1st iteration so the the whole result should look like this
Result = ([[0,0,0,0,0,6,0,8,0,10],
[0,5,0,5,0,0,0,0,0,0],
[3,3,0,5,0,7,0,4,0,8]]
请注意,搜索窗口的最后一行必须从 2x2 数组更改为 2x1,因为搜索窗口不重叠
您的帮助将不胜感激。 提前谢谢你
以下是使用skimage.util.view_as_blocks
的方法:
>>> import numpy as np
>>> import skimage.util as su
>>>
>>> def split_axis(N, n):
... q, r = divmod(N, n)
... left = ((np.s_[:q*n], n),) if q else ()
... right = ((np.s_[q*n:], r),) if r else ()
... return (*left, *right)
...
>>> def block_max(x, block, inplace=False):
... if not inplace:
... x = x.copy()
... xi, xj = x.shape
... bi, bj = block
... for ci, ri in split_axis(xi, bi):
... for cj, rj in split_axis(xj, bj):
... vab = su.view_as_blocks(x[ci, cj], (ri, rj))
... vab[vab < vab.max(axis=(-1, -2), keepdims=True)] = 0
... return x
...
>>> x = ([[1,2,3,4,5,6,7,8,9,10],
... [2,5,4,5,3,4,6,7,5,3],
... [3,3,4,5,6,7,3,4,5,8]])
>>>
>>> x = np.array(x)
>>>
>>> block_max(x, (2, 2))
array([[ 0, 0, 0, 0, 0, 6, 0, 8, 0, 10],
[ 0, 5, 0, 5, 0, 0, 0, 0, 0, 0],
[ 3, 3, 0, 5, 0, 7, 0, 4, 0, 8]])
如果您没有skimage
:
>>> def view_as_blocks(x, blockshape):
... *xs, xi, xj = x.shape
... bi, bj = blockshape
... return np.ascontiguousarray(x).reshape(*xs, xi//bi, xj//bj, *blockshape)
您的更新问题(未经测试(:
>>> def block_max(x, block):
... out = np.zeros_like(x)
... xi, xj = x.shape
... bi, bj = block
... for ci, ri in split_axis(xi, bi):
... for cj, rj in split_axis(xj, bj):
... vab = su.view_as_blocks(x[ci, cj], (ri, rj))
... oab = su.view_as_blocks(out[ci, cj], (ri, rj))
... vmx = vab.max(axis=(-1, -2), keepdims=True)
... vmn = vab.min(axis=(-1, -2), keepdims=True)
... cond = vmx - vmn > 2
... oab[cond & (vab == vmx)] == 1
... oab[cond & (vab == vmn)] == 2
... return out