1

I have an 8X8 image array like this:

a = np.array([[1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4]])

I want to reshape it into an array that each section are separate to each other like this:

a = np.array([
              [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],
              [[2,2,2,2],[2,2,2,2],[2,2,2,2],[2,2,2,2]],
              [[3,3,3,3],[3,3,3,3],[3,3,3,3],[3,3,3,3]],
              [[4,4,4,4],[4,4,4,4],[4,4,4,4],[4,4,4,4]]
             ])

which is a 4X4X4 array, and I can plot section of the image separately. How do I do this?

user3716774
  • 431
  • 3
  • 11

4 Answers4

3

This would do it:

>>> b = np.split(np.hstack(np.split(a, 2)), 4, axis=1)
>>> np.array(b)
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2]],

       [[3, 3, 3, 3],
        [3, 3, 3, 3],
        [3, 3, 3, 3],
        [3, 3, 3, 3]],

       [[4, 4, 4, 4],
        [4, 4, 4, 4],
        [4, 4, 4, 4],
        [4, 4, 4, 4]]])
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
2

Re-arranging the array's strides:

import numpy as np
from numpy.lib.stride_tricks import as_strided

def windows(a, window = (2,2), ss = None, flatten = True):
    '''
    Return a sliding window over a.

    a - numpy ndarray
    window - shape of the window, int for 1d or tuple for 2d+
    ss - int for 1d or tuple for 2d+ how much to slide the window
         defaults to window (no overlap)
    flatten - if True, all slices are flattened, otherwise, there is an 
                  extra dimension for each dimension of the input.

    Returns
        an array containing each n-dimensional window from a
    '''
    if ss is None:
        ss = window
    data_shape = np.array(a.shape)

    # how many windows are there?
    windowed_array_shape = tuple(((data_shape - window) // window) + 1)
    nbr_windows = np.product(windowed_array_shape)

    # the shape of the windowed array
    newshape = windowed_array_shape + window

    # calculate the strides for the windowed array
    newstrides =  tuple(np.array(a.strides) * window) + a.strides

    # use as_strided to 'transform' the array
    windowed_array = as_strided(a, shape = newshape, strides = newstrides)

    if not flatten:
        return windowed_array

    # flatten the windowed array for iteration
    dim = (nbr_windows,) + window
    windowed_array = windowed_array.reshape(dim)
    return windowed_array

a = np.array([[1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [1,1,1,1,2,2,2,2],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4],
              [3,3,3,3,4,4,4,4]])

>>> b = windows(a, (4,4))
>>> b
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2]],

       [[3, 3, 3, 3],
        [3, 3, 3, 3],
        [3, 3, 3, 3],
        [3, 3, 3, 3]],

       [[4, 4, 4, 4],
        [4, 4, 4, 4],
        [4, 4, 4, 4],
        [4, 4, 4, 4]]])
>>>

A couple of other options in this SO q&a

Community
  • 1
  • 1
wwii
  • 23,232
  • 7
  • 37
  • 77
1

Here's an approach using reshape and swapaxes -

B = 4 # Blocksize
m,n = a.shape
out = a.reshape(m//B,B,n//B,B).swapaxes(1,2).reshape(-1,B,B)
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • I get [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] Is there a swap I have missed? shouldn't it be swapaxes(1,2) ? – NaN Oct 27 '16 at 07:08
  • @NaN Ah yes I interpreted it differently the first time around. Fixed it. Thanks for pointing it out! – Divakar Oct 27 '16 at 07:54
0

You can also try this:

np.column_stack((a[:4,:4].ravel(),a[:4,4:8].ravel(),a[4:8,:4].ravel(),a[4:8,4:8].ravel())).T.reshape((4,4,4))

or this one:

np.concatenate(a.reshape(2,4,8).T).T.reshape((4,4,4))
quantummind
  • 2,086
  • 1
  • 14
  • 20