【问题标题】:Slicing the channels of image and storing the channels into numpy array(same size as image). Plotting the numpy array not giving the original image切片图像的通道并将通道存储到 numpy 数组中(与图像大小相同)。绘制未给出原始图像的 numpy 数组
【发布时间】:2019-07-18 14:44:20
【问题描述】:

我分离了彩色图像的 3 个通道。我创建了一个与图像大小相同的新 NumPy 数组,并将图像的 3 个通道存储到 3D NumPy 数组的 3 个切片中。绘制 NumPy 数组后,绘制的图像与原始图像不同。为什么会这样?

imgnew_img 数组的元素相同,但图像不同。

    import matplotlib.image as mpimg
    import matplotlib.pyplot as plt
    import numpy as np

    img=mpimg.imread('/storage/emulated/0/1sumint/kali5.jpg')

    new_img=np.empty(img.shape)

    new_img[:,:,0]=img[:,:,0]
    new_img[:,:,1]=img[:,:,1]
    new_img[:,:,2]=img[:,:,2]

    plt.imshow(new_img)
    plt.show()

期待与原始图像相同的图像。

【问题讨论】:

    标签: matplotlib image-processing rgb numpy-slicing bgr


    【解决方案1】:

    问题是您的新图像将在此行使用float64 的默认数据类型创建:

    new_img=np.empty(img.shape)
    

    除非您指定不同的dtype

    您可以(最好)像这样复制原始图像的dtype

    new_img = np.empty(im.shape, dtype=img.dtype)
    

    或使用类似的东西:

    new_img = np.zeros_like(im) 
    

    或者(最坏的)指定一个你碰巧知道的匹配你的数据,像这样,

    new_img = np.empty(im.shape, dtype=np.uint8)
    

    我认为你有一些理由一次复制一个频道,但如果没有,你可以避免上述所有问题,只需这样做:

    new_img = np.copy(img)
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2020-01-15
      • 2019-08-11
      相关资源
      最近更新 更多