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

问题描述 投票:-1回答:2

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

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

我怎样才能以上述格式重塑它?

编写的代码是:

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]) 
python numpy opencv image-processing cv2
2个回答
1
投票

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

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


-1
投票

不要调整整个数组的大小,单独调整数组中的每个图像的大小。

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

这将创建一个包含230个项目的一维数组。如果你在它上面调用reshape,numpy将尝试重新整形这个数组,而不是单个图像!

© www.soinside.com 2019 - 2024. All rights reserved.