image-processing - 保存CNN模型的输出图像
问题描述
我为边缘检测任务制作了一个 CNN 模型。它工作得很好,我可以通过特征图看到很好的结果。问题是我在每个卷积层中使用 4 个过滤器,因此输出将是具有 4 个通道的图像,但我需要将图像保存为 3 个通道以便稍后使用,这里是代码示例我用了:
#Feed image to model:
output = model(img) #input size (1, 3, 224, 224)
#convert output tensor to numpy
fimg = output.detach().cpu().numpy()
#get rid of the single batch dimension
f2img = fimg.squeeze(0)
#swap axes to (13, 13, 4)
f2img = fimg.transpose(1, 2, 0)
它会被保存为 (13,13,4) 但我需要它作为 (13, 13, 3)。如果有人可以提供帮助,将不胜感激。
解决方案
我建议采取以下步骤:
将图像数组 RGBA 转换为 PIL 图像对象
从 PIL 导入图像
img_rgba = Image.fromarray(img_rgba)
RGBA 图像对象转 RGB
img_rgb = img_rgba.convert('RGB')
返回 np.ndarray
img_rgb = np.array(img_rgb)
注意:如果您的图像是 RBGA 格式而不是 RGBA,您将需要一个额外的步骤将其从 RBG 转换为 RGB。
img_rgb = cv2.cvtColor(bgr_image_array, cv2.COLOR_BGR2RGB)
推荐阅读
- docker - Docker 撰写关键字
- input - 让用户从图库中选择图像或从手机上的相机拍照
- python - Bresenham 算法“类型错误:‘系列’对象不能解释为整数”
- java - 根据条件成员从休眠中获取元素集合
- python - Pandas 列中的嵌套 Json
- android - 如何将自定义参考代码或促销代码从链接传递到播放或应用商店,并在安装后将其传递给 react-native 应用
- html - 如何隐藏 HTML 页面底部的水平滚动条
- chef-infra - 将厨师前端从 12.17.33 升级到 14.5.29 时出错
- apache-spark - 如何在pyspark数据框中用0替换负值
- angularjs - 如何在 AngularJS 中模拟选项卡自动单击?