對於圖像來說:
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
