将相同形状的动态图像数量读入 Python NumPy 数组
read dynamic number of images of same shape into Python NumPy array
我有这个代码:
image_list = []
for filename in glob.glob('C:\Users\Utilizador\Desktop\menu\*.jpg'):
im=cv2.imread(filename)
image_list.append(im)
这为我创建了一个图像列表,但我需要它是一个数组形式,形状为 (num_of_images, width, height, 3)
所有图像的形状都相同
有任何想法吗?
谢谢
由于所有图像的形状都相同,我们可以创建一个空数组,然后将每个图像读入其中。
In [103]: file_path = 'C:\Users\Utilizador\Desktop\menu\*.jpg'
In [104]: num_imgs = len(glob.glob(file_path))
In [105]: width, height, channels = 512, 512, 3
In [106]: batch_arr = np.empty((num_imgs, width, height, channels), dtype=np.uint8)
In [107]: for idx, filename in enumerate(glob.glob(file_path)):
img = cv2.imread(filename)
# if img is of different width and height than defined above
# do some resize and then insert in the array.
batch_arr[idx] = img
我有这个代码:
image_list = []
for filename in glob.glob('C:\Users\Utilizador\Desktop\menu\*.jpg'):
im=cv2.imread(filename)
image_list.append(im)
这为我创建了一个图像列表,但我需要它是一个数组形式,形状为 (num_of_images, width, height, 3)
所有图像的形状都相同 有任何想法吗? 谢谢
由于所有图像的形状都相同,我们可以创建一个空数组,然后将每个图像读入其中。
In [103]: file_path = 'C:\Users\Utilizador\Desktop\menu\*.jpg'
In [104]: num_imgs = len(glob.glob(file_path))
In [105]: width, height, channels = 512, 512, 3
In [106]: batch_arr = np.empty((num_imgs, width, height, channels), dtype=np.uint8)
In [107]: for idx, filename in enumerate(glob.glob(file_path)):
img = cv2.imread(filename)
# if img is of different width and height than defined above
# do some resize and then insert in the array.
batch_arr[idx] = img