RGB转灰度?——3个通道变成1个通道

哈喽,之前我注意过这个问题,源于mnist,如果数字是RGB的咋办,训练好的模型咋识别?

当读图的时候是3个通道的,彩色都是如此,我想要灰度图咋办?

小明哥的绝招是直接读其中一个通道即可,因为我之前查找rgb2灰度,发现用cv,作为cv菜鸟,我还是不想用这玩意。

下面是举例子说明:

当我想用scipy,PIL进行读取图片时,我发现物是人非。

scipy中的函数都没了,我已经活了两个世纪了吗??

    img=scipy.misc.imread(path)
AttributeError: module 'scipy.misc' has no attribute 'imread'
>>> import scipy.ndimage
>>> scipy.ndimage.imread()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    scipy.ndimage.imread()
AttributeError: module 'scipy.ndimage' has no attribute 'imread'

还好PIL枕头库还在,果断用枕头库。

from PIL import Image

给我哥来个特写

扫描二维码关注公众号,回复: 11284825 查看本文章

但是卧槽,咋不能取得里面的数值啊?卧槽。

>>> img=Image.open(path)
>>> import matplotlib.pyplot as plt
>>> plt.imshow(img)
<matplotlib.image.AxesImage object at 0x000002330EF06DD8>
>>> plt.show()

>>> img.shape
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    img.shape
AttributeError: 'JpegImageFile' object has no attribute 'shape'

>>> img.size
(710, 486)

还是拿不到数据,PIL枕头库就是垃圾,转投matplotlib

后来发现加个np.array就好了。与matplotlib效果一样。

不再细说。不懂你问我啊,哈哈

后记:

用了skimage的灰度图,imshow出来效果不错。哈哈

下面看看3个通道中的各个,直接imshow最后一个索引img[:,:,0]这种,哈哈,这是不是一种扩大数据的方法,是不是很厉害??

另外有相关问题可以加入QQ群讨论,不设微信群

QQ群:868373192 

语音深度学习及信号处理群

猜你喜欢

转载自blog.csdn.net/SPESEG/article/details/102854528