如何在numpy中以3x3矩阵的形式获取特定数字的周围数字?

时间:2017-07-08 05:11:42

标签: python arrays numpy matrix

我写了一个函数,通过将索引作为参数以3X3矩阵的形式传递来获取数字的周围数字。

import numpy as np
ar = np.arange(0,120)
ar = np.reshape(ar,(12,10))

def get_surroundings(ar,i,j):
    if ((i>0) & (i<ar.shape[0])) & ((j>0) & (j<ar.shape[1])):
        temp = np.array([[ar[i-1][j-1],ar[i-1][j],ar[i-1][j+1]],[ar[i][j-1],ar[i][j],ar[i][j+1]],[ar[i+1][j-1],ar[i+1][j],ar[i+1][j-1]]])
        return temp

get_surroundings(ar,9,5)

ar是一个12x10矩阵,如下所示:

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],
   [100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
   [110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])

我从下面的函数中得到结果:

 array([[ 84,  85,  86],
       [ 94,  95,  96],
       [104, 105, 104]])

是否有一个numpy函数来实现相同的功能? 如果我给(ar,0,0)那么输出应该是

array([[ 0,  0,  0],
       [ 0,  10,  11],
       [0, 20, 21]])

同样如果(ar,11,0)则输出应为

array([[ 0, 100, 101],
       [ 0 , 110, 111],
       [ 0 , 0,0]])

1 个答案:

答案 0 :(得分:2)

使用np.pad -

填充零后,您可以使用scikit-image as view_as_windows创建此类滑动窗口
from skimage.util.shape import view_as_windows

def get_windows(ar, W, i, j):
    h0,h1 = (W[0]-1)//2, (W[1]-1)//2 
    ar_ext = np.pad(ar,((h0,h0),(h1,h1)),'constant',constant_values=(0,0))
    return view_as_windows(ar_ext, (W[0],W[1]))[i,j]

示例运行 -

1)输入数组:

In [39]: ar
Out[39]: 
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],
       [100, 101, 102, 103, 104, 105, 106, 107, 108, 109],
       [110, 111, 112, 113, 114, 115, 116, 117, 118, 119]])

2)在内部区域输出(3,3)的窗口:

In [40]: get_windows(ar, (3,3), 9,5)
(1, 1)
Out[40]: 
array([[ 84,  85,  86],
       [ 94,  95,  96],
       [104, 105, 106]])

3)在内部区域输出(5,5)的窗口:

In [41]: get_windows(ar, (5,5), 9,5)
(2, 2)
Out[41]: 
array([[ 73,  74,  75,  76,  77],
       [ 83,  84,  85,  86,  87],
       [ 93,  94,  95,  96,  97],
       [103, 104, 105, 106, 107],
       [113, 114, 115, 116, 117]])

4)在内部区域输出(5,3)的窗口:

In [30]: get_windows(ar, (5,3), 9,5)
Out[30]: 
array([[ 74,  75,  76],
       [ 84,  85,  86],
       [ 94,  95,  96],
       [104, 105, 106],
       [114, 115, 116]])

5)在边界位置输出(5,5)的窗口:

In [42]: get_windows(ar, (5,5), 0,0)
(2, 2)
Out[42]: 
array([[ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  1,  2],
       [ 0,  0, 10, 11, 12],
       [ 0,  0, 20, 21, 22]])