1
import os
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
from PIL import Image

train_path_positive = "/content/Dataset_P5/Train/Positive"

positive_patches = []
for filename in os.listdir(train_path_positive):
  image = cv2.imread(train_path_positive + "/" +filename,0)
  image = cv2.resize(image, (500,500))
  print(image.shape)
  positive_patches.append(image)

positive_patches_array = np.array(positive_patches) 

我有 15 张 jpg 格式的图片

当我尝试打印形状时,我得到(15,)并且我试图输入这些图片并将其存储在数组中,格式为(15, 500,500)

4

1 回答 1

1

您需要预先分配一个 numpy 数组

# Use your required dtype in below line
positive_patches_array = np.empty((15,500,500), dtype='uint16')


for num, filename in enumerate(os.listdir(train_path_positive)): 
    image = cv2.imread(train_path_positive + "/" +filename,0)
    image = cv2.resize(image, (500,500))   
    positive_patches_array[num, :, :] = image
于 2021-02-13T15:04:23.253 回答