gpt4 book ai didi

python - 如何从二维数组构造二维数组

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:42 25 4
gpt4 key购买 nike

我需要从许多较小的数组中重建特定形状的二维数组(来自图像颜色 channel ):

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

此代码产生以下结果:

enter image description here

它与原始数组不同: enter image description here

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

block 状函数来自here

最佳答案

reshape 以将第一个轴分割为长度为 2,2,8 的三个轴,保持第二个轴不变。然后,使用 rollaxis/swapaxes/transpose 排列轴,并最终 reshape 为 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

关于python - 如何从二维数组构造二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com