Reputation: 350
I observe the change from repeated imagery in time. I have a 3D numpy array, which should represent a stack of 2 images, each 3x3 px (for simplicity - shape: 2×3×3):
x = np.array([
[
[1,2,3],
[4,5,6],
[7,8,9]
],
[
[10,11,12],
[13,14,15],
[16,17,18]
]
])
I transposed and reshaped it such, that now I have "pixel vectors in time", like this:
y = x.transpose(2,1,0).reshape(-1, x.shape[0])
>>> array([[ 1, 10],
[ 4, 13],
[ 7, 16],
[ 2, 11],
[ 5, 14],
[ 8, 17],
[ 3, 12],
[ 6, 15],
[ 9, 18]])
I made a few scientific computations and ended up with Boolean values for each pixel vector, assessing certain characteristics:
>>> array([[ True, False, True],
[ True, True, True],
[False, False, True],
[False, False, False],
[ True, False, True],
[ True, False, True],
[ True, False, True],
[ True, True, True],
[False, False, True]])
Now, how do I reshape/transpose this array to a shape of (number of characteristics)×3×3 and preserve a logical order of pixels of a single image, as in array x?
I will try to clarify further if necessary. Thanks!
Upvotes: 0
Views: 518
Reputation: 88236
You can use y
(not the mask) to verify that you successfully reconstruct x
, but essentially, keep track of the shape of the intermediate array before reshaping, and just reshape back to that shape and transpose again:
y_t = x.transpose(2,1,0) # same as x.T
y = y_t.reshape(-1, x.shape[0])
y.reshape(y_t.shape).T
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
Otherwise you can also just use the initial array's shape as a reference:
y = x.transpose(2,1,0).reshape(-1, x.shape[0])
y.reshape(x.shape[::-1]).T
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
Upvotes: 1