函数cv2.imread(filepath, flags), 其中filename为读入image的路径,flags为标志位。
flags有以下几种参数可供选择:
cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道,可用1作为实参替代
cv2.IMREAD_GRAYSCALE:读入灰度图片,可用0作为实参替代
cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道,可用-1作为实参替代
PS:alpha通道,又称A通道,是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度复信息,定义透明、不透明和半透明区域,其中黑表示全透明,白表示不透明,灰表示半透明
当取flags=0时:读入的是灰度图片,shape为(h,w),当flags=1是读入彩色图片,shape为(h,w,c),还有一个函数cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),可以将彩色图片转换成灰度图片,那么不仅就会产生疑问:
gray_1 = cv2.imread(filename, 0) 和 先 img =cv2.imread(filename, 1)再gray_2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)处理之后gray_1和gray_2的像素值矩阵是否一样,下面进行了如下实验:
lena_face_template = r'D:\Desktop\2-7notebookcourseware\images_operation\face.jpg' # 读取一张图片
img = cv2.imread(lena_face_template, 1) # flags = 1, 以彩色图模式读取图片
gray_1 = cv2.imread(lena_face_template, 0) # flags = 0, 以灰度图模式读取图片
gray_2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将flags=1模式下读取的彩色图转化为灰度图
print('gray_1像素矩阵大小:', img_1.shape)
unequal = 0 # 记录像素值不相等的个数
index = [] # 索引矩阵,记录不相等像数值索引
# for 循环遍历图像矩阵检查像素值不等
for i in range(gray_1.shape[0]):
for j in range(gray_1.shape[1]):
if gray_1[i, j] != gray_2[i, j]:
unequal += 1
index.append((i, j))
print(gray_1[i, j], gray_2[i, j])
print(unequal) # 输出像素值不等总数
print(index) # 输出记录的不等像素值索引
E:\Anaconda\envs\pytorch_gpu_python3.8\python.exe C:/Users/admin/AppData/Roaming/JetBrains/PyCharmCE2021.2/scratches/test3.py
gray_1像素矩阵大小: (110, 85))
15 19
19 20
16 18
16 18
22 23
24 25
21 22
23 24
23 24
206 205
213 212
22 23
24 25
24 25
19 21
218 216
21 23
214 213
24 25
27 28
220 218
215 214
220 217
24 25
221 220
219 217
217 216
220 219
221 220
222 221
219 218
228 225
226 224
231 228
225 224
221 220
222 221
220 219
227 226
19 20
220 219
221 220
226 225
18 19
224 223
19 20
24 25
233 231
225 223
231 228
228 225
231 227
228 224
217 216
225 221
214 212
200 199
207 205
206 205
212 209
209 208
215 211
203 202
193 190
197 193
196 192
192 190
192 190
186 185
69
[(2, 41), (6, 37), (13, 67), (14, 28), (25, 17), (27, 15), (29, 13), (33, 8), (33, 10), (35, 68), (37, 65), (40, 2), (40, 5), (41, 4), (43, 4), (43, 19), (44, 3), (44, 18), (46, 1), (46, 77), (47, 54), (47, 55), (48, 54), (49, 0), (49, 53), (49, 54), (50, 34), (50, 52), (50, 53), (51, 53), (52, 52), (52, 53), (53, 53), (54, 53), (55, 53), (57, 5), (60, 53), (61, 53), (62, 2), (62, 4), (62, 53), (63, 53), (64, 54), (65, 2), (65, 54), (68, 0), (69, 2), (71, 55), (71, 56), (72, 55), (72, 56), (73, 55), (73, 56), (74, 55), (74, 56), (75, 56), (83, 47), (84, 47), (84, 48), (85, 47), (85, 48), (86, 47), (87, 48), (93, 48), (93, 49), (93, 50), (94, 48), (94, 49), (94, 50)]
Process finished with exit code 0
观察发现确实gray _1和gray_2不全部相等, 但不相等的像素值占比69/(110*85)大约占0.74%而且不相等的像素值大小相近。
flags=-1时读取的彩色图像的像素矩阵和flags=1时读取的彩色图像的像素矩阵一样:
import cv2
import matplotlib.pyplot as plt
import numpy as np
lena_face_template = r'D:\Desktop\2-7notebookcourseware\images_operation\face.jpg'
img_1 = cv2.imread(lena_face_template, -1)
img_2 = cv2.imread(lena_face_template, 1)
print(np.array_equal(img_1, img_2))
E:\Anaconda\envs\pytorch_gpu_python3.8\python.exe C:/Users/admin/AppData/Roaming/JetBrains/PyCharmCE2021.2/scratches/test2.py
True
因此上述 先img =cv2.imread(filename, 1)再 gray_2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)与先img =cv2.imread(filename, -1)再 gray_2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)得到的gray_2一样