读写图像文件:
imread()
imwrite()
import numpy as np
img = np.zeros((3, 3), dtype=np.uint8)
print(img)
# 3*3矩阵,9个像素,每个像素点的取值都由一个8位整数来表示,取值范围0-255
"""
[[0 0 0]
[0 0 0]
[0 0 0]]
"""
每个像素都由一个三元数组表示,每个整型向量分别表示一个B、G、R通道。
img_cv = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
print(img_cv)
print(img_cv.shape)
"""
[[[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]]]
(3, 3, 3)
"""
无论采用哪种模式。imread()函数会删除所有的alpha通道的信息(透明度)。
imwrite()函数要求图像为BGR或灰度格式,并且每个通道要有一定的位bit
将图片作为灰度图像,丢失颜色信息
img_gray = cv2.imread('Cat.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('gray', img_gray)
cv2.waitKey(0)
cv2.destroyWindow('gray')