对于彩色转灰度,有一个很著名的心理学公式:
Gray = R*0.299 + G*0.587 + B*0.114
from PIL import Image
import matplotlib.image as mpimg
import numpy as np
import matplotlib.pyplot as plt
img = mpimg.imread('C:\\Users\\Administrator\\Desktop\\sd\\test.jpg')#输出一个数组
def RGB_to_Grey(image):
return np.dot(image[...,:3],[0.299,0.587,0.114])
grey = RGB_to_Grey(img)
plt.imshow(grey,cmap='Greys_r')
plt.show()
本文介绍了一种将彩色图像转换为灰度图像的方法,并提供了一个具体的Python实现案例。使用了PIL库读取图像,并通过matplotlib显示处理后的灰度图像。

231

被折叠的 条评论
为什么被折叠?



