rai*_*n_3 3 numpy image-processing
我正在使用图像,我想用零填充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]])
Run Code Online (Sandbox Code Playgroud)
如果我们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
Run Code Online (Sandbox Code Playgroud)
因为这样的数组包含R,G,B通道,填充时也可以考虑吗?
编辑:
比如c_x使用RGB通道存储28x28像素的10个图像的商店列表
现在我想填充所有10个图像,因此在修改10个图像后,30x30的边框像素为 [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'):
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]]])
Run Code Online (Sandbox Code Playgroud)