2016-12-03 46 views
1

我不知道什么是正确的术语在这里。请改正。 我有一个环形的网格(二维数组)。我的意思是第一行是最后一行之后的下一行。列相同。如何切片环状阵列

我想记住这个循环规则的大网格子集。 因此,拥有电网:

[[ 0 1 2 3 4 5 6 7 8 9] 
[10 11 12 13 14 15 16 17 18 19] 
[20 21 22 23 24 25 26 27 28 29] 
[30 31 32 33 34 35 36 37 38 39] 
[40 41 42 43 44 45 46 47 48 49] 
[50 51 52 53 54 55 56 57 58 59] 
[60 61 62 63 64 65 66 67 68 69] 
[70 71 72 73 74 75 76 77 78 79] 
[80 81 82 83 84 85 86 87 88 89] 
[90 91 92 93 94 95 96 97 98 99]] 

而且我想通过3中间(5,5)为中心的大小为3集,我会得到:

[[44 45 46] 
[54 55 56] 
[64 65 66]] 

但是,如果我想它是在(0,0)为中心,我会得到:

[[99 90 91] 
[ 9 0 1] 
[19 10 11]] 

在我目前的解决方案,我结合np.roll与切片。它正在工作,但我正在寻找更高性能的解决方案。

我目前的解决方案:

def get_centered_section(arr, center, side_size): 
    if side_size % 2 is 0: 
     raise "size shuold be odd number" 
    half_side_size = int((side_size - 1)/2) 
    w, h = arr.shape 
    x, y = center 

    ystart = y - half_side_size 
    if ystart < 0: 
     arr = np.roll(arr, abs(ystart), 0) 
     ystart = 0 
    elif ystart + side_size >= h: 
     overflow = ystart + side_size - h 
     ystart -= overflow 
     arr = np.roll(arr, -overflow, 0) 

    xstart = x - half_side_size 
    if xstart < 0: 
     arr = np.roll(arr, abs(xstart), 1) 
     xstart = 0 
    elif xstart + side_size >= w: 
     overflow = xstart + side_size - w 
     xstart -= overflow 
     arr = np.roll(arr, -overflow, 1) 

    return arr[ystart:ystart+side_size,xstart:xstart+side_size] 

test_a1 = np.reshape(np.arange(10*10), (10, 10)) 
get_centered_section(test_a1, (0, 0), 3) 

也许有缓存我的生活方式。我的具体用法将需要通过每个单元得到这个有点片。

+1

你叫什么*循环*是正确的术语*包裹*。搜索'numpy wrap around',你到达[这个问题](http://stackoverflow.com/questions/17739543/wrapping-around-slices-in-python-numpy),与'numpy.take'的答案似乎很好的解决方案。 [This](http://stackoverflow.com/questions/21396121/wrap-slice-around-edges-of-a-2d-array-in-numpy)和[this](http://stackoverflow.com/questions/4148292 /)也显得密切相关。我建议关闭这个问题作为其中一个重复... –

回答

2

一种方法将涉及与包裹填充周围使用np.pad然后slicing,像这样 -

def get_centered_section(a, center, side_size): 
    ext_size = (side_size[0]-1)/2, (side_size[1]-1)//2 
    a_pad = np.lib.pad(a, ([ext_size[0]],[ext_size[1]]), 'wrap') 
    return a_pad[center[0]:center[0]+side_size[0], \ 
       center[1]:center[1]+side_size[1]] 

很少的样品试验 -

In [94]: a 
Out[94]: 
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
     [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], 
     [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], 
     [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], 
     [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], 
     [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], 
     [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], 
     [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], 
     [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], 
     [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]) 

In [95]: get_centered_section(a, center = (0,0), side_size = (3,3)) 
Out[95]: 
array([[99, 90, 91], 
     [ 9, 0, 1], 
     [19, 10, 11]]) 

In [97]: get_centered_section(a, center = (5,5), side_size = (5,5)) 
Out[97]: 
array([[33, 34, 35, 36, 37], 
     [43, 44, 45, 46, 47], 
     [53, 54, 55, 56, 57], 
     [63, 64, 65, 66, 67], 
     [73, 74, 75, 76, 77]]) 

In [98]: get_centered_section(a, center = (7,2), side_size = (3,5)) 
Out[98]: 
array([[60, 61, 62, 63, 64], 
     [70, 71, 72, 73, 74], 
     [80, 81, 82, 83, 84]]) 
+0

这很有趣。我想到类似的解决方案,然后刷新和看到它=)我认为,如果我填充一次最大我需要,然后正常切片for循环使用修改,填充网格。最大的区别是我不知道如何做这个填充。 –

+0

这个解决方案比我的要慢很多倍。有10x10的网格和3x3内核我需要〜0.05ms,这一个〜0.3ms –

+0

但是也许当我迭代它将花费更少的时间。让我检查一下。 –