对于彩色转灰度,有一个很著名的心理学公式:
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()