用numpy展平多个rgb图像

时间:2019-10-11 05:42:28

标签: python numpy opencv computer-vision

我正在尝试将多个图像展平为(n,1)的numpy数组。如果我只加载1张图片,效果很好。但是,如果我增加图像计数的数量,将无法正常工作。

图像加载

for dataset, path in data_dir.items():
    for i, cat in enumerate(categories):
        cur_dir = os.path.join(path, cat)
        print (" current directory path is :", cur_dir, "\n")
        filenames = glob(os.path.join(cur_dir, '*.png'))
        for file in filenames:
            img = cv.imread(file)
            print (img.shape)
            if dataset == 'Training':
                x_train.append(img)
                y_train.append(i)

图像平整

print ("Number of training examples: m_train = " + str(len(y_train)) + " " + str(len(x_train)))
x_train = np.asarray(x_train)
y_train = np.asarray(y_train)

print (x_train.shape[0])

## Logic to Flatten ##
train_set_x_flatten = x_train.reshape(x_train.shape[0],-1).T
train_set_y_flatten = y_train.reshape(y_train.shape[0],-1).T

print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y_flatten.shape))

输出1张图片

(297, 319, 3) -- dimension of image
Number of training examples: m_train = 1 1
train_set_x_flatten shape: (284229, 1)
train_set_y shape: (1, 1)

输出4张图像

(297, 319, 3) -- dimension of image
(297, 319, 3) -- dimension of image
(499, 390, 3)
(499, 390, 3)
Number of training examples: m_train = 4 4
train_set_x_flatten shape: (1, 4)
train_set_y shape: (1, 4)

0 个答案:

没有答案