Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用周围像素的内核将numpy数组重塑为新的ndarraw_Python_Arrays_Numpy - Fatal编程技术网

Python 如何使用周围像素的内核将numpy数组重塑为新的ndarraw

Python 如何使用周围像素的内核将numpy数组重塑为新的ndarraw,python,arrays,numpy,Python,Arrays,Numpy,我有一个numpy数组,它的大小可能会有所不同(我马上就会讲到),我想对它进行重塑,使数组中的每个点都是原始数组的核心 假设我从数组开始: np.arange(81).reshape(9,9) >>> 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],

我有一个numpy数组,它的大小可能会有所不同(我马上就会讲到),我想对它进行重塑,使数组中的每个点都是原始数组的核心

假设我从数组开始:

np.arange(81).reshape(9,9)

>>> 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]])
但现在我想重塑这个数组,使数组中的每个“点”不是一个整数值,而是一个小的NxM数组。但是,我遇到的问题是较小的阵列重叠。所以我希望它看起来像:

array(
[[[[ 0  1  2]   [[ 1  2  3]   [[ 2  3  4]
   [ 9 10 11]    [10 11 12]    [11 12 13]
   [18 19 20]]   [19 20 21]]   [20 21 22]]]
因此,正如您所看到的,这些数组中存在重叠。我不知道如何使用numpy重塑方法来实现这一点。我最接近的方法是使用:

arr = np.arange(81).reshape(9,9)
kernalized = np.swapaxes(arr.reshape(3,3,3,-1),1,2)

>>> 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]]])
这就达到了正确的形状!所以我觉得我走对了路。但没有重叠。有人有什么想法我可以试试吗? 正如上面添加的cherry一样,数组可以有不同的大小,因此理想情况下,我提出的任何代码都可以处理这个问题,但这并不是最大的问题,因为数组非常大,只需删掉几行或几列,使其可以被3整除就可以了


谢谢

你是在大踏步地寻找

from numpy.lib.stride_tricks import as_strided

strides = a.strides
window = 3 

out = as_strided(a, ((a.shape[0]-window+1),(a.shape[1]-window+1),window,window), 
               (strides[1], strides[1]) + a.strides 
              ).reshape(-1, window, window)

print(out[:3])
输出:

[[[ 0  1  2]
  [ 9 10 11]
  [18 19 20]]

 [[ 1  2  3]
  [10 11 12]
  [19 20 21]]

 [[ 2  3  4]
  [11 12 13]
  [20 21 22]]]

您是否正在寻找跨越式的

from numpy.lib.stride_tricks import as_strided

strides = a.strides
window = 3 

out = as_strided(a, ((a.shape[0]-window+1),(a.shape[1]-window+1),window,window), 
               (strides[1], strides[1]) + a.strides 
              ).reshape(-1, window, window)

print(out[:3])
输出:

[[[ 0  1  2]
  [ 9 10 11]
  [18 19 20]]

 [[ 1  2  3]
  [10 11 12]
  [19 20 21]]

 [[ 2  3  4]
  [11 12 13]
  [20 21 22]]]

谢谢你,是的,这确实是我要找的。要指出的是,out变量的第二行应该是strips[0],如果希望它在整个数组中移动,那么strips[1]。否则步子会重复。谢谢你,是的,这确实是我要找的。要指出的是,out变量的第二行应该是strips[0],如果希望它在整个数组中移动,那么strips[1]。否则,步伐会重复。