如果图像具有形状(28,28,3,1),如何将其转换为形状(28,28,3)?

问题描述 投票:0回答:1

如果图像具有形状(28, 28, 3, 1),如何将其转换为形状(28, 28, 3)

我想我的形状的最后一个1是批量大小。

image computer-vision cv2
1个回答
0
投票

如注释np.squeeze中所建议,是最原则的方法。添加一些细节。

import numpy as np

image = np.ones(shape=(28, 28, 3, 1))
print(image.shape)  # (28, 28, 3, 1)

image = np.squeeze(image, axis=-1)
print(image.shape)  # (28, 28, 3)

我也强烈建议您始终明确指定要使用axis参数挤压的轴,以避免错误地删除其他单轴。实际上,默认情况下,np.squeeze会删除所有一维条目。如果您加载例如灰度图像。

gray = np.ones(shape=(28, 28, 1, 1))
print(gray.shape)  # (28, 28, 1, 1)

gray = np.squeeze(gray)
print(gray.shape)  # (28, 28) may not be what you want

您还可以根据自己的喜好将索引用于相同的目的。

image = np.ones(shape=(28, 28, 3, 1))
image = image[..., 0]  # same as: image[:, :, :, 0]
print(image.shape)  # (28, 28, 3)
© www.soinside.com 2019 - 2024. All rights reserved.