【问题标题】:Store arrays in unique variables at each for loop iteration in python在python中的每个for循环迭代中将数组存储在唯一变量中
【发布时间】:2017-06-08 06:10:35
【问题描述】:

目前我正在使用 Windows 平台开发一个 python 项目,我需要在其中处理图像。 在此之下,我需要将我的图像分成几个部分,然后将它们存储为唯一的图像。 为此,我需要将我的图像转换为浮点数组,然后将其分段 对分割部分进行排序。 但我卡住的地方是存储这些新生成的数组。

例如,如果我的循环运行 6 次,将生成 6 个新数组(例如 300 X 420 大),我需要将它们存储在每次迭代的唯一变量中,这样它们就不会相互重叠。

我该怎么做?

如何将循环中新生成的数组存储到每次迭代的不同数组中?之后我需要这些数组,所以存储它们对我来说很重要。

生成的数组数量也是动态的!

这是代码:

`

#read image and do its segmentation
 image = cv2.imread("Path to input image\\___.png")
 segments = slic(img_as_float(image), compactness=100.0, n_segments = 2,sigma = 5) 
 #loop to generate individual segments and to store them individually
 for (i, segVal) in enumerate(np.unique(segments)):
    print "[x] inspecting segment %d" % (i)
    mask = np.zeros(image.shape[:2], dtype = "uint8")
    mask[segments == segVal] = 255
    s2 = np.array(segments);
    bb= (cv2.bitwise_and(image, image, mask = mask) )
    #above the unique array is generated
    #store the generated array as unique image at desired location
    cv2.imwrite('Path to store\\__.png', bb)

`

注意:生成的数组已经是二维的,大小为 327 X 500。 所以请给我一个存储二维数组的解决方案。 现在我们使用字典、列表还是新变量很重要:)

编辑

根据一些解决方案,我尝试将新生成的数组插入另一个大数组。但是我仍然遇到一些错误。新的代码块是:

#read image and do its segmentation
 image = cv2.imread("Path to input image\\___.png")
 segments = slic(img_as_float(image), compactness=100.0, n_segments = 2,sigma = 5) 
 #loop to generate individual segments and to store them individually
 for (i, segVal) in enumerate(np.unique(segments)):
    print "[x] inspecting segment %d" % (i)
    mask = np.zeros(image.shape[:2], dtype = "uint8")
    mask[segments == segVal] = 255
    s2 = np.array(segments);
    results= []
    bb= (cv2.bitwise_and(image, image, mask = mask) )
    results.append(bb)
    #above the unique array is generated and appended into a big array
    #store the generated array as unique image at desired location
    cv2.imwrite('Path to store\\__.png', results(i))

我得到的错误是:

此行无法调用列表对象cv2.imwrite('Path to store\\__.png', results(i))

应该做哪些改变?

【问题讨论】:

  • 您可以将所有内容存储在一个大数组中
  • 您可以使用二维数组或字典。可以分享使用的示例代码吗?
  • 它们的数组已经是二维的并且很大。请分享一个示例代码来解释如何将它们存储在一个大数组中。 @moritzg
  • 327x500 对于数组来说是一个相当小的尺寸。你想要的是使用一个列表,动态生成的变量通常是一个坏主意。只需在循环之前创建一个空列表 l 并在执行过程中将每个新数组附加到其中,以便在您只需调用 l[2] 或任何其他号码后访问数组。

标签: python arrays


【解决方案1】:

您不能将它们存储在唯一变量中。但是,您可以创建一个二维数组来存储所有这些。例如,如果生成了 3 个数组:[1,2,3][4,5,6][7,8,9],则二维数组的结构如下:

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

然后,要访问各个数组,您可以简单地访问所需的索引,然后访问所需元素的索引。例如,要获取第一个数组的第二个元素,您可以说类似arr[0][1]。这将返回2

编辑

单个数组是否已经是二维的并不重要。只需添加另一个维度。例如,

# make a big array
bigarray = []

#read image and do its segmentation
 image = cv2.imread("Path to input image\\___.png")
 segments = slic(img_as_float(image), compactness=100.0, n_segments = 2,sigma = 5) 
 #loop to generate individual segments and to store them individually
 for (i, segVal) in enumerate(np.unique(segments)):
    print "[x] inspecting segment %d" % (i)
    mask = np.zeros(image.shape[:2], dtype = "uint8")
    mask[segments == segVal] = 255
    s2 = np.array(segments);
    bb= (cv2.bitwise_and(image, image, mask = mask) )
    #above the unique array is generated
    #store the generated array as unique image at desired location
    cv2.imwrite('Path to store\\__.png', bb)

    # push the generated 2d array to the big array
    bigarray.append(bb)

然后,假设您要访问第一个二维数组,您可以说bigarray[0]。如果要访问第一个二维数组的左上角像素,可以说bigarray[0][0][0]

【讨论】:

  • 我的数组是二维的。有什么方法可以将二维数组存储到另一个数组中?
  • 只需添加另一个维度。数组可以是 3 维的 :) 所以它类似于 arr[0][0][1]。
  • 请举个例子。比如它对我的代码有什么帮助? @ThomasJamesTiam-Lee
  • 感谢您的解决方案@thomasjamestiam-lee! :) 但是我在使用这个 big_array 解决方案时遇到了各种错误。我将编辑我的问题并显示我遇到的错误。请检查并帮助!谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多