将二维阵列堆叠到四维

问题描述 投票:0回答:1

给定一个有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.comdownloadsafb0a2fbfbd8b6047a68dad41b56a0d520200509184625761355。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

改编自帕迪的回答。

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
python numpy
1个回答
2
投票

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)

1
投票

我不能玩弄你的图像,所以我会生成一个独特的(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]])
© www.soinside.com 2019 - 2024. All rights reserved.