我是张量流和 keras 的新手,我正在尝试为街景门牌号码 (SVHN) 数据集创建 CNN 模型。数据集包含彩色图像,我想将它们转换为灰度图像。我在网上发现了一些代码,声称他们正在将图像转换为灰度,但它只是改变颜色。
人们正在阅读带有灰色色彩图的第二张图像。有什么办法可以真正将这张图像变成灰度图吗?(我不知道如何用这种编程语言处理图像。如果这是一个愚蠢的问题,请原谅我并提供一个简短的解释。)我在下面提供了图像和代码,我将不胜感激。
代码:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
#Read picture:
picture = plt.imread('google.jpg')
print("google logo's shape is: ",picture.shape) #(500, 500, 3)
#saving picture as an np array:
pic_array = np.array(picture)
#Turning image to grayscale
grayscale_pic = np.expand_dims(np.dot(pic_array[...,:3],[0.299, 0.587, 0.144]),axis = 0)
#Dimensions shifted, (probly my mistake):
grayscale_pic = np.moveaxis(grayscale_pic, 0, -1)
print("shape of grayscale pic = ", grayscale_pic.shape) # (500, 500, 1)
plt.imshow(picture) #Figure_1
plt.show()
plt.imshow(grayscale_pic) #Figure_2
plt.show()
海绵宝宝撒
慕田峪7331174
慕桂英3389331
相关分类