I want to divide a 4x4 image with 2 channes into multiple non-overlapping squares.
After that, I want to rebuilt the image.
from skimage.util import view_as_blocks
# create testM array
array([[[[0.53258505, 0.31525832, 0.21378392, 0.5019507 ],
[0.31612498, 0.24320562, 0.93560226, 0.08232264],
[0.89784454, 0.12741783, 0.88049819, 0.29542855],
[0.11336386, 0.71023215, 0.45679456, 0.2318959 ]],
[[0.61038755, 0.74389586, 0.85199794, 0.46680889],
[0.01701045, 0.93953861, 0.03183684, 0.00740579],
[0.58878569, 0.71348253, 0.33221104, 0.12276253],
[0.04026615, 0.53837528, 0.06759152, 0.27477069]]]])
# use view_as_blocks() to get "grid" image
testB = view_as_blocks(testM, block_shape=(1,2,2,2)).reshape(-1,*(1,2,2,2))
Now I have multiple blocks of this array of the size 2x2:
array([[[[[0.53258505, 0.31525832],
[0.31612498, 0.24320562]],
...
[[0.33221104, 0.12276253],
[0.06759152, 0.27477069]]]]])
However, I am not able to reshape it back to its prior shape:
testB.reshape(1,2,4,4)
Leads to this. Every "block" is just appended one value after the other but not treated as a block.
array([[[[0.53258505, 0.31525832, 0.31612498, 0.24320562],
[0.61038755, 0.74389586, 0.01701045, 0.93953861],
[0.21378392, 0.5019507 , 0.93560226, 0.08232264],
[0.85199794, 0.46680889, 0.03183684, 0.00740579]],
[[0.89784454, 0.12741783, 0.11336386, 0.71023215],
[0.58878569, 0.71348253, 0.04026615, 0.53837528],
[0.88049819, 0.29542855, 0.45679456, 0.2318959 ],
[0.33221104, 0.12276253, 0.06759152, 0.27477069]]]])
I have tried multiple .swapaxes()
prior to using reshape()
but just can't get it to work.