NumPy,提取多个不连续的子数组

时间:2019-03-06 17:10:02

标签: arrays python-3.x numpy

因此,我尝试了多种组合或单独使用r_,s_,大步技巧,切片技巧,_ix和类似方法的方法,但我无法在这里向量化我想做的事情。

总而言之,给定一个2d数组,一个Nx2坐标数组和一个窗口形状(h,w),我怎么能一次不循环地从原始数组中提取一个Nxhxw子矩阵数组。在for循环中很容易做到,但是在实践中,我正在处理大图像中的数百个坐标和(50,50)的窗口形状,所以对速度进行矢量化处理。请注意,只有脾气暴躁,没有臭味。

例如:

arr:
array([[13, 11,  7,  6,  6,  6,  4,  5,  3, 12],
       [13,  1,  8,  7, 12,  3,  3, 15,  4,  8],
       [10, 12,  2,  1,  6,  3, 11,  5, 15,  4],
       [ 1,  2, 13, 12, 10,  7,  7, 11, 12,  2],
       [ 3,  1, 11,  8,  5, 11,  9,  6,  8,  6],
       [ 9,  7,  1,  9,  2,  3,  6,  5,  7, 12],
       [13,  7,  6,  1,  6,  3, 13,  3, 12,  8],
       [ 1,  9,  3, 15,  6,  4,  9, 15,  2,  6],
       [ 9,  2,  8, 12,  3, 11,  7, 12, 15, 14],
       [13, 15,  9, 14, 14, 14,  1, 11,  6, 15]])
coords:
array([[9,  3],
       [5,  2],
       [ 5,  6],
       [ 1,  4],
       [ 0,  6]])
window = (3,3)
submatrics_at_points(arr,coords,window):
array([[[8,12,3],
        [9,14,14],
        [nan,nan,nan]],
       [[1,11,8],
        [7,1,9],
        [7,6,1]],
       [[11,9,6],
        [3,6,5],
        [3,13,3]],
       [[6,6,6],
        [7,12,3],
        [1,6,3]],
       [[nan,nan,nan],
        [6,4,5],
        [3,3,15]],])

0 个答案:

没有答案