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,所以需要轉換
cv2.COLOR_BGR2RGB
官方文檔說 imread 返回的是一個mat類型的變量
也就是用它來存儲圖片數據
為了弄明白mat到底是個啥玩意,我開始了測試
上面的代碼輸出結果為:
行數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應該是一種矩陣,以圖片中像素的行和列來排布
每一個像素對應的就是[ , , ,]中 RGB的值
圖畫的丑了點,大概就是這樣的
每一個行是一個集合,其中包括着該行的每一列的一個數值
然后mat再把這些行包括起來
構成一個矩陣