【问题标题】:Stack 2D array to 4D将 2D 数组堆叠到 4D
【发布时间】:2020-05-10 04:13:48
【问题描述】:

给定一个包含 6 个具有相同维度 (200, 200) 的二维数组的列表。每三个连续的数组可以堆叠成一个 3D 数组。

期望的输出:

array = (200, 200, 3, 2)

我熟悉 np.dstack:

n = 6
array = []
for i in range(n):
    x = np.random.randn(n1, n2)
    array.append(x)
array = np.dstack(array)

在处理大型数据集时应考虑速度。


编辑: 我创建了用于测试的图像: https://wetransfer.com/downloads/afb0a2fbfbd8b6047a68dad41b56a0d520200509184625/761355

改编帕迪的回答:

import numpy as np
from skimage import io
import glob
from natsort import natsorted

DIR = (Set folder path with the test images)

list_path = glob.glob(DIR + "/*.png")
list_path_sorted = natsorted(list_path)
array = []
for i in range(len(list_path_sorted)):
    image = np.array( io.imread(list_path_sorted[i]) )
    array.append(image)
a = np.dstack(array).reshape(200, 200, 3, 2)

a.shape
>>> (200, 200, 3, 2)

问题:第三维中的顺序不匹配。对此进行测试:

plt.imshow(a[:,:,0,0]) -> should show picture with A1
plt.imshow(a[:,:,2,0]) -> should show picture with A3
plt.imshow(a[:,:,0,1]) -> should show picture with B1
plt.imshow(a[:,:,2,1]) -> should show picture with B3

【问题讨论】:

  • 问题是?
  • 我坚持这样一个条件:一个列表中的三个连续数组形成一个 3D 堆栈。

标签: python numpy


【解决方案1】:

dstack 按照你的建议然后reshape 得到的数组:

import numpy as np

# six arrays of (200, 200)
n = 6
array = []
for i in range(n):
    x = np.random.randn(n1, n2)
    array.append(x)

# EDIT to use 'F' ordering
a = np.dstack(array).reshape(200, 200, 3, 2, order='F')

a.shape
>>> (200, 200, 3, 2)

【讨论】:

  • 感谢您的快速回答。似乎数组的顺序不正确。我用随机测试图像替换了行:x = np.random.randn(n1, n2) 来测试它。
  • 进行编辑以解决问题。通常使用“C”(行)排序,但也可以使用“F”(列,F for Fortran)排序。在这种情况下,图像应该在正确的位置,即。 a[:,:,0,0] = 'A1'a[:,:,0,1] = 'B1'(之前是a[:,:,0,1] = 'A2')。
【解决方案2】:

我不能玩你的图片,所以我会生成一个独特的 (2,2) 数组列表:

In [412]: alist = [np.arange(i,i+4).reshape(2,2) for i in range(6)]                                    
In [413]: alist                                                                                        
Out[413]: 
[array([[0, 1],
        [2, 3]]), array([[1, 2],
        [3, 4]]), array([[2, 3],
        [4, 5]]), array([[3, 4],
        [5, 6]]), array([[4, 5],
        [6, 7]]), array([[5, 6],
        [7, 8]])]

使用dstack,我们得到一个 (2,2,6) 数组

In [414]: arr = np.dstack(alist)                                                                       
In [415]: arr                                                                                          
Out[415]: 
array([[[0, 1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5, 6]],

       [[2, 3, 4, 5, 6, 7],
        [3, 4, 5, 6, 7, 8]]])

(3,2) 重塑产生:

In [417]: arr.reshape(2,2,3,2)                                                                         
Out[417]: 
array([[[[0, 1],
         [2, 3],
         [4, 5]],

        [[1, 2],
         [3, 4],
         [5, 6]]],


       [[[2, 3],
         [4, 5],
         [6, 7]],

        [[3, 4],
         [5, 6],
         [7, 8]]]])

听起来您对该布局不满意(您的编辑有点不清楚)。我们可以转置值

In [419]: arr.reshape(2,2,2,3).transpose(0,1,3,2)                                                      
Out[419]: 
array([[[[0, 3],
         [1, 4],
         [2, 5]],

        [[1, 4],
         [2, 5],
         [3, 6]]],


       [[[2, 5],
         [3, 6],
         [4, 7]],

        [[3, 6],
         [4, 7],
         [5, 8]]]])

按照最后的安排,

In [431]: _419[:,:,2,0]                                                                                
Out[431]: 
array([[2, 3],
       [4, 5]])
In [432]: _413[2]       # alist                                                                               
Out[432]: 
array([[2, 3],
       [4, 5]])

In [435]: _419[:,:,0,1]                                                                                
Out[435]: 
array([[3, 4],
       [5, 6]])
In [436]: _413[3]                                                                                      
Out[436]: 
array([[3, 4],
       [5, 6]])

【讨论】:

  • 我为糟糕的编辑道歉。请查看带有工作代码的更新编辑和图像的下载链接。只需将 DIR 设置为包含图像的下载文件夹即可。我希望它更清楚。
  • 假设您的图片按[A1,A2,A3,B1,B2,B3] 的顺序在列表中,那么我的最后一个订单就是您想要的 - 查看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
相关资源
最近更新 更多