RGB图像灰度化

灰度是用单个通道表示图像,是图像亮度的一种表示方法

RGB图像可通过如下公式转化为灰度图像

Y = 0.2126 * R +0.7152*G + 0.0722*B


源码:

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()


 

 
原图

 

 
灰度图像
 

posted on 2020-03-14 15:02  我坚信阳光灿烂  阅读(461)  评论(0编辑  收藏  举报

导航