提示:内容整理自:https://github.com/gzr2017/ImageProcessing100Wen
CV小白从0开始学数字图像处理
02灰度化(Grayscale)
将图像灰度化,通过下式计算:
Y = 0.2126 R + 0.7152 G + 0.0722 B
代码如下:
1.引入库
CV2计算机视觉库
import cv2
import numpy as np
2.读入数据
img = cv2.imread("imori.jpg").astype(np.float)
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
3.灰度化
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)
4.保存结果
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()