numpy的shape(0)简单摸索

对于图像来说:

        img.shape[0]:图像的垂直尺寸(高度)

        img.shape[1]:图像的水平尺寸(宽度)

        img.shape[2]:图像的通道数

举例来说,下面是一张300X534X3的图像,我们用代码,进行验证。

 

 

    

代码如下:

import matplotlib.image as mpimg  # mpimg 用于读取图片
 
if __name__ == '__main__':
    img = mpimg.imread('cat.jpg')  # 读取和代码处于同一目录下的 img.png
    # 此时 img 就已经是一个 np.array 了,可以对它进行任意处理
    print(img.shape)  # (512, 512, 3)
    print(img.shape[0])
    print(img.shape[1])
    print(img.shape[2])

  


运行结果如下:

(300, 534, 3)
300
534
3

  


由此证明,上述结果是没有问题的。

而对于矩阵来说:

        shape[0]:表示矩阵的行数()

        shape[1]:表示矩阵的列数()

 

举例如下:

  import numpy as np
 
if __name__ == '__main__':
    w = np.array([[1, 2, 3], [4, 5, 6]])  # 2X3的矩阵
    print(w.shape)
    print(w.shape[0])
    print(w.shape[1])

  


运行结果如下:
 

(2, 3)
2
3

  

由此证明,上述结果是没有问题的。

 


原文:https://blog.csdn.net/xiasli123/article/details/102932607

原文地址:https://www.cnblogs.com/cheflone/p/13216925.html