【问题标题】:read dynamic number of images of same shape into Python NumPy array将相同形状的动态图像数读入 Python NumPy 数组
【发布时间】:2018-06-21 10:28:55
【问题描述】:

我有这个代码:

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) 的数组形式

所有图像都具有相同的形状 有任何想法吗? 谢谢

【问题讨论】:

  • 是的,它们的形状相同

标签: python numpy opencv image-processing glob


【解决方案1】:

由于所有图像具有相同的形状,我们可以创建一个空数组,然后将每个图像读入其中。

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

【讨论】:

  • 这行得通,但我想要一些更动态的东西,其中图像的数量、宽度、高度不会固定,比如读取图像并将它们作为数组而不是列表
  • 但是是否可以编辑图像,例如在数组中调整它们的大小?或者我应该在将它们添加到数组之前这样做吗?
  • 有一个问题,原始图像是 uint8 格式,当我附加到数组时,它变成了 float64,这弄乱了图像。
猜你喜欢
  • 2019-09-06
  • 2021-10-12
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多