【问题标题】:Is there a way to save multiple images to an array in python for later use?有没有办法将多个图像保存到 python 中的数组以供以后使用?
【发布时间】:2020-06-08 23:33:38
【问题描述】:

我正在使用 opencv python。我有一个嵌套的 for 循环,每次迭代都会生成一个图像。有没有办法将这些图像保存到数组中以供以后查看,或者在每次迭代中单独保存每个图像?

【问题讨论】:

  • 我假设在循环之外创建一个列表 images = [] 并在循环中附加它 images.append(image) 就是您所需要的。
  • 太好了,我会试试的。对不起,python也是新手,所以当我这样做的时候。以后如何单独访问图像?
  • 您可以遍历列表:for img in images: 或者您可以访问其各个项目:first_img = images[0]second_img = images[1] 我假设第一个选项就是您所追求的。
  • 您可以在此处阅读有关 python 列表的信息:geeksforgeeks.org/python-list
  • 对,但我还能这样显示图像吗?喜欢查看实际图像而不仅仅是数组值

标签: python python-3.x opencv computer-vision opencv-python


【解决方案1】:

是的,这是可能的。您可以简单地使用append() 函数将每个图像添加到数组中。

假设您的文件中有一些 *.jpg 格式的图像。在循环中,您可以读取每个图像并将它们附加到数组中。循环完成后,您可以从数组中调用所需的图像并用imshow()显示它们

这是一个示例代码:

import cv2
import glob
import numpy as np


image_array = [] // array which ll hold the images
files = glob.glob ("*.jpg")
for myFile in files:
    image = cv2.imread (myFile)
    image_array.append (image) // append each image to array
// this will print the channel number, size, and number of images in the file
print('image_array shape:', np.array(image_array).shape) 

cv2.imshow('frame', image_array[0])

cv2.waitKey(0) 

【讨论】:

  • 所以说我嵌套了 for 循环(抱歉现在不能发布工作中的代码)但是例如:for x in range(0,10,1) then for y in range (0 ,3,1) 我想通过循环的每次迭代来保存图像。我可以根据 x 和 y 访问图像,就像我想查看 x = 3 和 y = 2 的图像一样吗?如果可以,我该怎么做?
猜你喜欢
  • 2019-06-21
  • 2019-09-28
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2011-04-12
  • 2012-06-03
  • 1970-01-01
  • 2015-05-08
相关资源
最近更新 更多