【问题标题】:numpy: padding array with zerosnumpy:用零填充数组
【发布时间】:2017-10-24 01:32:23
【问题描述】:

我正在处理图像,我想用零填充 numpy 数组。 我看了np.pad

对于填充单个数组,它工作正常

x = np.array([[1,2],[3,4]])
y = np.pad(x,(1,1), 'constant')

x
=> array([[1, 2],
       [3, 4]])
y
=> array([[0, 0, 0, 0],
       [0, 1, 2, 0],
       [0, 3, 4, 0],
       [0, 0, 0, 0]])

如果我们在 list/array 中有 x type 数组,如何实现,例如

c_x=np.array([[[2,2],[2,3]],[[3,2],[2,3]],[[4,4],[2,3]]])
c_y=np.pad(c_x,((0,0),(1,1),(0,0)),'constant')  #padding is present only on top and bottom

由于这样的数组包含R,G,B通道,填充时也可以考虑吗?

编辑:

c_x 存储 10 个图像的列表,在 28x28 像素上使用 RGB 通道

现在我想填充所有 10 张图片,所以修改后 10 张图片的大小为 30x30,边框上的像素为 [0,0,0]

【问题讨论】:

  • 我不清楚所需的输出是什么。您的意思是要向存储在形状为 (3, m, n) 的数组中的 RGB 图像添加零边框吗?我想你是说c_y 不是你想要的。你能展示一下想要的结果是什么吗?小例子只需手动输入即可。
  • @WarrenWeckesser 你回答了我的问题,我试图提供一些关于 RGB 部分的信息

标签: numpy image-processing


【解决方案1】:

我不清楚你想要的输出是什么,但我认为它要么是np.pad(c_x, ((1,1), (0,0), (0,0)), mode='constant') 要么是np.pad(c_x, ((0,0), (1,1), (1,1)), mode='constant')

In [228]: c_x
Out[228]: 
array([[[2, 2],
        [2, 3]],

       [[3, 2],
        [2, 3]],

       [[4, 4],
        [2, 3]]])

In [229]: np.pad(c_x, ((1,1), (0,0), (0,0)), mode='constant')
Out[229]: 
array([[[0, 0],
        [0, 0]],

       [[2, 2],
        [2, 3]],

       [[3, 2],
        [2, 3]],

       [[4, 4],
        [2, 3]],

       [[0, 0],
        [0, 0]]])

In [230]: np.pad(c_x, ((0,0), (1,1), (1,1)), mode='constant')
Out[230]: 
array([[[0, 0, 0, 0],
        [0, 2, 2, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 3, 2, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 4, 4, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]]])

【讨论】:

  • 是的,看来 OP 喜欢 np.pad 并且会选择它 :)
  • @Warren Weckesser:非常有用。如果我考虑图像RGB。我想添加零填充图像,使我的原始图像位于新图像的中心(即 256x256x3-->512x512x3)。那么我的代码正确吗? delta_w = width_new - image.shape[0] delta_h = height_new - image.shape[1] top, bottom = delta_h // 2, delta_h - (delta_h // 2) left, right = delta_w // 2, delta_w - (delta_w // 2) image= np.pad(image, ((top, bottom), (left, right), (0,0)), mode='constant')
【解决方案2】:

这是一种使用 zeros 初始化输出数组然后为其赋值的方法 -

def pad3D(c_x, padlen=1):
    m,n,r = c_x.shape
    c_y = np.zeros((m, n+2*padlen, r+2*padlen),dtype=c_x.dtype)
    c_y[:, padlen:-padlen, padlen:-padlen] = c_x
    return c_y

现在,考虑到数组可能是图像数据,并且通常您可能将通道由最后一个轴表示,而前两个轴表示高度和宽度,我们需要更改那里的索引。修改部分将是初始化和赋值:

c_y = np.zeros((m+2*padlen, n+2*padlen, r),dtype=c_x.dtype)
c_y[padlen:-padlen, padlen:-padlen, :] = c_x

因此,如果您注意到,我们正在使用 padlen:-padlen 沿着需要填充的轴进行切片。使用这个一般理论,您可以处理各种图像数据数组以进行填充。

示例运行 -

In [422]: c_x
Out[422]: 
array([[[2, 2],
        [2, 3]],

       [[3, 2],
        [2, 3]],

       [[4, 4],
        [2, 3]]])

In [423]: pad3D(c_x, padlen=1) # pads all across not just top and bottom
Out[423]: 
array([[[0, 0, 0, 0],
        [0, 2, 2, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 3, 2, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 4, 4, 0],
        [0, 2, 3, 0],
        [0, 0, 0, 0]]])

【讨论】:

  • 您的实现很好,但是否可以在没有索引和切片的情况下使用 np.pad 本身
【解决方案3】:

试试这个。使用np.pad 我知道我迟到了,但它可能对某人有所帮助。 :)

def padwithzero(vector, pad_width, iaxis, kwargs):
    vector[:pad_width[0]] = 0
    vector[-pad_width[1]:] = 0
    return vector


tmp_mainImg = np.pad(mainImg, 1, padwithzero)

print(tmp_mainImg)

【讨论】:

    【解决方案4】:

    我更新了@Divakar 对二维矩阵的回答:

    import numpy
    def pad2D(c_x, padlen=1):
        m, n = c_x.shape
        c_y = np.zeros((m + 2 * padlen, n + 2 * padlen), dtype = c_x.dtype)
        c_y[padlen:-padlen, padlen:-padlen] = c_x
        return c_y
    

    这也适用于Numba!

    import numpy as np
    from numba import jit
    @jit(nopython = True)
    def pad2D(c_x, padlen=1):
        m, n = c_x.shape
        c_y = np.zeros((m + 2 * padlen, n + 2 * padlen), dtype = c_x.dtype)
        c_y[padlen:-padlen, padlen:-padlen] = c_x
        return c_y
    

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 2018-10-05
      • 2016-06-15
      • 2020-06-06
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多