首页 > 解决方案 > 在 numpy 数组中裁剪图像

问题描述

我想裁剪 RGB​​ 图像,以便删除图像的上半部分。裁剪后,我想将图像连接到一个 numpy 数组(这里是图像)。但我收到以下错误ValueError: all the input array dimensions except for the concatenation axis must match exactly。我尝试了多种方法,但我的任何尝试似乎都没有运气。

我的代码看起来像

images = np.zeros((1, 32, 64, 3))
image = get_image()  # has shape 1, 64, 64, 3
# removing the first coordinate didn't change the error.
images = np.concatenate([images, image[:, 32:63, :, :]], axis=0)  

编辑:以下修改image[:, 32:63, :, :]没有解决问题

a) [:, 32:63, :, :] -> [32:63, :, :]

b) [:, 32:63, :, :] -> [:][32:63][:][:]

标签: pythonimagenumpy

解决方案


你应该做

images = np.zeros((1, 32, 64, 3))
image = get_image()  # has shape 1, 64, 64, 3
# removing the first coordinate didn't change the error.
images = np.concatenate([images, image[:, 32:, :, :]], axis=0)  

由于 32:63 省略了最后一个元素。(32:64 也是可能的)


推荐阅读