代码之家  ›  专栏  ›  技术社区  ›  blue-sky

将3维图像数组转换为2维

  •  0
  • blue-sky  · 技术社区  · 7 年前

    使用此代码:

    from scipy import misc
    import matplotlib.pyplot as plt
    
    images = data.test.images[0:9]
    plt.imshow(images[0].reshape(28 , 28))
    
    print(images[0].shape)
    

    我绘制图像:

    enter image description here

    使用 misc scipy :

    face = misc.face()
    misc.imsave('face.png', face) # First we need to create the PNG file
    face = misc.imread('face.png')
    
    print(face.shape)
    plt.imshow(face)
    

    enter image description here

    绘制图像:

    如何转换 face 到二维图像,可使用 plt.imshow ?

    使用:

    plt.imshow(face.reshape(768 , 1024))
    

    产生错误:

    ValueError                                Traceback (most recent call last)
    <ipython-input-104-99fef1cec0d2> in <module>()
          6 plt.imshow(face)
          7 
    ----> 8 plt.imshow(face.reshape(768 , 1024))
    
    ValueError: cannot reshape array of size 2359296 into shape (768,1024)
    

    我不是想把图像转换成灰度,而是把它转换成二维而不是三维。

    更新:

    检查单个像素值: print(face[0][0]) 是: [121 112 131] . 我应该取平均值吗 [121 112 131] 作为整形的一部分?

    2 回复  |  直到 7 年前
        1
  •  0
  •   cvanelteren    7 年前

    我真的不明白你想在这里做什么。如果您希望能够绘制三维数据 imshow 这是可以开箱即用的。如果要转换为灰度,请签出 this . 或者,如果您想切片一个3D数据集,从而得到一个二维矩阵,请看 this 还有:

    from scipy.misc import face
    f = face()
    print(f.shape)
    print(f[..., 0].shape) # slicing in last dimension
    import matplotlib.pyplot as plt
    plt.imshow(f[..., 0])
    
        2
  •  0
  •   blue-sky    7 年前

    此代码按预期工作:

       def rgb2gray(rgb):
            return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
    
    
        gray = rgb2gray(face)  
        print(gray.shape)
    
        plt.imshow(gray)
    

    但颜色是歪斜的。 rgb2灰色来源: How can I convert an RGB image into grayscale in Python?

    同时,我也对帮助我实现这一目标的评论表示敬意。

    推荐文章