image.shape[] 和array.shape[]的含義

image.shape[]也是將圖片轉換成array.shape[]來輸出的,所以理解了array.shape[]就可以理解image.shape[]了
array.shape表示array的組成情況
shape[0],shape[1],shape[2],shape[3]…表示array不同層次的元素數量
從shape[0]-shape[1]表示的是array從外到內的組成元素個數
舉例如下
定義如下數組 dem1

dem1 = np.array([[[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]],
       [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]]])
print(dem1.shape[0])
print(dem1.shape[1])
print(dem1.shape[2])

輸出結果爲

2
3
8

定義如下數組dem2

dem2 = np.array([[[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
print(dem2.shape[0])
print(dem2.shape[1])

輸出結果爲

3
8

若在dem2的程序中加入 print(dem2.shape[2])
則會顯示 IndexError: tuple index out of range

同理,運用到image.shape[]時,image.shape[0],image.shape[1]表示圖像長,寬,image.shape[0],image.shape[1],image.shape[2]表示圖像長,寬,通道數

*需要注意,數組層數不同,方括號的數量也不同,否則會顯示

TypeError: data type not understood

此問題可參考博客 https://blog.csdn.net/quintind/article/details/77370276

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章