【问题标题】:How to construct 2d array from 2d arrays如何从二维数组构造二维数组
【发布时间】:2017-08-27 06:13:04
【问题描述】:

我需要从一些较小的数组中重建特定形状的二维数组(来自图像颜色通道):

import numpy as np
from PIL import Image

def blockshaped(arr, nrows, ncols):
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size

If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.

"""
h, w = arr.shape
return (arr.reshape(h//nrows, nrows, -1, ncols)
           .swapaxes(1,2)
           .reshape(-1, nrows, ncols))

pic = Image.open('testimage.bmp') # open image
(r, g, b) = pic.split()# split to channels

c = np.asarray(b) # channel b as array

ar = np.empty((0,256),int) # empty array for appending 

n_a = blockshaped(c,8,8) # dividing array into 4 subarrays
n_a2 = np.concatenate(n_a, axis = 0) #concatenate arrays

for i in n_a2: 
    ar = np.append(ar, i) # append array elements

ar = ar.reshape((16,16)) # reshaping

此代码产生以下结果:

它与原始数组不同:

这是可以理解的,因为 np.concatenate 不会按照我需要的顺序连接数组。问题是如何合并数组以将它们恢复到原始状态?

块状函数来自here

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    重塑以将第一个轴分成三个长度为2,2,8 的轴,保持第二个轴不变。然后,用rollaxis/swapaxes/transpose 置换轴,最后重塑为16 x 16 形状-

    n_a2.reshape(2,2,8,8).swapaxes(1,2).reshape(16,16)
    

    用于验证的示例运行 -

    In [46]: c = np.random.randint(11,99,(16,16))
    
    In [47]: n_a = blockshaped(c,8,8) # dividing array into 4 subarrays
        ...: n_a2 = np.concatenate(n_a, axis = 0) #concatenate arrays
        ...: 
    
    In [48]: out = n_a2.reshape(2,2,8,8).swapaxes(1,2).reshape(16,16)
    
    In [49]: np.allclose(out, c)
    Out[49]: True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2019-11-29
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多