numpy:用零填充数组

numpy:用零填充数组,numpy,image-processing,Numpy,Image Processing,我正在处理图像,我想用零填充numpy数组。 我看着 对于填充单个数组,它可以正常工作 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]]) 如果列表/数组中有x t

我正在处理图像,我想用零填充numpy数组。 我看着

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

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]])
如果列表/数组中有
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
使用RGB通道在28x28像素上存储10幅图像的列表


现在我想填充所有10个图像,因此在修改10个图像后,它们是30x30的,边界上的像素为
[0,0,0]
这里有一种方法,使用
零初始化输出数组,然后将值赋给它-

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(c_x,((1,1),(0,0),(0,0)),mode='constant')
np.pad(c_x,((0,0),(1,1),(1,1)),mode='constant')


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


我不清楚期望的输出是什么。您的意思是要向存储在形状为(3,m,n)的数组中的RGB图像添加零边框吗?我想你是在说,你想要的不是你想要的。你能告诉我想要的结果是什么吗?您回答了我的问题,我试图提供一些关于RGB部件的信息。您的实现很好,但是是否可以使用
np.pad
本身而无需索引和滑动,似乎OP喜欢
np.pad
,并愿意与之配套:)@Warren Weckesser:它非常有用。如果我考虑图像RGB。我想在图像中添加零填充,使原始图像位于新图像的中心(即256x256x3-->512x512x3)。那么我的代码正确吗
delta_w=width\u new-image.shape[0]delta_h=height\u 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,((上,下,(左,右),(0,0)),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]]])
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)