如何将来自 CNN 预测的多张图像合并为一张图像? [英] How to merge multiple images from CNN prediction into one image?

查看:55
本文介绍了如何将来自 CNN 预测的多张图像合并为一张图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Keras 中使用 CNN 来构建模型,现在我想使用所有图像来进行最终预测.这是我的输入和标签的尺寸:

I use CNN in Keras to build a model and now I want to use all the images to make the final prediction. This is the dimensions of my inputs and labels:

X_pred, y_pred=next(pred_generator)
X_pred.shape, y_pred.shape

结果:

((132, 64, 64, 3), (132, 64, 64, 1))

这是预测代码:

pred_64= model.predict(X_pred)
pred_64.shape

结果:

(132, 64, 64, 1)

很明显,有 132 张大小为 64X64 的预测图像.我想合并每 4 张图像以创建大小为 128X128 的单个图像.最后,通过合并从 model.predict 生成的每 4 张图像,我应该有 33 张图像.

As it is obvious, there 132 predicted images with the size of 64X64. I want to merge every 4 images to create individual image with the size of 128X128. Finally, I should have 33 images by merging every 4 images produced from model.predict.

这是一个关于图片保存在 128X128 维度时的顺序的例子.

This is an example about the order of images when they are saved in 128X128 dimension.

添加swapaxes代码后,图像将如下旋转:

After adding the swapaxes code the images will be rotated as below:

pred_128 = pred_64.reshape(66, 128, 64, 1).swapaxes(1, 2).reshape(33, 128, 128, 1)

我添加了 np.transpose 如下.它有助于纠正旋转,但图像 2 替换了图像 3,反之亦然.你能帮我解决这个问题吗?

I added np.transpose as below. It helps to correct the rotation but image 2 replaced image 3 and vice versa. Could you please help me to solve that?

pred_128 = pred_64.reshape(66, 128, 64, 1).swapaxes(1, 2).reshape(33, 128, 128, 1)
pred_128= np.transpose(pred_128, [0, 2, 1, 3])

推荐答案

我仍然希望我们可以使用 reshapes 来解决这个问题,但同时这里有一个更暴力的解决方案:

I still hope we can fix the problem using reshapes, but in the meantime here is a more brute force solution:


# Initializing counters
i = 0  # Old image number
j = 0  # New image number

# Pre-allocate new images array
pred_128 = np.zeros((33, 128, 128, 1))

# Loop over new images
while j < 33:
    pred_128 [j, :64, :64, 0] = pred_64[0+i, :, :, 0]  # Upper left
    pred_128 [j, :64, 64:, 0] = pred_64[1+i, :, :, 0]  # Upper right
    pred_128 [j, 64:, :64, 0] = pred_64[2+i, :, :, 0]  # Lower left
    pred_128 [j, 64:, 64:, 0] = pred_64[3+i, :, :, 0]  # Lower right

    # Add to counters
    i += 4
    j += 1

这篇关于如何将来自 CNN 预测的多张图像合并为一张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆