gpt4 book ai didi

python - 值错误 : cannot reshape array of size 230 into shape (3, 600,800)

转载 作者:行者123 更新时间:2023-12-02 17:31:52 29 4
gpt4 key购买 nike

我正在读取 230 张图像作为来自 2 个不同文件夹的数组并调整它的大小,以使每个图像的纵横比保持不变(调整后的图像大小宽度 = 600 * 高度 = 800)。之后,我试图将标签和图像数组分成 2 个不同的列表。现在,在将图像数组列表提供给 CNN 模型之前,我将其 reshape 为 reshape([-1, 3, 600, 800]) 格式,但出现以下错误:

ValueError:无法将大小为 230 的数组 reshape 为形状 (3,600,800)

如何以上述格式 reshape 它?

编写的代码是:

def create_data():
for category in LABELS:
path = os.path.join(DATADIR,category)
class_num = LABELS.index(category) # get the classification (0 or a 1).
for img in tqdm(os.listdir(path)):
img_array = cv2.imread(os.path.join(path,img)) # convert to array
fac = np.array(img_array).shape[0]/np.array(img_array).shape[1]
new_array = cv2.resize(img_array, (600, int(np.ceil((fac*600)))))# resize to normalize data size
data.append([new_array, class_num]) # add to data


create_data()


Xtest = []
ytest = []


for features,label in data:
Xtest.append(features)
ytest.append(label)

X = np.array(Xtest).reshape([-1, 3, 600, 800])

最佳答案

之后 cv2.resize ,您的图像的高度均为 600,但宽度不同。这意味着它们都有不同数量的像素,可能太多或太少而无法形成您期望的输出形状。您也无法将这些图像连接成一个大数组。

您将需要裁剪/填充您的图像以使其具有相同的大小。

关于python - 值错误 : cannot reshape array of size 230 into shape (3, 600,800),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55182956/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com