reshape an array of images

前端 未结 3 935
陌清茗
陌清茗 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:27

    I think you just need to use reshape:

    >>> images = np.ndarray([60000, 28, 28])
    >>> images.shape
    (60000, 28, 28)
    >>> images_rs = images.reshape([60000, 28*28])
    >>> images_rs.shape
    (60000, 784)
    
    0 讨论(0)
  • 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.],
     ...
    
    0 讨论(0)
  • 2021-01-19 06:31

    You can reshape train_images and verify it by plotting the images,

    Reshaping:

    train_features_images = train_images.reshape(train_images.shape[0],28,28) 
    

    Plotting images:

    import matplotlib.pyplot as plt
    def show_images(features_images,labels,start, howmany):
        for i in range(start, start+howmany):
            plt.figure(i)
            plt.imshow(features_images[i], cmap=plt.get_cmap('gray'))
            plt.title(labels[i])
        plt.show()
    show_images(train_features_images, labels, 1, 10)
    
    0 讨论(0)
提交回复
热议问题