-1

Im pretty new to python. I have a np array (image) of size (128x128) and I want to divide it into 64 patches of 16x16. how can I slice the array? and How can I reassemble it into a 128x128 array afterwards?

I tried this but it's adding memory expense when I try to reassemble it

import numpy as np
xpx = np.random.rand(5,128,128)

new=np.zeros((5,64,16,16))


for i in range(0,5):
  w=0
  for j in range(0,128,16):
    for k in range(0,128,16):
      new[i,w]=xpx[i,j:j+16,k:k+16]
      w=w+1
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
sab sab
  • 17
  • 2

1 Answers1

0

Split the array into 64 patches of size 16x16

patches = np.split(image, 64, axis=0)

Reassemble the patches back into a single array

reassembled = np.concatenate(patches, axis=0)
kalzso
  • 502
  • 2
  • 6
  • 27