Caiphas Kain
Caiphas Kain

Reputation: 119

How to construct 2d array from 2d arrays

I need to reconstruct 2d array(from image colour channel) of the specific shape from a number of smaller arrays:

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

This code produces following result:

enter image description here

It differs from the original array: enter image description here

Which is understandable, since np.concatenate does not concatenate arrays in the order I need. The question is how do I merge arrays to restore them to the original state?

blockshaped function is from here

Upvotes: 1

Views: 163

Answers (1)

Divakar
Divakar

Reputation: 221564

Reshape to split the first axis into three axes of lengths 2,2,8, keeping the second axis as it is. Then, permute axes with rollaxis/swapaxes/transpose and a final reshape to have 16 x 16 shape -

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

Sample run for verification -

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

Upvotes: 1

Related Questions