reshape an array of images

前端 未结 3 934
陌清茗
陌清茗 2021-01-19 06:15

I have 60000 train_images brought in as a shape (28,28,60000) matrix. It is a numpy.ndarray. I want to convert it to an array of 1 dimensional images, meaning each image is

3条回答
  •  隐瞒了意图╮
    2021-01-19 06:29

    Since this is coming via loadmat, a shape of (28,28,60000) makes sense - MATLAB iterates starting with the last index.

    images.transpose()  # or images.T
    

    reorders the axes, so the result is (60000,28,28). The last two dimensions can combined with a reshape

    images.T.reshape(60000,28*28)
    images.T.reshape(60000,-1)   # short hand
    

    You many need to transpose the 28x28 images, e.g.

    images.transpose([2,0,1])  # instead of the default [2,1,0]
    

    .T is the same as the MATLAB ' (or .').

    images may also be order='F'.


    octave:38> images=reshape(1:30,2,3,5);
    octave:39> save test.mat -v7 images
    octave:40> images
    images =
    
    ans(:,:,1) =
    
       1   3   5
       2   4   6
    
    ans(:,:,2) =
    
        7    9   11
        8   10   12
    ....
    

    I chose test dimensions to be small, and to make it easy to distinguish the different axes.

    In a Ipython session:

    In [15]: data=io.loadmat('test.mat')
    
    In [16]: data
    Out[16]: 
    {'__globals__': [],
     '__header__': 'MATLAB 5.0 MAT-file, written by Octave 3.8.2, 2016-02-10 05:19:18 UTC',
     '__version__': '1.0',
     'images': array([[[  1.,   7.,  13.,  19.,  25.],
            [  3.,   9.,  15.,  21.,  27.],
            [  5.,  11.,  17.,  23.,  29.]],
    
           [[  2.,   8.,  14.,  20.,  26.],
            [  4.,  10.,  16.,  22.,  28.],
            [  6.,  12.,  18.,  24.,  30.]]])}
    
    In [18]: data['images'].T
    Out[18]: 
    array([[[  1.,   2.],
            [  3.,   4.],
            [  5.,   6.]],
    
           [[  7.,   8.],
            [  9.,  10.],
            [ 11.,  12.]],
    ....
    In [19]: data['images'].transpose([2,0,1])
    Out[19]: 
    array([[[  1.,   3.,   5.],
            [  2.,   4.,   6.]],
    
           [[  7.,   9.,  11.],
            [  8.,  10.,  12.]],
     ....
    In [22]: data['images'].transpose([2,1,0]).reshape(5,-1)
    Out[22]: 
    array([[  1.,   2.,   3.,   4.,   5.,   6.],
           [  7.,   8.,   9.,  10.,  11.,  12.],
     ...
    

提交回复
热议问题