python - 在 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][:][:]
解决方案
你应该做
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 也是可能的)
推荐阅读
- php - 编辑 sudoers 文件以仅允许特定命令
- python - 如何在熊猫中有条件地修改列中的单元格?
- html - 从单独的容器 div 内联显示文本
- http-headers - icecast 返回 text/html 而不是 audio/mpeg
- css - css动画如何改变z-index?
- javascript - Javascript - 重用变量来创建 DOM 元素
- vb.net - 使用 ContextMenuStrip 从 TreeView 中删除节点(禁用选择时)
- java - Java Applet - 阻止其他线程更改图形颜色
- aws-lambda - AWS SAM 模板/cloudformation 没有为方法定义集成(服务:AmazonApiGateway
- python-3.x - Python 2 到 Python 3 结构包问题