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再把这些行包括起来
构成一个矩阵