如何获得多维数组的填充片?

时间:2021-10-02 15:39:17

I am stuck on a little issue in the project I am currently working on.

在我目前正在进行的项目中,我被一个小问题困住了。

Getting straight to the point, let's assume I have a 2-dimensional numpy.array - I will call it arr.

直接说到这一点,假设我有一个二维的numpy。数组-我叫它arr。

I need to slice arr, but this slice must contain some padding depending on the selected interval.

我需要切片arr,但是这个切片必须包含一些填充,这取决于所选择的间隔。

Example:

例子:

arr = numpy.array([
    [  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]
])

Actually, numpy's response for arr[3:7, 3:7] is:

实际上,numpy对arr[3:7, 3:7]的反应是:

array([[19, 20],
       [24, 25]])

But I need it to be padded as if arr were bigger than it really is.

但我需要填充它就好像arr比它实际的要大。

Here is what I need as response for arr[3:7, 3:7]:

以下是我对arr的回应[3:7,3:7]:

array([[19, 20,  0,  0],
       [24, 25,  0,  0],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0]])

This padding should also occur in case of negative indices. If the requested slice is bigger than the whole image, padding must occur in all sides, if needed.

这种填充也应该在索引为负时出现。如果请求的切片大于整个图像,则必须在所有边中进行填充(如果需要的话)。

Another example, negative indices. This is the expected result for arr[-2:2, -1:3]:

另一个例子,消极的指标。这是arr[-2:2, -1:3]的预期结果:

array([[ 0,  0,  0,  0],
       [ 0,  0,  1,  2],
       [ 0,  0,  6,  7],
       [ 0,  0, 11, 12]])

Is there any native numpy function for this? If not, any idea of how can I implement this?

有什么本地的numpy函数吗?如果没有,我怎么实现它呢?

3 个解决方案

#1


3  

About the first part of your question you can use a simple indexing, and you can create a zero_like of your array with numpy.zeros_like then assign the special part :

关于您的问题的第一部分,您可以使用简单的索引,您可以用numpy创建一个数组的zero_like。然后分配特殊部分:

>>> new=numpy.zeros_like(arr)
>>> part=arr[3:7, 3:7]
>>> i,j=part.shape
>>> new[:i,:j]=part
>>> new
array([[19, 20,  0,  0,  0],
       [24, 25,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0]])

But for the second case you can not use a negative indexing for for numpy arrays like this.Negative indices are interpreted as counting from the end of the array so if you are counting from -2 actually in a 5x5 array there are not any row between -2 and 2 so the result would be an empty array :

但是对于第二种情况,您不能对这样的numpy数组使用负索引。负索引被解释为从数组末尾开始计数所以如果你从-2开始计数实际上在一个5x5的数组中在-2和2之间没有任何行所以结果是一个空数组:

>>> arr[-2:2]
array([], shape=(0, 5), dtype=int64)

#2


3  

You can do something like:

你可以这样做:

print np.lib.pad(arr[3:7,3:7], ((0, 2), (0, 2)), 'constant', constant_values=(0,0 ))



[[19 20  0  0]
 [24 25  0  0]
 [ 0  0  0  0]
 [ 0  0  0  0]]

For the negative indexing:

负索引:

print np.lib.pad(arr[ max(0,-1):3 , 0:2 ], ((1, 0), (2, 0)), 'constant', constant_values=(0,0 ))

[[ 0  0  0  0]
 [ 0  0  1  2]
 [ 0  0  6  7]
 [ 0  0 11 12]]

Check here for reference

检查在这里供参考

#3


1  

import numpy as np

def convert(inarr, x1, x2, y1, y2):
  xd = x2 - x1
  yd = y2 - y1
  outarr = np.zeros(xd * yd).reshape(xd, yd)
  x1fr = max(0, x1)
  x2fr = min(x2, inarr.shape[0])
  y1fr = max(0, y1)
  y2fr = min(y2, inarr.shape[1])
  x1to = max(0, xd - x2) 
  x2to = x1to + x2fr - x1fr
  y1to = max(0, yd - y2)
  y2to = y1to + y2fr - y1fr
  outarr[x1to:x2to, y1to:y2to] = inarr[x1fr:x2fr, y1fr:y2fr]
  return outarr


arr = np.array([[ 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]])

print(convert(arr, -2, 2, -1, 3))

Well this works but returns

好吧,这行得通,但会有回报

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  1.  2.  3.]
 [ 0.  6.  7.  8.]]

for your -ve index example. You can play around to get it to do what you expect

对于您的-ve索引示例。你可以利用它做你想做的事

#1


3  

About the first part of your question you can use a simple indexing, and you can create a zero_like of your array with numpy.zeros_like then assign the special part :

关于您的问题的第一部分,您可以使用简单的索引,您可以用numpy创建一个数组的zero_like。然后分配特殊部分:

>>> new=numpy.zeros_like(arr)
>>> part=arr[3:7, 3:7]
>>> i,j=part.shape
>>> new[:i,:j]=part
>>> new
array([[19, 20,  0,  0,  0],
       [24, 25,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0]])

But for the second case you can not use a negative indexing for for numpy arrays like this.Negative indices are interpreted as counting from the end of the array so if you are counting from -2 actually in a 5x5 array there are not any row between -2 and 2 so the result would be an empty array :

但是对于第二种情况,您不能对这样的numpy数组使用负索引。负索引被解释为从数组末尾开始计数所以如果你从-2开始计数实际上在一个5x5的数组中在-2和2之间没有任何行所以结果是一个空数组:

>>> arr[-2:2]
array([], shape=(0, 5), dtype=int64)

#2


3  

You can do something like:

你可以这样做:

print np.lib.pad(arr[3:7,3:7], ((0, 2), (0, 2)), 'constant', constant_values=(0,0 ))



[[19 20  0  0]
 [24 25  0  0]
 [ 0  0  0  0]
 [ 0  0  0  0]]

For the negative indexing:

负索引:

print np.lib.pad(arr[ max(0,-1):3 , 0:2 ], ((1, 0), (2, 0)), 'constant', constant_values=(0,0 ))

[[ 0  0  0  0]
 [ 0  0  1  2]
 [ 0  0  6  7]
 [ 0  0 11 12]]

Check here for reference

检查在这里供参考

#3


1  

import numpy as np

def convert(inarr, x1, x2, y1, y2):
  xd = x2 - x1
  yd = y2 - y1
  outarr = np.zeros(xd * yd).reshape(xd, yd)
  x1fr = max(0, x1)
  x2fr = min(x2, inarr.shape[0])
  y1fr = max(0, y1)
  y2fr = min(y2, inarr.shape[1])
  x1to = max(0, xd - x2) 
  x2to = x1to + x2fr - x1fr
  y1to = max(0, yd - y2)
  y2to = y1to + y2fr - y1fr
  outarr[x1to:x2to, y1to:y2to] = inarr[x1fr:x2fr, y1fr:y2fr]
  return outarr


arr = np.array([[ 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]])

print(convert(arr, -2, 2, -1, 3))

Well this works but returns

好吧,这行得通,但会有回报

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  1.  2.  3.]
 [ 0.  6.  7.  8.]]

for your -ve index example. You can play around to get it to do what you expect

对于您的-ve索引示例。你可以利用它做你想做的事