Search code examples
pythonarraysnumpyreshapepixel

Converting an array from one shape to another?


I have a video i.e image sequences saved in an array. The output is:

Output:
(13,9,9)

Where the 13 represents the 13 image sequences and the two 9's represent the pixels. I wish to convert the array into an output like:

Output:
(81,13)

Where the 81 represents the 81 pixel instances and the 13 is capturing the time domain i.e. the video frames in time. I will then be feeding this into my CNN.

Does anyone have any suggestions? As using array.reshape(81,13) of course doesn't work.


Solution

  • Assuming x is the original video 3D array, you need this to convert it to the desired 2D array:

    import numpy as np
    x2d = x.transpose(1, 2, 0).reshape(-1, x.shape[0])
    

    This also works:

    x2d = x.reshape(x.shape[0], -1).T
    

    Essentially the concept is to reshape or transpose the array in such a way that the elements you want in a row should end up in contiguous memory locations.