Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: padding array with zeros

I am working with images and I want to pad numpy array with zeros. I looked at np.pad

For padding single array it works fine

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]])

How to implement if we have x type arrays in a list/array , like

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

As such arrays contains R,G,B channel, Can that too be accounted when padding?

edit:

Say c_x stores list of 10 images on 28x28 pixels with RGB channel

Now I want to pad all 10 images , So after modifying 10 images are of 30x30 with pixels on border as [0,0,0]

like image 544
rainversion_3 Avatar asked Nov 28 '25 11:11

rainversion_3


2 Answers

It is not clear to me what your desired output is, but I think it is either np.pad(c_x, ((1,1), (0,0), (0,0)), mode='constant') or 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]]])
like image 117
Warren Weckesser Avatar answered Dec 01 '25 14:12

Warren Weckesser


Here's one approach using initialization of output array with zeros and then assigning values into it -

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

Now, considering that arrays could be image data and usually you might have the channel being represented by the last axis, while the first two axes representing the height and width, we need to change the indexing there. The modified portions would be the initialization and assignment :

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

So, if you notice, we are slicing with padlen:-padlen along the axes that needs padding. Using this general theory, you can handle various image data arrays for padding.

Sample run -

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]]])
like image 34
Divakar Avatar answered Dec 01 '25 14:12

Divakar