import cv2 import numpy as np fengmian='picture.jpg' img3=cv2.imread(fengmian) img4=cv2.cvtColor(img3, cv2.COLOR_BGR2RGB) # cv2默認爲bgr順序 h, w, _ = img3.shape #返回height,width,以及通道數,不用因此省略掉 print('行數%d,列數%d' % (h, w)) for i in img4: jihe.append(i) print(jihe) print('集合長度%d' % (len(jihe)))
注意cv2默認爲 BGR順序,而其餘軟件通常使用RGB,因此須要轉換 app
cv2.COLOR_BGR2RGB
官方文檔說 imread 返回的是一個mat類型的變量測試
也就是用它來存儲圖片數據ui
爲了弄明白mat究竟是個啥玩意,我開始了測試spa
上面的代碼輸出結果爲:code
行數9,列數5 [array([[ 94, 121, 252], [ 92, 119, 250], [ 90, 117, 248], [ 89, 116, 247], [ 89, 116, 247]], dtype=uint8), array([[ 93, 120, 251], [ 90, 117, 248], [ 90, 117, 248], [ 91, 118, 249], [ 91, 118, 249]], dtype=uint8), array([[ 94, 121, 252], [ 91, 118, 249], [ 91, 118, 249], [ 93, 120, 251], [ 92, 119, 250]], dtype=uint8), array([[ 96, 123, 254], [ 92, 119, 250], [ 91, 118, 249], [ 93, 120, 251], [ 92, 119, 250]], dtype=uint8), array([[ 96, 123, 254], [ 90, 117, 248], [ 89, 116, 247], [ 91, 118, 249], [ 91, 118, 249]], dtype=uint8), array([[ 94, 121, 252], [ 90, 117, 248], [ 88, 115, 246], [ 88, 115, 246], [ 87, 114, 245]], dtype=uint8), array([[ 94, 121, 252], [ 91, 118, 249], [ 90, 117, 248], [ 88, 115, 246], [ 86, 113, 244]], dtype=uint8), array([[ 93, 120, 251], [ 92, 119, 250], [ 93, 120, 251], [ 90, 117, 248], [ 87, 114, 245]], dtype=uint8), array([[ 96, 123, 254], [ 93, 120, 251], [ 93, 120, 251], [ 92, 119, 250], [ 90, 117, 248]], dtype=uint8)] 集合長度9
能夠看到,mat應該是一種矩陣,以圖片中像素的行和列來排布blog
每個像素對應的就是[ , , ,]中 RGB的值圖片
圖畫的醜了點,大概就是這樣的文檔
每個行是一個集合,其中包括着該行的每一列的一個數值class
而後mat再把這些行包括起來import
構成一個矩陣