灰度是用单个通道表示图像,是图像亮度的一种表示方法
RGB图像可通过如下公式转化为灰度图像
源码:
import cv2
import numpy as np
# Gray scale
def BGR2GRAY(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# Gray scale
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)
return out
# Read image
img = cv2.imread("../paojie.jpg").astype(np.float)
# Grayscale
out = BGR2GRAY(img)
# Show results
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()